如何用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();}}}

在上面的示例中,你需要将hdfsFilePathlocalFilePath替换为实际的HDFS文件路径和本地文件路径。然后运行程序,即可将HDFS文件下载到本地。


上一篇:jvm内存溢出故障如何排查

下一篇:Python正则化匹配列表怎么实现


Java hdfs
Copyright © 2002-2019 测速网 www.inhv.cn 皖ICP备2023010105号
测速城市 测速地区 测速街道 网速测试城市 网速测试地区 网速测试街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!

热门搜索 城市网站建设 地区网站制作 街道网页设计 大写数字 热点城市 热点地区 热点街道 热点时间 房贷计算器