使用MapReduce运行单词统计案例 | 您所在的位置:网站首页 › 画出使用mapreduce来对英语句子 › 使用MapReduce运行单词统计案例 |
import lombok.SneakyThrows; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Mapper; /** * wordCountMapper * * @author * @description: * @time: 2021/3/24 15:07 */ public class WordCountMapper extends Mapper { /** * 输出key */ private Text out_key = new Text(); /** * 输出value * 每个单词出现一次记为1 */ private IntWritable out_value = new IntWritable(1); @SneakyThrows @Override protected void map(LongWritable key, Text value, Mapper.Context context) { /** * value为一行数据转成字符串 * hello world text 第一行数据 * 根据"\t"空格分隔 */ String[] words = value.toString().split("\t"); for (String word : words) { out_key.set(word); /** * 写出数据(单词,1) */ context.write(out_key, out_value); } } } |
CopyRight 2018-2019 实验室设备网 版权所有 |