共計 1481 個字符,預計需要花費 4 分鐘才能閱讀完成。
要使用 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" 的值。
丸趣 TV 網 – 提供最優質的資源集合!
正文完