编写我的第一个MapReducer程序

环境 :

   hadoop集群

 

1、编写Mapper类

package com.xue.mapreducer;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author 薛向毅
 * @create 2018-09-01 19:33
 **/
public class WordCountMapper extends Mapper<LongWritable,Text,Text,LongWritable> {

    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {

        /*
        * key : 输入的key
        * value: 数据
        * context: map上下文
        * */

        String  data = value.toString();
        //分词
        String[] words = data.split(" ");
        //输出每个单词
        for (String word : words) {
            context.write(new Text(word),new LongWritable(1));
        }


    }
}

2、编写Reducer类

package com.xue.mapreducer;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.mapreduce.Reducer;

import javax.xml.soap.Text;
import java.io.IOException;

/**
 * @author 薛向毅
 * @create 2018-09-01 19:30
 **/
public class WordCountReducer extends Reducer<Text,LongWritable,Text,LongWritable>{

    protected void reduce(Text k3, Iterable<LongWritable> v3, Context context) throws IOException, InterruptedException {
        //v3 是一个集合。 每个元素就是v2.
        long total = 0;
        for (LongWritable l : v3) {
            total+=l.get();
        }
        context.write(k3,new LongWritable(total));
    }
}

3、编写启动类

package com.xue.mapreducer;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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;

import java.io.IOException;

/**
 * @author 薛向毅
 * @create 2018-09-01 19:44
 **/
public class WordCountMain {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //创建一个job = map + reduce
        Configuration conf = new Configuration();

        //创建一个job
        Job job = Job.getInstance(conf);
        //指定程序的入口
        job.setJarByClass(WordCountMain.class);


        //指定job的mapper 和 reducer
        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setReducerClass(WordCountReducer.class);
        job.setOutputValueClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //指定任务的输入和输出
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        //提交任务
        job.waitForCompletion(true);


    }
}

4、打jar包运行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SpringCloud1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值