WordCount案例-本地模式下源码

WordCountMapper

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, LongWritable> {
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //获取传入的每一行内容
        String line = value.toString();
        //按照数据分隔符切割
        String[] words = line.split(" ");
        //遍历单词数组,出现单词就标记为1
        for (String word : words) {
            context.write(new Text(word), new LongWritable(1));
        }
    }
}

WordCountReducer

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


public class WordCountReducer extends Reducer<Text,LongWritable,Text,LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException {
        //声明一个变量
        int count=0;
        for (LongWritable value: values) {
            count +=value.get();
        }
        context.write(key, new LongWritable(count));
    }
}

WordCountRunner

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;

public class WordCountRunner {
    public static void main(String[] args) throws Exception {
        // 创建本次mr程序的job实例
        Configuration conf = new Configuration();
        // conf.set("mapreduce.framework.name", "local");
        Job job = Job.getInstance(conf);
        // 指定本次job运行的主类
        job.setJarByClass(WordCountRunner.class);
        // 指定本次job的具体mapper reducer实现类
        job.setMapperClass(WordCountMapper.class);
        job.setReducerClass(WordCountReducer.class);
        // 指定本次job map阶段的输出数据类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        // 指定本次job reduce阶段的输出数据类型 也就是整个mr任务的最终输出类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);
        // 指定本次job待处理数据的目录 和程序执行完输出结果存放的目录
        long startTime=System.currentTimeMillis();   //获取开始时间
        //FileInputFormat.setMaxInputSplitSize(job, 128);
        //FileInputFormat.setMinInputSplitSize(job, 128);
        FileInputFormat.setInputPaths(job,"E:\\wordcount\\input\\words.txt");
        FileOutputFormat.setOutputPath(job, new Path("E:\\wordcount\\output"));
        // 提交本次job
        boolean b = job.waitForCompletion(true);
        long endTime=System.currentTimeMillis(); //获取结束时间
        System.out.println("程序运行时间: "+(endTime-startTime)+"ms");
        System.exit(b ? 0 : 1);

    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45492179/article/details/103043966