hadoop02_HDFS的API操作

2023-12-22 08:40:11

HDFS的API操作

1 HDFS 核心类简介

Configuration类:处理HDFS配置的核心类。

FileSystem类:处理HDFS文件相关操作的核心类,包括对文件夹或文件的创建,删除,查看状态,复制,从本地挪动到HDFS文件系统中等。

Path类:处理HDFS文件路径。

IOUtils类:处理HDFS文件读写的工具类。

2 HDFS文件处理类FileSystem的核心方法介绍:

1. FileSystem get(URI uri, Configuration conf)

根据HDFSURI和配置,创建FileSystem实例


2. public boolean mkdirs(Path f) throws IOException

根据路径创建HDFS文件夹


3. FSDataOutput Stream create(Path f, boolean overwrite)

根据具体的路径创建文件,并且知名是否以重写的方式


4. abstract boolean delete(Path f, boolean recursive)

根据路径删除文件


5. abstract FileStatus[] listStatus(Path f)

根据路径,返回该路径下所有文件夹或文件的状态。


6. Void moveFromLocalFile(Path src, Path dst)

将本地路径下的文件,挪动到HDFS的指定路径下


7. FSDataInputStream open(Path f)

打开指定路径下的文件内容

3 执行流程

maven依赖

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.8.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-client</artifactId>
            <version>3.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-hdfs</artifactId>
            <version>3.3.2</version>
        </dependency>
        
    </dependencies>

hdfs 创建文件夹

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

        Configuration conf = new Configuration();
//		conf.set("fs.defaultFS", "hdfs://hadoop102:9000");

        // 1 获取hdfs客户端对象
//		FileSystem fs = FileSystem.get(conf );
        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf, "root");


        // 2 在hdfs上创建路径
        fs.mkdirs(new Path("/dir01/"));

        // 3 关闭资源
        fs.close();

        System.out.println("over");
    }

1 HDFS文件上传(测试参数优先级)


    // 1 文件上传
    @Test
    public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取fs对象
        Configuration conf = new Configuration();
        conf.set("dfs.replication", "2");
        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");

        // 2 执行上传API
        fs.copyFromLocalFile(new Path("e:/info.txt"), new Path("/file1.txt"));

        // 3 关闭资源
        fs.close();
    }

2 HDFS文件下载

  // 2 文件下载
    @Test
    public void testCopyToLocalFile() throws URISyntaxException, IOException, InterruptedException {

        // 1 获取对象
        Configuration conf = new Configuration();
       // conf.set("dfs.replication", "2");
        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");

        // 2 执行下载操作
//		fs.copyToLocalFile(new Path("/banhua.txt"), new Path("e:/banhua.txt"));
        fs.copyToLocalFile(false, new Path("/file1.txt"), new Path("e:/file2.txt"), true);

        // 3 关闭资源
        fs.close();
    }

3 HDFS文件夹删除

// 3 文件删除
    @Test
    public void testDelete() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");

        // 2 文件删除
        fs.delete(new Path("/dir01"), true);

        // 3 关闭资源
        fs.close();
    }

4 HDFS文件名更改

// 4 文件更名
    @Test
    public void testRename() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");

        // 2 执行更名操作
        fs.rename(new Path("/file1.txt"), new Path("/file111.txt"));

        // 3 关闭资源
        fs.close();
    }

5 HDFS文件详情查看

查看文件名称、权限、长度、块信息

// 5 文件详情查看
    @Test
    public void testListFiles() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");

        // 2 查看文件详情
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);

        while(listFiles.hasNext()){
            LocatedFileStatus fileStatus = listFiles.next();

            // 查看文件名称、权限、长度、块信息
            System.out.println(fileStatus.getPath().getName());// 文件名称
            System.out.println(fileStatus.getPermission());// 文件权限
            System.out.println(fileStatus.getLen());// 文件长度

            BlockLocation[] blockLocations = fileStatus.getBlockLocations();

            for (BlockLocation blockLocation : blockLocations) {

                String[] hosts = blockLocation.getHosts();

                for (String host : hosts) {
                    System.out.println(host);
                }
            }

            System.out.println("------ok分割线--------");
        }

        // 3 关闭资源
        fs.close();
    }

6 HDFS文件和文件夹判断

// 6 判断是文件还是文件夹
    @Test
    public void testListStatus() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取对象
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), conf , "root");

        // 2 判断操作
        FileStatus[] listStatus = fs.listStatus(new Path("/"));

        for (FileStatus fileStatus : listStatus) {

            if (fileStatus.isFile()) {
                // 文件
                System.out.println("f:"+fileStatus.getPath().getName());
            }else{
                // 文件夹
                System.out.println("d:"+fileStatus.getPath().getName());
            }
        }

        // 3 关闭资源
        fs.close();
    }

4 HDFS的I/O流操作

上面我们学的API操作HDFS系统都是框架封装好的。那么如果我们想自己实现上述API的操作该怎么实现呢?
我们可以采用IO流的方式实现数据的上传和下载。

1 HDFS文件上传

1.需求:把本地e盘上的banhua.txt文件上传到HDFS根目录
2.编写代码

@Test
    public void putFileToHDFS() throws IOException, InterruptedException, URISyntaxException {

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), configuration, "root");

        // 2 创建输入流
        FileInputStream fis = new FileInputStream(new File("e:/hahaha.txt"));

        // 3 获取输出流
        FSDataOutputStream fos = fs.create(new Path("/hahaha.txt"));

        // 4 流对拷
        IOUtils.copyBytes(fis, fos, configuration);

        // 5 关闭资源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fs.close();
    }

2 HDFS文件下载

1.需求:从HDFS上下载banhua.txt文件到本地e盘上
2.编写代码

@Test
    public void getFileFromHDFS() throws IOException, InterruptedException, URISyntaxException{

        // 1 获取文件系统
        Configuration configuration = new Configuration();
        FileSystem fs = FileSystem.get(new URI("hdfs://node1:9820"), configuration, "root");

        // 2 获取输入流
        FSDataInputStream fis = fs.open(new Path("/jinan/info/lenovo/hello.txt"));

        // 3 获取输出流
        FileOutputStream fos = new FileOutputStream(new File("e:/hello.txt"));

        // 4 流的对拷
        IOUtils.copyBytes(fis, fos, configuration);

        // 5 关闭资源
        IOUtils.closeStream(fos);
        IOUtils.closeStream(fis);
        fs.close();
    }

文章来源:https://blog.csdn.net/tonyshi1989/article/details/135144185
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。