如何用java下载hdfs文件到本地
你可以使用Hadoop的FileSystem API来下载HDFS文件到本地。以下是一个简单的Java程序示例:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class DownloadHdfsFile {
public static void main(String[] args) {
String hdfsFilePath = "/path/to/hdfs/file";
String localFilePath = "/path/to/local/file";
Configuration conf = new Configuration();
try {
FileSystem fs = FileSystem.get(conf);
Path hdfsPath = new Path(hdfsFilePath);
InputStream inputStream = fs.open(hdfsPath);
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(localFilePath));
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outputStream.close();
fs.close();
System.out.println("File downloaded successfully!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
在上面的示例中,你需要将hdfsFilePath
和localFilePath
替换为实际的HDFS文件路径和本地文件路径。然后运行程序,即可将HDFS文件下载到本地。
版权声明
本文仅代表作者观点,不代表米安网络立场。
上一篇:Go中如何用反射遍历任意类型切片的元素 下一篇:java定义枚举的方法是什么
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。