计算单词数量的mapreduce程序

学习阶段尝试写的第一个MapReduce程序,测试成功跑过。没有什么技术难度。记录下来,以备复习使用。

Mapper代码如下:


import java.io.IOException;

import org.apache.commons.lang.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class WordCountMapper extends
        Mapper<LongWritable, Text, Text, LongWritable> {

    @Override
    protected void map(LongWritable key, Text value, Context context)
            throws IOException, InterruptedException {
        // 获取一行数据的内容
        String line = value.toString();

        // 切分一行的内容为一个单词数组
        String[] words = StringUtils.split(line, " ");

        // 输出<word,1>
        for (String word: words) {
            context.write(new Text(word), new LongWritable(1));
        }
    }

}

Reducer代码如下:


import java.io.IOException;

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

public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {

    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context)
            throws IOException, InterruptedException {
        // 定义一个累加计数器
        long count = 0;
        for (LongWritable value: values) {
            count += value.get();
        }

        // 输出单词键值对
        context.write(key, new LongWritable(count));
    }

}

主程序代码如下:

package com.ligy.mapreduce.wordcount;

import java.io.IOException;

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;

/**
 * 用来描述一个job(使用哪个mapper类,哪个reducer类,输入文件在哪,输出结果放在哪)
 * 然后提交job给Hadoop集群
 * @author gyli
 *
 */
// com.ligy.mapreduce.wordcount.WordCountRunner
public class WordCountRunner {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();

        conf.set("mapreduce.job.jar", "wcjob.jar");

        Job wcjob = Job.getInstance(conf);

        // 设置wcjob中的资源所在的jar包所
        wcjob.setJarByClass(WordCountRunner.class);

        // wcjob 要使用哪个mapper类
        wcjob.setMapperClass(WordCountMapper.class);

        // wcjob 要使用哪个Reducer类
        wcjob.setReducerClass(WordCountReducer.class);

        // wcjob的mapper类输出的kv数据的类型
        wcjob.setMapOutputKeyClass(Text.class);
        wcjob.setMapOutputValueClass(LongWritable.class);

        // wcjob的reducer类输出的kv数据的类型
        wcjob.setOutputKeyClass(Text.class);
        wcjob.setOutputValueClass(LongWritable.class);

        // 指定要处理的原始数据所存放的路径
        FileInputFormat.setInputPaths(wcjob, "hdfs://master:9000/data/test/GenericDemo5.java"); 
//      FileInputFormat.setInputPaths(wcjob, "C:\\Users\\gyli\\Desktop\\word\\WordCountMapper.java");

        // 指定处理之后的结果输出到什么路径
        FileOutputFormat.setOutputPath(wcjob, new Path("hdfs://master:9000/data/test/output5"));
//      FileOutputFormat.setOutputPath(wcjob, new Path("C:\\Users\\gyli\\Desktop\\word\\output"));

        boolean res = wcjob.waitForCompletion(true);

        System.exit(res ? 0 : 1);

    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值