javaAPI操作HDFS文件

javaAPI写入HDFS文件。

package com.twq.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
/**
* 需要执行
* hadoop fs -chmod 757 hdfs://master:9999/user/hadoop-twq/cmd/
*/
public class FileWriter {
    public static void main(String[] args) throws IOException {
        String content = "this is an example";
        String dest = "hdfs://master:9999/user/hadoop-twq/cmd/java_writer.txt";
        Configuration configuration = new Configuration();
         FileSystem fileSystem = FileSystem.get(URI.create(dest), configuration);
        FSDataOutputStream out = fileSystem.create(new Path(dest));
        out.write(content.getBytes("UTF-8"));
        out.close();
    }
}
image.png

javaAPI读取HDFS

package com.twq.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.*;
import java.net.URI;

public class FileReader {
    public static void main(String[] args) throws IOException {
        String dest = "hdfs://master:9999/user/hadoop-twq/cmd/java_writer.txt";
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(dest), configuration);
        FSDataInputStream in = fileSystem.open(new Path(dest));

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));

        String line = null;

        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
    }
}
image.png

javaAPI删除HDFS

package com.twq.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;

public class FileDeleter {

    public static void main(String[] args) throws IOException {
        String dest = "hdfs://master:9999/user/hadoop-twq/cmd/java_writer.txt";

        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(URI.create(dest), configuration);
        fileSystem.delete(new Path(dest), false);
        fileSystem.delete(new Path(""), true);
    }
}
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容