2

我有一个 mapreduce 程序,它的输出现在都在文本文件中。该程序的示例如下。我不明白该怎么做是以序列文件格式从减速器输出键/值对。 不,我不能使用 SequeceFileFormat 说明符,因为我使用的是 hadoop 0.20 库

那我该怎么办?下面是一个示例 wordcount 程序只是我的较大程序的一小部分。如果我知道如何用一个来做,我可以用剩下的来做。请帮忙。 字数减少器

public void reduce(Text key, Iterable<IntWritable> values, Context context) 
  throws IOException, InterruptedException 
  {
    int sum = 0;
    for (IntWritable val : values) {
        sum += val.get();
    }
    System.out.println("reducer.output: "+key.toString()+" "+sum);

    context.write(key, new IntWritable(sum)); **//RIGHT HERE!! OUTPUTS TO TEXT**

}

}

现在这是运行它的主程序(我省略了映射器和其他不相关的细节)

Configuration conf = new Configuration();

Job job = new Job(conf, "Terms");
job.setJarByClass(wordCount.class);

//Outputting key value pairs as a dictionary (rememb python)
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

//Setting the mapper and reducer classes
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);


//Setting the type of input format. In this case, plain TEXT
job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class);

我知道如何将文本文件转换为序列文件。我知道如何反其道而行之。这不是这里的问题。我找不到任何在 hadoop 程序中实际执行此操作的示例,这就是我被卡住的原因。

所以我想要的输出是让这个程序在序列文件而不是文本文件中写入键/值对

我还想知道如何使用 Mapper 读取序列文件

任何帮助将不胜感激。

4

1 回答 1

1

我相信改变输入和输出格式就足够了。一旦正确编码/解码,键/值对应该是相同的。所以使用:

import org.apache.hadoop.mapreduce.lib.input.SequenceFileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.SequenceFileOutputFormat;

&

job.setInputFormatClass(SequenceFileInputFormat.class);
job.setOutputFormatClass(SequenceFileOutputFormat.class);

试试看,因为我有一段时间没有这样做了......

于 2014-04-10T17:12:30.943 回答