美文网首页
2022-03-08

2022-03-08

作者: 眼君 | 来源:发表于2022-03-08 17:40 被阅读0次
package com.bigdata.wordcount;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;


public class WCMapper extends Mapper<LongWritable,Text,Text,LongWritable>{
    final LongWritable one = new LongWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] words = line.split("\t");
        for (String w:words) {
            context.write(new Text(w),one);
        }   
    }
}

public class WCReducer extends Reducer<Text,LongWritable,Text,LongWritable>{
    @Override
    protected void reduce(Text key, Iterable<LongWritable> values,Context context) throws IOException, InterruptedException {
        int valueout = 0;
        for(LongWritable value:values) {
            valueout += value.get();
        }
        context.write(key, new LongWritable(valueout));
    }
}

public class Driver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        job.setJarByClass(Driver.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.addInputPath(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        job.waitForCompletion(true);
    }

相关文章

  • JavaScript:获取数组前N位元素

    2022-03-08 周二

  • 橙子的ScalersTalk第六轮新概念朗读持续力训练Day 1

    练习材料: [Day 2721 2022-03-08] Lesson 25-1 The Cutty Sark On...

  • 2022-03-08

    2022-03-08坚持分享第1254天 读《建构解决之道》P263-267 感悟:灵活运用关系问句及评论问句,以...

  • 女神节的男同胞们

    2022-03-08 晴 周二 今天是半边天的节日。 昨天,工作群里说,早上上班的时候,单位领导会在...

  • 2022-03-08 心态需要改变

    2022-03-08 女生节,下午放半天假,我开完会后,大概3点半左右 出去溜达。感受最深刻的就是 自由职业真好,...

  • 0127行动|子弹笔记

    2022-03-08 北京 晴天 女神节快乐今天日出和日落都很美,特别是傍晚夕阳真的太有能量了。了解了慈济环保,北...

  • 生活里面最好的教育是智慧

    生活里面最好的教养是智慧 吉祥如意幸福久久说 2022-03-08 文/吉祥如意幸福久久 生活俗话常常在说,枪打出...

  • 时间该花在值得人和事上

    幸福日志2022-03-08 周二 晴 现在对于我,时间越来越重要,几乎时间就是金钱,对于如此珍贵的时间,我只愿意...

  • 2022-03-08

    07:45 - 08:25昨日回顾 ——【出门反思】此前起床习惯性经过一周末全部被打乱,但这也给我新的思考,把这些...

  • 2022-03-08

    近期收藏的高质杂句 1.有话就说 不要憋着 爱你的人没有读心术 2.有些事心里清楚就好 不必去验证 你知道的 问清...

网友评论

      本文标题:2022-03-08

      本文链接:https://www.haomeiwen.com/subject/hgwarrtx.html