要將文件上傳到HDFS,可以使用以下方法:
hadoop fs -put <local_file_path> <hdfs_directory_path>
hdfs dfs -put <local_file_path> <hdfs_directory_path>
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class UploadFileToHDFS {
public static void main(String[] args) {
String localFilePath = "path/to/local/file";
String hdfsDirectoryPath = "hdfs://localhost:9000/user/hadoop/";
Configuration conf = new Configuration();
try {
FileSystem fs = FileSystem.get(conf);
fs.copyFromLocalFile(new Path(localFilePath), new Path(hdfsDirectoryPath));
System.out.println("File uploaded to HDFS successfully");
} catch (Exception e) {
e.printStackTrace();
}
}
}
以上是常用的方法,可以根據具體的需求選擇適合自己的方法上傳文件到HDFS。