Java 未能设置KeyComparator函数
我正在尝试按值对数据进行排序 我使用的方法是将键和值组合为复合键 e、 g(键,值)->({key,value},value) 并定义我的KeyComaparator,它比较键中的值部分 我的数据是一段我应该数一数的文字 我做了两项工作,第一项是进行字数统计,但在reducer中将键组合到复合键 这就是结果 是,4Java 未能设置KeyComparator函数,java,hadoop,mapper,reducers,secondary-sort,Java,Hadoop,Mapper,Reducers,Secondary Sort,我正在尝试按值对数据进行排序 我使用的方法是将键和值组合为复合键 e、 g(键,值)->({key,value},value) 并定义我的KeyComaparator,它比较键中的值部分 我的数据是一段我应该数一数的文字 我做了两项工作,第一项是进行字数统计,但在reducer中将键组合到复合键 这就是结果 是,4 第15页,第15页 ECA,11 到,6 在第二项工作中,我尝试使用复合键按值排序 这是我的地图 public static class Map2 extends MapReduc
第15页,第15页
ECA,11
到,6 在第二项工作中,我尝试使用复合键按值排序 这是我的地图
public static class Map2 extends MapReduceBase
implements Mapper<LongWritable,Text,Text,IntWritable>{
private Text word = new Text();
public void map(LongWritable key, Text value, OutputCollector<Text,IntWritable> output, Reporter reporter) throws IOException {
String line = value.toString();
String w1[] = line.split("\t");
word.set(w1[0]);
output.collect(word,new IntWritable(Integer.valueOf(w1[1])));
}
}
这是我的减速机
public static class Reduce2 extends MapReduceBase
implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException{
int sum=0;
while( values.hasNext()){
sum+= values.next().get();
}
//String cpKey[] = key.toString().split(",");
Text outputKey = new Text();
//outputKey.set(cpKey[0]);
output.collect(key, new IntWritable(sum));
}
}
但结果是这样的
是13第32条
ECA 21
到14
. . 而且丢了很多字 但如果我不使用键盘比较器 它返回未排序的结果,就像我提到的第一个一样
有什么办法解决这个问题吗?谢谢 我不确定你在哪里出错。
但是,您尝试执行的是基于值的
二次排序。
这不是一项琐碎的工作,但您需要为分类、聚合和其他内容创建更多的类,这些都有清楚的解释和
只需按照这些博客中的说明进行操作肯定会对您有所帮助。谢谢您的帮助,但我想对所有值进行排序,而不是像第一个链接那样对它们进行分组,我还需要进行分区吗?谢谢,它行得通,但我仍然不知道为什么这两个(分区,分组)是必要的。
public static class Reduce2 extends MapReduceBase
implements Reducer<Text, IntWritable, Text, IntWritable> {
public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException{
int sum=0;
while( values.hasNext()){
sum+= values.next().get();
}
//String cpKey[] = key.toString().split(",");
Text outputKey = new Text();
//outputKey.set(cpKey[0]);
output.collect(key, new IntWritable(sum));
}
}
public static void main(String[] args) throws Exception {
int reduceTasks = 1;
int mapTasks = 3;
System.out.println("1. New JobConf...");
JobConf conf = new JobConf(WordCountV2.class);
conf.setJobName("WordCount");
System.out.println("2. Setting output key and value...");
conf.setOutputKeyClass(Text.class);
conf.setOutputValueClass(IntWritable.class);
System.out.println("3. Setting Mapper and Reducer classes...");
conf.setMapperClass(Map.class);
conf.setReducerClass(Reduce.class);
// set numbers of reducers
System.out.println("4. Setting number of reduce and map tasks...");
conf.setNumReduceTasks(reduceTasks);
conf.setNumMapTasks(mapTasks);
System.out.println("5. Setting input and output formats...");
conf.setInputFormat(TextInputFormat.class);
conf.setOutputFormat(TextOutputFormat.class);
System.out.println("6. Setting input and output paths...");
FileInputFormat.setInputPaths(conf, new Path(args[0]));
String TempDir = "temp" + Integer.toString(new Random().nextInt(1000)+1);
FileOutputFormat.setOutputPath(conf, new Path(TempDir));
//FileOutputFormat.setOutputPath(conf,new Path(args[1]));
System.out.println("7. Running job...");
JobClient.runJob(conf);
JobConf sort = new JobConf(WordCountV2.class);
sort.setJobName("sort");
sort.setMapOutputKeyClass(Text.class);
sort.setMapOutputValueClass(IntWritable.class);
sort.setOutputKeyComparatorClass(KeyComparator.class);
sort.setMapperClass(Map2.class);
sort.setReducerClass(Reduce2.class);
sort.setNumReduceTasks(reduceTasks);
sort.setNumMapTasks(mapTasks);
sort.setInputFormat(TextInputFormat.class);
sort.setOutputFormat(TextOutputFormat.class);
FileInputFormat.setInputPaths(sort,TempDir);
FileOutputFormat.setOutputPath(sort, new Path(args[1]));
JobClient.runJob(sort);
}