数字的输出格式未按java mapreduce程序排序
减速机的数字输出以字典顺序给出输出,如19后显示2,29后显示3,但应按数字顺序。请帮助解决这个问题数字的输出格式未按java mapreduce程序排序,java,mapreduce,Java,Mapreduce,减速机的数字输出以字典顺序给出输出,如19后显示2,29后显示3,但应按数字顺序。请帮助解决这个问题 Mapper code: public class SPMapper extends Mapper<LongWritable, Text, Text, IntWritable> { public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedExce
Mapper code:
public class SPMapper extends Mapper<LongWritable, Text, Text, IntWritable>
{
public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException
{
//1 1.232
//1 1.45
String s = value.toString();
String[] pat = s.split(" ");
String patent = pat[0];
context.write(new Text(patent),new IntWritable(1));
context.write(new Text(patent.),IntWritable(1));
//1, 1
//1, 1
}
}
Reducer code:
public class SPReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
protected void reduce(Text key, Iterable<IntWritable>
values, Reducer<Text, IntWritable, Text, IntWritable>.Context arg2) throws IOException, InterruptedException {
Integer y=0;
//1, {1,1,1,1,1}
for(IntWritable x: values)
{
y=y+ Integer.parseInt(x.toString());
}
arg2.write(key,new IntWritable(y));
}
}
但实际输出应为
(number,count)
0 1
1 3
2 4
.
.
.
11 3
12 5
树形图可以按键将内容按顺序排列
Map<Text, Integer> sorted = new TreeMap<>();
sorted.put(key,new IntWritable(y));
Map sorted=newtreemap();
排序后的.put(key,新的intwriteable(y));
获取输出
for (Map.Entry<Text, Integer> entry : sorted.entrySet())
System.out.println("number:" + entry.getKey() +
", count:" + entry.getValue());
for(Map.Entry:sorted.entrySet())
System.out.println(“编号:+entry.getKey()+
,计数:“+entry.getValue());
请参见页面。。。。如果没有代码,您如何期望答案?排序的唯一方法是对字符串而不是数字进行排序。随附代码,请提供帮助,提前感谢每个减速机按排序顺序输出键。如果你想对所有输出进行排序,你应该用一个减速机来运行你的作业
for (Map.Entry<Text, Integer> entry : sorted.entrySet())
System.out.println("number:" + entry.getKey() +
", count:" + entry.getValue());