hadoop上传文件到hdfs的方法是什么

2024-04-22

要将文件上传到HDFS,可以使用以下方法:

  1. 使用hadoop fs命令:
hadoop fs -put <local_file_path> <hdfs_directory_path>
  1. 使用hdfs dfs命令:
hdfs dfs -put <local_file_path> <hdfs_directory_path>
  1. 使用Java API编程:
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。