MapReduce入门之单词统计(练习)

//本地运行和远程调用需要
/* static {
System.setProperty(“hadoop.home.dir”, “E:\x3\hadoop-2.9.2”);
}*/

public  static class MyMapper extends Mapper<LongWritable,Text,Text,LongWritable>{

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

       }

    }
}


public static class MyReduce 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;
        //①遍历指定key单词对应的个数集合[1,1,1]
        for (LongWritable value : values){
            //累加个数
            sum += value.get();
        }
        //输出
        context.write(key,new LongWritable(sum));
    }
}

public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
    //初始化一个job
    Configuration conf = new Configuration();
    Job job = Job.getInstance(conf, "word-count");

    /*************************3.打jar集群方式start******************************/
    job.setJarByClass(WordCount.class);
    /*************************3.打jar集群方式start******************************/
    //1.输入文件
    //输入文件需要这个类 切片
    FileInputFormat.addInputPath(job ,new Path(args[0]));
    //2.map并行计算
    job.setMapperClass(MyMapper.class);
    //如果map输出的k和v类型  与 reduce输出的k和v类型一致的话,可省略
    //job.setMapOutputKeyClass(Text.class);
    //job.setMapOutputValueClass(LongWritable.class);
    //3.shuffle流程

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

    // 6.提交作业
    boolean result = job.waitForCompletion(true);
    System.out.println(result);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值