JAVA操作HDFS的文件系统

JAVA操作HDFS的文件系统

前言:上篇文章介绍了如何利用Shell去操作HDFS中的文件,本文介绍使用Java代码去操作HDFS中的文件,它的操作内容和shell的操作内容和方法基本一致,开发集成工具选择IDEA。

一、新建Maven项目

在这里插入图片描述
填写项目名称和路径,完成即可
在这里插入图片描述

二、添加依赖

打开maven配置文件pom.xml,添加如下依赖,这个下载过程根据网络情况,可能要很久…

<dependencies>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>2.9.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13</version>
        </dependency>
    </dependencies>

maven自动下载完成后,会看到如下三个依赖
在这里插入图片描述

三、开启HDFS集群

目前hdfs中只有一个aa.txt文件
在这里插入图片描述
注意:在浏览器中查看HDFS,需要在开启hdfs集群后(start-dfs.sh),关闭防火墙(systemctl stop firewalld)

四、编写Java程序操作HDFS中的文件

1、在java的package下新建package目录,然后新建TestHDFS类

在这里插入图片描述

操作1、获取HDFS中的所有文件的路径

1、实现代码

    public class TestHDFS {

   //获取hdfs中的所有文件路径
    @Test
    public void GetHDFSPath() throws IOException {
        //配置类(Configuration),用于配置hadoop中的core-sit.xml和hdfs-sit.xml
        Configuration configuration=new Configuration();
        configuration.set("fs.defaultFS","192.168.23.131:9000");//name和value两个参数,就是我们在hadoop的core-site.xml中配置的参数(指定哪台机器是namenode)
        FileSystem fileSystem=FileSystem.get(configuration);
        Path path=new Path("/");
        FileStatus[] fileStatuses = fileSystem.listStatus(path);
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println(fileStatus.getPath());
        }

    }
}

运行GetHDFSPath方法,获得文件路径
在这里插入图片描述
2、对上面的方法进行优化,得到以下代码

public class TestHDFS {

    private FileSystem fileSystem;

    @Before
    public void befor() throws IOException {
        //配置类(Configuration),用于配置hadoop中的core-sit.xml和hdfs-sit.xml
        Configuration configuration=new Configuration();
        configuration.set("fs.defaultFS","192.168.23.131:9000");//name和value两个参数,就是我们在hadoop的core-site.xml中配置的参数(指定哪台机器是namenode)
        fileSystem=FileSystem.get(configuration);
    }

   //获取hdfs中的所有文件路径
    @Test
    public void GetHDFSPath() throws IOException {
        Path path=new Path("/");
        FileStatus[] fileStatuses = fileSystem.listStatus(path);
        for (FileStatus fileStatus : fileStatuses) {
            System.out.println(fileStatus.getPath());
        }
    }

    @After
    public void after() throws IOException {
        fileSystem.close();
    }
}

操作2、向HDFS中上传文件

1、实现方法代码

@Test
    public void UploadFileToHDFS() throws IOException {

        //上传到hdfs的文件流
        FSDataOutputStream fsDataOutputStream = fileSystem.create(new Path("/1.txt"));
        //从本地文件读取的文件流
        FileInputStream fileInputStream=new FileInputStream(new File("F:\\MyLog.Log"));
        //将本地文件流拷贝到待上传的hdfs文件流,完成上传
        IOUtils.copyBytes(fileInputStream,fsDataOutputStream,1024,true);
    }

在这里插入图片描述
注意:上传文件需要root用户的权限,如果再hdfs中没有配置关闭权限,需要在java代码中,修改当前用户为root

//System.setProperty("HADOOP_USER_NAME","root");如果未在hadoop配置关闭root权限,则需要执行此代码,才能实现java向hdfs上传下载文件的功能

2、补充:如何查找hadoop的配置属性

因为不关闭root权限,会有很多操作受限,建议再hadoop的hdfs-site.xml的配置文件中修改该权限属性。那么这个配置是怎么修改的呢?
在hadoop的官方文档中(https://hadoop.apache.org/docs/r2.9.2/)介绍了core-site.xml以及hdfs-site.xml的默认配置,我们可以查看这些默认属性配置的说明,进而去修改我们需要的属性配置
在这里插入图片描述
修改root权限是在hdfs-site.xml中,我们可以进入hdfs-default.xml中查找权限的相关属性(右键浏览器页面->查看源文件)
在这里插入图片描述
我们发现修改root权限的默认属性配置,然后我们把它拷贝,将ture修改为false配置到hadoop的hdfs-site.xml
在这里插入图片描述

操作3、从HDFS中下载文件

1、实现代码

 //从HDFS中下载文件
    @Test
    public void DownloadFileToLoacal() throws IOException {
        Path src=new Path("/1.txt");//待下载的hdfs中的文件路径
        Path dst=new Path("E:\\");//下载到本地的目录路径
        fileSystem.copyToLocalFile(false,src,dst,true);//最后一个参数要设置为true,这个是解决跨平台问题的
    }

猜你喜欢

转载自blog.csdn.net/qq_34720818/article/details/106849506