我有一个 Mapper 类,它提供一个文本键和 IntWritable
值,可以是 1 2 或 3。根据这些值,我必须用不同的 key 编写三个不同的文件。我得到一个没有记录的单一文件输出。
另外,是否有任何好的多输出示例(带解释)可以指导我?
我的驱动类有这样的代码:
MultipleOutputs.addNamedOutput(job, "name", TextOutputFormat.class, Text.class, IntWritable.class);
MultipleOutputs.addNamedOutput(job, "attributes", TextOutputFormat.class, Text.class, IntWritable.class);
MultipleOutputs.addNamedOutput(job, "others", TextOutputFormat.class, Text.class, IntWritable.class);
我的 reducer 类是:
public static class Reduce extends Reducer<Text, IntWritable, Text, NullWritable> {
private MultipleOutputs mos;
public void setup(Context context) {
mos = new MultipleOutputs(context);
}
public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
int sum = 0;
String CheckKey = values.toString();
if("1".equals(CheckKey)) {
mos.write("name", key, new IntWritable(1));
}
else if("2".equals(CheckKey)) {
mos.write("attributes", key, new IntWritable(2));
}
else if("3".equals(CheckKey)) {
mos.write("others", key,new IntWritable(3));
}
/* for (IntWritable val : values) {
sum += val.get();
}*/
//context.write(key, null);
}
@Override
public void cleanup(Context context) throws IOException, InterruptedException {
mos.close();
}
}
P.S 我是 HADOOP/MAP-Reduce 编程的新手。
最佳答案
ArrayList<Integer> l = new ArrayList<Integer>();
l.add(1);
System.out.println(l.toString());
结果是 "[1]"而不是 1 所以
values.toString()
不会给“1”
除此之外,我只是尝试打印一个 Iterable,它只是提供了一个引用,所以这绝对是你的问题。如果您想遍历这些值,请按照以下示例进行操作:
Iterator<Text> valueIterator = values.iterator();
while (valueIterator.hasNext()){
}
请注意,您只能迭代一次!
关于hadoop - 为什么 MultipleOutputs 不适用于此 Map Reduce 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19178420/