温馨提示×

如何使用java下载hdfs文件

小亿
92
2024-03-27 15:14:54
栏目: 编程语言

要使用Java下载HDFS文件,可以使用Hadoop的FileSystem API来实现。以下是一个简单的示例代码:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class HDFSFileDownload {

    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://localhost:9000");

        try {
            FileSystem fs = FileSystem.get(conf);
            Path srcPath = new Path("/path/to/source/file/in/hdfs");
            Path dstPath = new Path("/path/to/destination/file/on/local/machine");

            if (!fs.exists(srcPath)) {
                System.out.println("Source file does not exist");
                return;
            }

            InputStream in = fs.open(srcPath);
            BufferedInputStream reader = new BufferedInputStream(in);
            BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(dstPath.toString()));

            byte[] buffer = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = reader.read(buffer)) > 0) {
                writer.write(buffer, 0, bytesRead);
            }

            reader.close();
            writer.close();
            fs.close();

            System.out.println("File downloaded successfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

在这个示例中,首先创建一个Hadoop的Configuration对象,并设置HDFS的地址。然后通过FileSystem.get方法获取一个FileSystem对象。接着指定HDFS中的源文件路径和本地机器上的目标文件路径,并通过FileSystem的open方法打开源文件。通过BufferedInputStream和BufferedOutputStream来读取和写入文件内容,并最终将文件下载到本地机器上。

请记得替换示例中的"/path/to/source/file/in/hdfs"和"/path/to/destination/file/on/local/machine"为实际的文件路径。此外,还要根据HDFS的配置修改"fs.defaultFS"的值。

0