java - 在 Hadoop Map Reduce 中重命名部分文件

标签 java hadoop mapreduce

我已尝试按照页面 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));
}
}

最佳答案

即使你使用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 中使用原型(prototype) MultipleOutputs.write(String namedOutput, K key, V value),它只使用默认输出路径根据您的 namedOutput 生成类似于:{namedOutput}-(m|r)-{part-number}。如果你想对你的输出文件名有更多的控制,你应该使用原型(prototype) MultipleOutputs.write(String namedOutput, K key, V value, String baseOutputPath) 它可以让你获得在运行时生成的文件名基于您的键/值。

关于java - 在 Hadoop Map Reduce 中重命名部分文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14555313/

相关文章:

java - GestureDetector.OnGestureListener 与 GestureDetector.SimpleOnGestureListener 的目的是什么?

java - SignalR 调用已取消

java - Java 中的模式匹配

java - 最高温度Mapreduce Java代码中的运行时错误

java - 在maven中编译问题

hadoop - 有什么不同的方法来安装cloudera hadoop软件包?

hadoop - Hadoop Map Reduce 和 Google Map Reduce 之间的区别

oracle - Sqoop 使用钱包导入

python - MapReduce Amazon Python 获取输入文件的行号

hadoop - 跳过mapreduce中具有特定值(value)的记录