要將HDFS中的文件下載到本地,可以使用以下方法:
hadoop fs -get hdfs://<HDFS文件路徑> <本地文件路徑>
例如:
hadoop fs -get hdfs://localhost:9000/user/test.txt /tmp/test.txt
hadoop distcp hdfs://<HDFS文件路徑> file://<本地文件路徑>
例如:
hadoop distcp hdfs://localhost:9000/user/test.txt file:///tmp/test.txt
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class HDFSDownload {
public static void main(String[] args) {
try {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(conf);
Path hdfsFilePath = new Path("hdfs://localhost:9000/user/test.txt");
Path localFilePath = new Path("/tmp/test.txt");
fs.copyToLocalFile(hdfsFilePath, localFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}
}