hdfs 数据导出
时间: 2023-08-08 08:08:02 浏览: 89
要将 HDFS 中的数据导出,可以使用以下两种方法:
1. 使用 Hadoop 命令行工具
可以使用 Hadoop 命令行工具中的 `hadoop fs` 命令将数据导出。例如,如果要将 `/input` 目录下的数据导出到本地的 `/tmp` 目录中,可以执行以下命令:
```
hadoop fs -get /input /tmp
```
2. 使用 HDFS API
可以使用 HDFS API 来编写 Java 程序将数据导出。以下是一个简单的 Java 代码示例,用于将 HDFS 中的文件导出到本地:
```java
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HdfsExport {
public static void main(String[] args) throws IOException, URISyntaxException {
String hdfsUri = "hdfs://localhost:9000";
String filePath = "/input/file.txt";
String localPath = "/tmp/file.txt";
Configuration config = new Configuration();
FileSystem fs = FileSystem.get(new URI(hdfsUri), config);
Path hdfsPath = new Path(filePath);
Path localPath = new Path(localPath);
InputStream in = fs.open(hdfsPath);
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(localPath.toString()));
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
in.close();
out.close();
fs.close();
}
}
```
这段代码使用 HDFS API 打开 HDFS 中的文件,将其读入缓冲区并写入本地文件。
阅读全文
相关推荐

















