在执行之前在java代码中设置hadoop输出文件夹复制

0md85ypi  于 2021-05-30  发布在  Hadoop
关注(0)|答案(1)|浏览(269)

我有一个hadoop作业,我想将输出文件夹的复制号设置为1,我想在java代码中这样做。我们服务器上的默认值是3。另一个导入方面是在写入输出之前设置复制编号。这意味着我不想用3个副本来编写整个输出,然后将其减少到1。我希望在它开始写入输出文件夹之前,它被设置为只有一个复制。这样做的原因是输出可能相当大,我想腾出一些空间。

@Override
public int run(String[] args) throws Exception {

    /**Get configuration */
    Configuration conf = getConf();
    conf.setStrings("args", args);

    /**Job configuration */
    Job job = Job.getInstance(conf, "HadoopSearch");
    job.setJarByClass(Search.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(NullWritable.class);

    /**Set Mapper and Reducer, use identity reducer*/
    job.setMapperClass(Map.class);
    job.setReducerClass(Reducer.class); // identity

    /**Set input and output formats */
    job.setInputFormatClass(TextInputFormat.class);
    job.setOutputFormatClass(TextOutputFormat.class);

    /**Set input and output path */
    FileInputFormat.addInputPath(job, new Path("IN PATH"));
    FileOutputFormat.setOutputPath(job, new Path("OUT PATH"));

    job.waitForCompletion(true);
    return 0;
}

我知道我可以用 FileSystem.setReplication(Path p, short s) 但这只适用于每个文件,我希望它设置为整个文件夹。我可以循环通过文件夹内的文件,但更重要的是,这似乎只有在工作完成后,文件已经存在的工作。我假设复制的进程已经在运行,我可能会遇到磁盘空间方面的问题,这是我想要避免的。

au9on6nz

au9on6nz1#

在mapreduce中,可以使用“设置作业配置”设置dfs.replication属性,以便在该作业中创建的文件具有指定的复制因子。希望这会有帮助。

Configuration conf = new Configuration();
    conf.set("dfs.replication", "1");
    Job job = new Job(conf);

相关问题