写了一个普通的WordCount在运行时发现有多个不同key输入后,在reducer中加入一个控制台显示语句,该语句只会输出一次,但是如果只执行了一次reduce方法key是如何实现遍历的?
map和reduce方法
private static class MyMapper extends Mapper<LongWritable, Text, MedcineWritable, NullWritable>{
static MedcineWritable outkey = new MedcineWritable();;
static NullWritable outval = NullWritable.get();
static String[] tmp ;
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
tmp = value.toString().split(" ");
context.getCounter("Mapper_line","Total_line").increment(1);
try {
outkey.setDate(tmp[0]);
outkey.setCid((tmp[1]));
outkey.setSid(Integer.parseInt(tmp[2]));
outkey.setName(tmp[3]);
outkey.setNumber(Double.parseDouble(tmp[4]));
outkey.setExceptprice(Double.parseDouble(tmp[5]));
outkey.setRealprice(Double.parseDouble(tmp[6]));
context.getCounter("Mapper_line","good_line").increment(1);
System.out.println(outkey);
context.write(outkey, outval);
}
catch (ArrayIndexOutOfBoundsException e){
context.getCounter("Mapper_line","bad_line").increment(1);
}catch (ClassFormatError e){
context.getCounter("Mapper_line","bad_line").increment(1);
}
}
}
private static class MyReducer extends Reducer<MedcineWritable,NullWritable,MedcineWritable,NullWritable>{
@Override
protected void reduce(MedcineWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {
System.out.println("上面是遍历map的输出,下面是reduce的输出");
System.out.println(key);
}
}