Mapreduce top 10问题 在线求解 求求高手,我快疯了
我写了一个简单的top 10程序,分了2个map, 2个reduce做的,第一个map reduce出来是sum的结果,第2个map reduce是用来找出top10的, 第一个mapreduce的结果是对的,结果大概是<A,1>,<B,3>,<c,1>........(<name, count>)
但是跑到第2个mapreduce的时候就出问题了。。。
进入第2个mapreduce的map,主要是把每个block的top 10找出来,然后放入一个output中,比如全部放入context.write(NullWtritable.get(), Text),Text 是name+"\t"+count的组合,然后把整个结果传入到reduce,reduce照理说应该是用String[] str = value .ToString .split("\t")分解,能把count和name分别找出来,但是很奇怪的是str[1]报错,说是outof boundary.
我把代码贴上,只是第2个mapreduce的代码,请大家帮帮忙,帮我看看。。。不甚感激!!!
public class TopMapper {
/**
* @param args
*/
public static class Map extends Mapper<Object, Text, NullWritable, Text> {
private TreeMap<Integer, Text> repToRecordMap = new TreeMap<Integer, Text>();
private Text outputValue = new Text();
@Override
protected void cleanup(Context context) throws IOException,
InterruptedException {
//Output our ten records to the reducers with a null key
for (Text t : repToRecordMap.values()) {
context.write(NullWritable.get(), t);
}
}
@Override
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String[] valueStr = value.toString().split("\t");
String visitorName = valueStr[0];
int visitorCount =Integer.parseInt(valueStr[1]);
outputValue.set(visitorName+"-"+visitorCount);
context.write(NullWritable.get(), outputValue);
// Add this record to our map with the reputation as the key
repToRecordMap.put(visitorCount, outputValue);
if (repToRecordMap.size() > 10) {
repToRecordMap.remove(repToRecordMap.firstKey());
}
}
}
public static class Reduce extends Reducer<NullWritable,Text, NullWritable,Text>{
private TreeMap<Integer, Text> repToRecordMap = new TreeMap<Integer, Text>();
private Text outputValue = new Text();
@Override
protected void reduce(NullWritable key, Iterable<Text> values,
Context context) throws IOException, InterruptedException {
for (Text value:values) {
String[] valueStr = value.toString().split("\t");
String visitorName =valueStr[0];
int visitorCount = Integer.parseInt(valueStr[1]);
repToRecordMap.put(visitorCount,new Text(visitorName));
if (repToRecordMap.size() > 10) {
repToRecordMap.remove(repToRecordMap.firstKey());
}
}
for (Text text: repToRecordMap.values()){
context.write(NullWritable.get(), text);
}
}
}
}