你可以使用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文件下載到本地。