要将文件上传到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。