Java-API对HDFS文件的读写操作(一)

我们要是提到大数据的文件存储,那么久离不开分布式文件系统;闲话少讲,我们今天的目标是:1.读取 hdfs 上的文件内容,2.将本地文件上传到 hdfs 上。
我们要在 hdfs 中操作文件,那么必须要先做好启动集群服务等这些基本条件,那么接下来我们开始操作:
一、启动集群服务并查看相应的进程

《Java-API对HDFS文件的读写操作(一)》 image.png

二、接下来我们使用命令操作hdfs

创建指定的目录:bin/hdfs dfs -mkdir -p /user/hdfs
将Linux本地的文件上传至hdfs指定的目录中:bin/hdfs dfs -put etc/hadoop/mapred-site.xml /user/hdfs
查看文件内容:bin/hdfs dfs -text /user/hdfs/mapred-site.xml
更多的命令操作可以在命令行中输入以下命令查看:bin/hdfs dfs

接下来我们可以打开此网页(http://bigdata-pro01.lcy.com:50070/)进入指定的目录浏览效果图:

《Java-API对HDFS文件的读写操作(一)》 image.png

三、打开IDEA并创建maven工程并配置依赖

打开 https://mvnrepository.com/ 网址搜索相应版本配置 pom.xml:

<project>
  ......
  ......
  ......
<properties>
        <hadoop.version>2.7.2</hadoop.version>
    </properties>

    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>${hadoop.version}</version>
        </dependency>

    </dependencies>
</project>

这里还需要注意的是我们在本地中操作 hdfs 以及需要控制台中输出日记信息,因此我们需要将 hadoop-2.7.2/etc/hadoop/ 目录下的三个文件拷贝只 maven 工程中,如图:

《Java-API对HDFS文件的读写操作(一)》 image.png

四、在制定包中创建操作 HdfsApplication类

1.在类中创建获取配置对象的方法:

private FileSystem getFiledSystem() throws IOException {
        Configuration configuration = new Configuration();
        FileSystem fileSystem = FileSystem.get(configuration);
        return fileSystem;
    }

2.创建读取文件的方法并使其在控制台中输出:

private void readHDFSFile(String filePath){
        FSDataInputStream fsDataInputStream = null;

        try {
            Path path = new Path(filePath);
            fsDataInputStream = this.getFiledSystem().open(path);
            IOUtils.copyBytes(fsDataInputStream, System.out, 4096, false);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(fsDataInputStream != null){
                IOUtils.closeStream(fsDataInputStream);
            }
        }

    }

3.创建将本地文件写入到hdfs中的方法:

private void writeHDFS(String localPath, String hdfsPath){
        FSDataOutputStream outputStream = null;
        FileInputStream fileInputStream = null;

        try {
            Path path = new Path(hdfsPath);
            outputStream = this.getFiledSystem().create(path);
            fileInputStream = new FileInputStream(new File(localPath));
            //输入流、输出流、缓冲区大小、是否关闭数据流,如果为false就在 finally里关闭
            IOUtils.copyBytes(fileInputStream, outputStream,4096, false);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(fileInputStream != null){
                IOUtils.closeStream(fileInputStream);
            }
            if(outputStream != null){
                IOUtils.closeStream(outputStream);
            }
        }

    }

  1. 主方法中调用:
public static void main(String[] args) {
        HdfsApplication hdfsApp = new HdfsApplication();

//        String filePath = "/user/hdfs/mapred-site.xml";
//        hdfsApp.readHDFSFile(filePath);

        String localPath= "C:\\Users\\Administrator\\Desktop\\abc\\lcy.txt";
        String hdfsPath = "hdfs://bigdata-pro01.lcy.com:9000/user/hdfs/yin.txt";
        hdfsApp.writeHDFS(localPath, hdfsPath);

    }

效果图如下:

《Java-API对HDFS文件的读写操作(一)》 image.png

最后,感谢指导老师,感谢所有资源,愿各位志同道合的人不断的进取,不断的收获。。。

    原文作者:小飞牛_666
    原文地址: https://www.jianshu.com/p/52506c7bf662
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞