随笔 - 20  文章 - 0  评论 - 0  阅读 - 9024

mapreduce统计单词个数

 

WordCount类代码:

复制代码
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class WordCount {
    public static void main(String[] args) throws Exception {
        //定义配置对象
        Configuration conf = new Configuration();
        //定义一个工作对象
        Job job = Job.getInstance(conf);


        //获取map的一个对象
        job.setMapperClass(WordCountMap.class);
        //指定map阶段的一个输出key
        job.setMapOutputKeyClass(Text.class);
        //指定map阶段输出的values类型
        job.setMapOutputValueClass(IntWritable.class);
        //指定map阶段的输入文件
        FileInputFormat.setInputPaths(job,new Path("D:\\代码\\大二下\\Hadoop\\4、MapReduce_Reduce\\123.txt"));


        //获取reduce的类
        job.setReducerClass(WordCountReduce.class);
        //指定reduce阶段的一个输出的key
        job.setOutputKeyClass(Text.class);
        //指定reduce阶段输出的values类型
        job.setOutputValueClass(IntWritable.class);
        //指定reduce阶段的输出文件
        FileOutputFormat.setOutputPath(job,new Path("D:\\代码\\大二下\\Hadoop\\4、MapReduce_Reduce\\456.txt"));

        job.waitForCompletion(true);
    }
}
复制代码

WordMap类代码:

复制代码
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class WordCountMap extends Mapper<LongWritable, Text, Text, IntWritable> {
    @Override
    protected void map(LongWritable key, Text value,Context context) throws IOException, InterruptedException {
        //读取每行文本
        String line = value.toString();
        //splite拆分每行
        String[] words = line.split(" ");
        //取出每个单词
        for (String word:
             words) {
            //将单词转为Text类型
            Text wordText = new Text(word);
            //将1转变成IntWritable
            IntWritable outValue = new IntWritable(1);
            //写出每个单词,跟对应1
            context.write(wordText, outValue);
        }
    }
}
复制代码

WordReduce类代码:

复制代码
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
/*
*   Text          输入的字符串类型,序列化
*   IntWritable   输入一串1,序列化
*   Text          输出的字符串类型,序列化
*   IntWritable   输出的求和数组,序列化
* */
public class WordCountReduce extends Reducer<Text, IntWritable,Text,IntWritable> {
    /*
     * key      输入单词名字
     * values   输入一串1
     * context  输出的工具
     * */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int sum=0;
        for(IntWritable number:values){
            sum += number.get();

        }
        context.write(key,new IntWritable(sum));
    }
}
复制代码

 

posted on   搁浅的小鲸鱼  阅读(95)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· winform 绘制太阳,地球,月球 运作规律
· 上周热点回顾(3.3-3.9)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示