MR案例之WordCount

MR案例之WordCount

1、在Eclipse上编写程序

1.1、导入hadoop的jar包



1.2、项目结构

WordCountMapper.java

package com.matrix.mr;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
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, IntWritable> {

    // 循环调用的方法,从split中每一行调用一次
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String[] words = value.toString().split(" ");
        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            context.write(new Text(word), new IntWritable(1));
        }
    }

}

WordCountReduce.java

package com.matrix.mr;

import java.io.IOException;

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

public class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {

    // reduce也是循环调用,每一组调用一次
    @Override
    protected void reduce(Text arg0, Iterable<IntWritable> arg1, Context arg2)
            throws IOException, InterruptedException {
        // 根据键进行分组,
        int sum = 0;

        for (IntWritable i : arg1) {
            sum = sum + i.get();
        }
        arg2.write(arg0, new IntWritable(sum));
    }

}

RunJob.java

package com.matrix.mr;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
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 RunJob {

    public static void main(String[] args) {
        // 读取配置文件
        Configuration conf = new Configuration();
        try {

            FileSystem fs = FileSystem.get(conf);

            Job job = Job.getInstance(conf);
            // 设置用户名称
            job.setJobName("wc");

            // 设置任务的入口类
            job.setJarByClass(RunJob.class);
            job.setMapperClass(WordCountMapper.class);
            job.setReducerClass(WordCountReduce.class);

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

            // 设置输入数据目录
            FileInputFormat.addInputPath(job, new Path("/usr/matrix/input/wc"));

            // 针对目录进行判断
            Path outdir = new Path("/usr/matrix/output/wc");
            if (fs.exists(outdir)) {
                fs.delete(outdir, true);
            }

            // 设置输出数据目录
            // path 一个目录 而且不能存在
            FileOutputFormat.setOutputPath(job, outdir);
            boolean f = job.waitForCompletion(true);

            if (f) {
                System.out.println("WordCount程序运行成功!");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

wc.java

hadoop hello world
hello hive
hadoop world
hello hbase

2、导出jar包

3、将jar包上传至node1主机/opt/modules/hadoop-2.5.1目录下

4、用命令运行jar包

[root@node1 hadoop-2.5.1]# hadoop jar wc.jar com.matrix.mr.RunJob


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值