要使用Java下載HDFS文件,可以使用Hadoop的FileSystem API來實現。以下是一個簡單的示例代碼:
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class HDFSFileDownload {
public static void main(String[] args) {
Configuration conf = new Configuration();
conf.set("fs.defaultFS", "hdfs://localhost:9000");
try {
FileSystem fs = FileSystem.get(conf);
Path srcPath = new Path("/path/to/source/file/in/hdfs");
Path dstPath = new Path("/path/to/destination/file/on/local/machine");
if (!fs.exists(srcPath)) {
System.out.println("Source file does not exist");
return;
}
InputStream in = fs.open(srcPath);
BufferedInputStream reader = new BufferedInputStream(in);
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(dstPath.toString()));
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = reader.read(buffer)) > 0) {
writer.write(buffer, 0, bytesRead);
}
reader.close();
writer.close();
fs.close();
System.out.println("File downloaded successfully");
} catch (IOException e) {
e.printStackTrace();
}
}
}
在這個示例中,首先創建一個Hadoop的Configuration對象,并設置HDFS的地址。然后通過FileSystem.get方法獲取一個FileSystem對象。接著指定HDFS中的源文件路徑和本地機器上的目標文件路徑,并通過FileSystem的open方法打開源文件。通過BufferedInputStream和BufferedOutputStream來讀取和寫入文件內容,并最終將文件下載到本地機器上。
請記得替換示例中的"/path/to/source/file/in/hdfs"和"/path/to/destination/file/on/local/machine"為實際的文件路徑。此外,還要根據HDFS的配置修改"fs.defaultFS"的值。