源代码如下:
public class WordCount {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
/*
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
*/
String delimeter = " ";
String[] temp;
String token = value.toString();
temp = token.split(delimeter);
for (int i = 0; i < temp.length; i++) {
for(int j=0;j < temp.length;j++){
word.set(temp[i]+","+temp[j]);
context.write(word, one);
}
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
为了我们的目的,我重写了wordcountv1.0版本
关注Map功能
思考:如果输入数据 a b c d
我把它们撕成碎片 a ,b ,c ,d
单词对齐的结果是: aa,ab ,ac ,ad,ba,bb,bc,bd ....
数一个字
context.write(word, one);
然后把它扔到减量计算中
但它不起作用,我应该如何更改我的代码??
1条答案
按热度按时间nbysray51#
只需声明变量单词
private Text word;
而不是并将for循环更改为below:-