当前位置: 代码迷 >> java >> Hadoop:如何将HDFS文件从一个目录移动到另一目录?
  详细解决方案

Hadoop:如何将HDFS文件从一个目录移动到另一目录?

热度:45   发布时间:2023-08-04 09:18:53.0

我在HDFS中有一个HDFS源目录和一个目标存档目录。 在每次运行作业的开始,我需要将Source目录中存在的所有零件文件移动(或复制,然后删除)到我的Archive目录中。

SparkSession spark = SparkSession.builder().getOrCreate();
JavaSparkContext jsc = new JavaSparkContext(spark.sparkContext());
String hdfsSrcDir = "hdfs://clusterName/my/source";
String archiveDir = "hdfs://clusterName/my/archive";
try{
    FileSystem fs = FileSystem.get(new URI(hdfsSrcDir ),jsc.hadoopConfiguration());
}

我不知道该如何进一步。 目前,我的fs对象仅引用我的目录。
我相信,创建具有存档位置的fs2不会有所帮助。

我发现了有关FileSystem.rename() ,但这需要使用文件名作为参数。 我需要将/my/source/*移至/my/archive/

检查这是否适合您,

Configuration configuration = new Configuration(); 
configuration.set("fs.defaultFS", "hdfs://xyz:1234"); 
FileSystem filesystem = FileSystem.get(configuration); 
FileUtil.copy(filesystem, new Path("src/path"), 
              filesystem, new Path("dst/path"), false, configuration); 
filesystem.delete(new Path("src/path"), true);
  相关解决方案