【问题标题】:Renaming Part Files in Hadoop Map Reduce在 Hadoop Map Reduce 中重命名部分文件
【发布时间】:2015-06-17 16:41:08
【问题描述】:

我已尝试按照页面http://hadoop.apache.org/docs/mapreduce/r0.21.0/api/index.html?org/apache/hadoop/mapreduce/lib/output/MultipleOutputs.html 中的示例使用MultipleOutputs

驱动程序代码

    Configuration conf = new Configuration();
    Job job = new Job(conf, "Wordcount");
    job.setJarByClass(WordCount.class);
    job.setInputFormatClass(TextInputFormat.class);
    job.setMapperClass(WordCountMapper.class);
    job.setReducerClass(WordCountReducer.class);
    job.setMapOutputKeyClass(Text.class);
    job.setMapOutputValueClass(IntWritable.class);
    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);
    FileInputFormat.setInputPaths(job, new Path(args[0]));
    FileOutputFormat.setOutputPath(job, new Path(args[1]));
    MultipleOutputs.addNamedOutput(job, "text", TextOutputFormat.class,
            Text.class, IntWritable.class);
    System.exit(job.waitForCompletion(true) ? 0 : 1);

reducer 代码

public class WordCountReducer extends
        Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable result = new IntWritable();
    private MultipleOutputs<Text, IntWritable> mos;
    public void setup(Context context){
        mos = new MultipleOutputs<Text, IntWritable>(context);
    }
    public void reduce(Text key, Iterable<IntWritable> values, Context context)
            throws IOException, InterruptedException {
        int sum = 0;
        for (IntWritable val : values) {
            sum += val.get();
        }
        result.set(sum);
        //context.write(key, result);
        mos.write("text", key,result);
    }
    public void cleanup(Context context)  {
         try {
            mos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         }
}

发现reducer的输出重命名为text-r-00000

但这里的问题是我也得到了一个空的 part-r-00000 文件。这是 MultipleOutputs 的预期行为方式,还是我的代码有问题?请指教。

我尝试过的另一种替代方法是使用 FileSystem 类遍历我的输出文件夹并手动重命名以 part 开头的所有文件。

最好的方法是什么?

FileSystem hdfs = FileSystem.get(configuration);
FileStatus fs[] = hdfs.listStatus(new Path(outputPath));
for (FileStatus aFile : fs) {
if (aFile.isDir()) {
hdfs.delete(aFile.getPath(), true);
// delete all directories and sub-directories (if any) in the output directory
} 
else {
if (aFile.getPath().getName().contains("_"))
hdfs.delete(aFile.getPath(), true);
// delete all log files and the _SUCCESS file in the output directory
else {
hdfs.rename(aFile.getPath(), new Path(myCustomName));
}
}

【问题讨论】:

    标签: java hadoop mapreduce


    【解决方案1】:

    即使您使用的是MultipleOutputs,默认的OutputFormat(我相信它是TextOutputFormat)仍在使用,因此它会初始化并创建您所看到的这些part-r-xxxxx 文件。

    它们为空的事实是因为您没有执行任何context.write,因为您正在使用MultipleOutputs。但这并不妨碍它们在初始化期间被创建。

    要摆脱它们,您需要定义您的 OutputFormat 来表示您不期望任何输出。你可以这样做:

    job.setOutputFormat(NullOutputFormat.class);
    

    使用该属性集,这应该确保您的零件文件根本不会被初始化,但您仍然可以在MultipleOutputs 中获得输出。

    您也可以使用LazyOutputFormat,这将确保您的输出文件仅在/如果有一些数据时创建,而不是初始化空文件。你可以这样做:

    import org.apache.hadoop.mapreduce.lib.output.LazyOutputFormat; 
    LazyOutputFormat.setOutputFormatClass(job, TextOutputFormat.class);
    

    请注意,您在Reducer 中使用原型MultipleOutputs.write(String namedOutput, K key, V value),它仅使用将根据您的namedOutput 生成的默认输出路径,例如:{namedOutput}-(m|r)-{part-number}。如果你想更好地控制你的输出文件名,你应该使用原型MultipleOutputs.write(String namedOutput, K key, V value, String baseOutputPath),它可以让你在运行时根据你的键/值生成文件名。

    【讨论】:

    • @Charles:我在下面的帖子中看到了 LazyOutputFormat 的使用,但我不知道如何使用它。非常感谢您的回复。 stackoverflow.com/questions/10924852/…
    • @Charles:当我尝试使用“NullOutputFormat”时,即使 MultipleOutputs 也没有写入输出路径……这有意义吗,还是我遗漏了什么?
    【解决方案2】:

    这是您在 Driver 类中更改输出文件的基本名称所需要做的所有事情: job.getConfiguration().set("mapreduce.output.basename", "text"); 所以这将导致您的文件被称为“text-r-00000”。

    【讨论】:

      猜你喜欢
      • 2016-11-16
      • 2012-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-06
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多