为什么“set”只有一个元素,而例如前 5 行输入应该有 4 个元素,这些元素具有相同的 URL 和四个不同的 IP。我还使用了“for-each”而不是“迭代器”,但不起作用。有人能帮我吗?
映射器
public class WordCount {
public static class TokenizerMapper extends Mapper<Object, Text, Text, Text> {
private Text IP = new Text();
private Text word = new Text();
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
String line = value.toString();
String[] tokens = line.split(",");
word.set(tokens[2]);
IP.set(tokens[0]);
context.write(word, IP);
}
}
减速器
public static class IntSumReducer extends Reducer<Text, Text, Text, Text> {
public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
Set<String> set = new HashSet<String>();
Iterator<Text> iterator = values.iterator();
while (iterator.hasNext()) {
set.add(iterator.next().toString());
}
int a = set.size();
String str = String.format("%d", a);
context.write(key, new Text(str));
}
}
工作
public static void main(String[] args) throws Exception {
Job job = new Job();
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
森林海
相关分类