Hadoop学习笔记(二)MapReduce Job

Hadoop学习笔记(三)MapReduce Job

MapReduce是分布式计算框架。要实现怎样的计算统计功能,可在Mapper类与Reduce类中自行覆写定义。

Map

public class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
    @Override
    protected void map(LongWritable key, Text value, Context context)
    throws IOException, InterruptedException {

      /*  
        String line = value.toString();
        String[] words = line.split(" ");
        for(String word:words){
        context.write(new Text(word), new IntWritable(1));
        */
        }
    }
}

 

Reduce

如果reduce任务读取到map任务处理结果是这样的:

(good,1)(good,1)(good,1)(good,1)

当传给reduce方法时,就变为:

key:good

value:(1,1,1,1)

(相同的key会整合处理)

 

import java.io.IOException;
import java.util.Iterator;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

public class WordcountReducer extends Reducer<Text, IntWritable, Text, IntWritable>{
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values,Context context) throws IOException, InterruptedException {
   /*
    int count = 0;
    Iterator<IntWritable> iterator = values.iterator();
    while(iterator.hasNext()){
        IntWritable value = iterator.next();
        count += value.get();
        }
      context.write(key, new IntWritable(count));
    }
    */
}

Job

package cn.edu360.mr.wc;

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 JobSubmitterLinuxToYarn {

    public static void main(String[] args) throws Exception {

        Configuration conf = new Configuration();
        conf.set("fs.defaultFS", "hdfs://hdp-01:9000");//指定默认文件系统
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");

        Job job = Job.getInstance(conf);

        job.setJarByClass(JobSubmitterLinuxToYarn.class);//当前驱动类
        job.setMapperClass(WordcountMapper.class);//设置mapper类
        job.setReducerClass(WordcountReducer.class);//设置 Reduce类

        job.setMapOutputKeyClass(Text.class);//设置map输出的key,value类型
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);/设置reduce输出的key,value类型
        job.setOutputValueClass(IntWritable.class);

        FileInputFormat.setInputPaths(job, new Path("/wordcount/input"));//待计算统计的文件所在路径
        FileOutputFormat.setOutputPath(job, new Path("/wordcount/output"));//计算统计的结果存储的文件路径

        job.setNumReduceTasks(3);

        boolean res = job.waitForCompletion(true);
        System.exit(res?0:1);

    }
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

乐乐Gold

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

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

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

打赏作者

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

抵扣说明:

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

余额充值