java操作HDFS

Posted 花未全开*月未圆

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java操作HDFS相关的知识,希望对你有一定的参考价值。


 

创建文件

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.permission.FsAction;
import org.apache.hadoop.fs.permission.FsPermission;

import java.io.IOException;

public class FileMaker {

    public static void main(String[] args) throws IOException {

        Configuration configuration = new Configuration();

        FileSystem fileSystem = FileSystem.get(configuration);

       fileSystem.mkdirs(new Path(""));

       fileSystem.mkdirs(new Path(""), new FsPermission(FsAction.ALL, FsAction.EXECUTE, FsAction.NONE));
    }
}

 

读取文件:

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();

    }
}

写入文件:

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();
    }
}

 

删除文件:

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);
    }
}

 

 

 

 

以上是关于java操作HDFS的主要内容,如果未能解决你的问题,请参考以下文章

Java操作HDFS代码样例

Java代码操作HDFS(在/user/root/下面創建目錄)

HDFS的Java操作方式

Java操作HDFS开发环境搭建以及HDFS的读写流程

[0014] HDFS 常用JAVA 操作实战

利用JAVA API远程进行HDFS的相关操作