hadoop之 HDFS读写 java interface

本文介绍了如何使用Java API进行HDFS文件的基本操作,包括读取、写入、列出目录及删除文件等。通过示例代码展示了如何实现这些功能,并解释了关键部分的工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

读取文件

    public static void readFile(String uri) {

        Configuration configuration = new Configuration();
        InputStream inputStream = null;
        try {
            // String uri = "hdfs://hadoop:9000/hadoop/temp/1901";
            FileSystem fileSystem = FileSystem.get(URI.create(uri), configuration);
            inputStream = fileSystem.open(new Path(uri));
            IOUtils.copyBytes(inputStream, System.out, 4096, false);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(inputStream);
        }
    }

seek 读取

    public static void readFileByFSDATAInputStream(String uri) {
        Configuration configuration = new Configuration();
        FSDataInputStream stream = null;
        try {
            FileSystem fileSystem = FileSystem.get(URI.create(uri), configuration);
            stream = fileSystem.open(new Path(uri));
            IOUtils.copyBytes(stream, System.out, 4096, false);

            stream.seek(0);
            IOUtils.copyBytes(stream, System.out, 4096, false);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeStream(stream);
        }
    }

写文件

    public static void writeData(String uri, String local) {


        try {
            InputStream inputStream = new BufferedInputStream(new FileInputStream(local));
            FileSystem fileSystem = FileSystem.get(configuration);


            // 创建一个新文件 必须保证uri不存在  如果向一个已经存在的文件添加内容,使用fileSystem.append(Path p)
            //fileSystem.exists(new Path("")); //判断文件是否存在
            //fileSystem.mkdirs(new Path(""));  //创建文件夹
            FSDataOutputStream outputStream = fileSystem.create(new Path(uri), new Progressable() {
                // 报告写入过程
                @Override
                public void progress() {
                    System.out.print(".");
                }
            });

            IOUtils.copyBytes(inputStream, outputStream, 4096, true);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

列出文件列表

    public static void listFiles(String uri) {

        try {
            FileSystem fileSystem = FileSystem.get(configuration);
          //  FileStatus fileStatus = fileSystem.getFileStatus(new Path(uri));
            FileStatus[] statuses = fileSystem.listStatus(new Path(uri));
            Path[] listPaths = FileUtil.stat2Paths(statuses);
            for (Path path : listPaths){
                System.out.println(path);
            }
            // fileSystem.listStatus(Path[] files); //批量读取

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

删除文件

    public static void deleteFile(String uri){

        try {
            FileSystem fileSystem = FileSystem.get(configuration);
            fileSystem.delete(new Path(uri),true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

源码

 /** Delete a file.
   *
   * @param f the path to delete.
   * @param recursive if path is a directory and set to 
   * true, the directory is deleted else throws an exception. In
   * case of a file the recursive can be set to either true or false. 
   * @return  true if delete is successful else false. 
   * @throws IOException
   */
  public abstract boolean delete(Path f, boolean recursive) throws IOException;

如果删除文件夹需要 设置为true,如果未flase会抛出异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值