MapReduce词频统计的开发

1.新建包(java rg下新建一个mr包,添加三个java类

 WordCountMapper中

package org.mr;

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

import java.io.IOException;

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    @Override
    protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        //输入 <0, "Hello World">
        //输出<"Hello", 1> <"World", 1>
        String line = value.toString();  //"Hello World"
        String[] words = line.split(" "); //"Hello" , "World"
        for(String word: words) {
            context.write(new Text(word), new IntWritable(1) ); //输出
        }
    }
}

        WordCountReducer

package org.mr;

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

import java.io.IOException;

public class WordCountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
    //输入:<"Hello", (1,1,1)>
    //输出:<"Hello", 3>

    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        int count = 0; //求迭代器里的每个数字之和
        for(IntWritable i: values) {
            count += i.get();
        }
        context.write(key, new IntWritable(count));
    }
}

WordCountDriver

package org.mr;

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;

import java.io.IOException;

public class WordCountDriver {
    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {
        Configuration conf = new Configuration();
        conf.set("mapreduce.framework.name", "local");
        Job job = Job.getInstance(conf);
        job.setJarByClass(WordCountDriver.class);

        job.setMapperClass(WordCountMapper.class); //设置Mapper类
        job.setReducerClass(WordCountReducer.class);//设置Reducer类
        job.setMapOutputKeyClass(Text.class);   //设置Map的输出的键的类型
        job.setMapOutputValueClass(IntWritable.class);//设置Map的输出的值的类型
        job.setOutputKeyClass(Text.class); //设置Reduce的输出的键的类型
        job.setOutputValueClass(IntWritable.class); //设置Reduce的输出的值的类型

        FileInputFormat.setInputPaths(job, new Path("/wordcount/input")); //设置输入目录
        FileOutputFormat.setOutputPath(job, new Path("/wordcount/output")); //设置输出目录

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

    }
}

3.打包

4.上传

5..启动hadoop集群

6.准备一个用于测试hadoop词频统计的文本文档,将这个上传。

vi wordcount_test.txt
hdfs dfs -mkdir -p /wordcount/input
dfs dfs -put wordcount_test.txt /wordcount/input
hadoop jar HadoopDemo-1.0-SNAPSHOT.jar org.mr.WordCountDriver

7.命令查看结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值