热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

mapreduce中最简单的wordcount程序

1.首先建一个WCMapper类:packagecom.zhichao.wan.mr.wordcount1;importjava.io.IOException;import

1.首先建一个WCMapper 类:

package com.zhichao.wan.mr.wordcount1;

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 WCMapper extends Mapper<LongWritable, Text, Text, LongWritable>{

    @Override
    protected void map(LongWritable key, Text value,Context context)
            throws IOException, InterruptedException {
        //1.获取每一行的字符串
        String sting = value.toString();

        //2.开始切割字符串,成为一个个的单词
        String[] words = StringUtils.split(sting, " ");

        for (String word : words) {

            context.write(new Text(word), new LongWritable(1));
        }

    }

}

2.然后建一个WCReducer 类:

package com.zhichao.wan.mr.wordcount1;

import java.io.IOException;

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

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

    @Override
    protected void reduce(Text key, Iterable values,Context context)
            throws IOException, InterruptedException {

        long count=0;

        for (LongWritable value : values) {

            count+=value.get();
        }

        context.write(key, new LongWritable(count));



    }

}

3.最后

package com.zhichao.wan.mr.wordcount1;

import java.io.IOException;

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;

public class WCRunner {

    /** * @param args * @throws Exception */
    public static void main(String[] args) throws Exception {

        Job job=Job.getInstance();

        job.setJarByClass(WCRunner.class);

        job.setMapperClass(WCMapper.class);
        job.setReducerClass(WCReducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        FileInputFormat.setInputPaths(job, new Path("/wc/srcdata/"));

        FileOutputFormat.setOutputPath(job, new Path("/wc/output"));

        job.waitForCompletion(true);

    }

}

5.将程序打成jar包,并拷贝到linux中。

6.建一个测试文件
这里写图片描述

7.运行jar包,指定主程序的地址
这里写图片描述

8.运行结果:
这里写图片描述


推荐阅读
author-avatar
谢绝沟通
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有