HDFS文件系统下载文件
时间: 2023-11-17 17:59:17 浏览: 106
要将HDFS文件系统中的文件下载到本地,可以使用FileSystem类的open()方法获取文件的输入流,然后使用Java IO流将文件写入本地文件系统。具体步骤如下:
1. 获取Hadoop配置对象Configuration。
2. 使用FileSystem.get()方法获取HDFS文件系统对象。
3. 使用FileSystem对象的open()方法获取文件的输入流。
4. 使用Java IO流将文件写入本地文件系统。
5. 关闭输入流和文件系统对象。
示例代码如下:
```
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.InputStream;
import java.io.OutputStream;
public class HDFSFileDownload {
public static void main(String[] args) throws Exception {
String hdfsFilePath = "/path/to/hdfs/file";
String localFilePath = "/path/to/local/file";
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
InputStream in = fs.open(new Path(hdfsFilePath));
OutputStream out = new BufferedOutputStream(new FileOutputStream(localFilePath));
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
fs.close();
}
}
```
阅读全文
相关推荐

















