Hadoop MapReduce 基础单词个数统计操作

package mapreduce;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.VLongWritable;
import org.apache.hadoop.mapreduce.Job;

import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
import java.util.Map;

public class WordCount {

    static {
        System.setProperty("hadoop.home.dir","D:\\soft\\hadoop\\hadoop-2.9.2");
    }

    public static class MyMapper extends Mapper<LongWritable, Text,Text, LongWritable> {
        /**
         * 
         * @param key 行索引
         * @param value 每行数据
         * @param context mr中的上下文环境
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            //1.将每行的数据拆分为数组
            String line = value.toString();
            String[] words = line.split(" ");
            //2.将各个单词映射成一个键值对的方式<k,v>
            for (String word : words
            ) {
                //3.写到内存缓冲区
                context.write(new Text(word), new LongWritable(1));

            }
        }
    }


    public static class MyReducer extends Reducer<Text,LongWritable,Text,LongWritable>{

        /**
         * @param key 每个单词
         * @param values 单词个数的集合
         * @param context 上下文环境
         * @throws IOException
         * @throws InterruptedException
         */
        @Override
        protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {

            Long sum=0L;
            //1.遍历指定key单词对用的个数集合[1,1,1]
            for (LongWritable value: values
                 ) {
                //2.累加个数
                sum += value.get();
            }
            //3.输出
            context.write(key,new LongWritable(sum));
        }
    }


    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //0.初始化一个job
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf, "word-count");
            /*
                打jar包集群方式运行
                job.setJarByClass(WordCount.class);
             */

        //1.输入文件
        FileInputFormat.addInputPath(job, new Path(args[0]));
        //2.map并行计算
            //如果map的输出key value 的类型个reduce key  value的类型相同可以省略
        job.setMapperClass(MyMapper.class);
//        job.setMapOutputKeyClass(Text.class);
//        job.setMapOutputValueClass(LongWritable.class);
        //3.shuffle流程(内部实现)

        //4.reduce计算
        job.setNumReduceTasks(2);
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        //5.输出文件
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //6.提交作业(总入口)
        boolean result = job.waitForCompletion(true);
        System.out.println(result ? 1 : 0);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值