You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用MapReduce实现偏移量为-20的词频统计?

带偏移值的词频统计实现(基于Hadoop MapReduce)

需求说明:完成常规词频统计后,给每个单词的统计结果加上指定偏移值**-20**。例如单词“Alpha”在输入中出现16次,最终统计结果为 16 + (-20) = -4。

以下是实现代码及修改说明:

SumReducer.java

核心修改:在统计完单词出现总数后,加入偏移值计算再输出结果

int wordCount = 0;

for (IntWritable value : values) {
    wordCount += value.get();
}
// 加入偏移值计算
wordCount += -20;
context.write(key, new IntWritable(wordCount));
}

WordCount.java

主入口类,负责配置Job参数,无需修改

if (args.length != 2) {
  System.out.printf(
      "Usage: WordCount <input dir> <output dir>\n");
  System.exit(-1);
}

Job job = new Job();

job.setJarByClass(WordCount.class);

job.setJobName("Word Count");

FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));

job.setMapperClass(WordMapper.class);
job.setReducerClass(SumReducer.class);

job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);

boolean success = job.waitForCompletion(true);
System.exit(success ? 0 : 1);

WordMapper.java

Mapper类,负责拆分文本为单词并输出<单词, 1>键值对,无需修改

String line = value.toString();

for (String word : line.split("\\W+")) {
  if (word.length() > 0) {         
    context.write(new Text(word), new IntWritable(1));
  }
}

内容的提问来源于stack exchange,提问作者dashboard34

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:25:21