资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

MapReduce单词统计

WordcountMapper类

package com.sky.mr.wordcount;
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 org.junit.Test;

import java.io.IOException;

public class WordcountMapper extends Mapper {
    //由于每读一行文本数据,就要调用一次map方法,为了避免多次创建对象,浪费内存资源,将Text,IntWritable对象创建在
    //map方法之外
   Text k = new Text();
   IntWritable v  = new IntWritable(1);
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //获取每一行的文本内容
        String line = value.toString();
        //按空格分割
        String[] words = line.split(" ");

        //转换数据格式,输出
        for ( String word: words) {
            k.set(word);
            context.write(k, v);
        }
    }
}

WordcountReducer类

package com.sky.mr.wordcount;

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 {
    IntWritable v  = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
        //求每组相同key的总个数
        int sum = 0;
        for ( IntWritable count:values) {
            sum += count.get();
        }
        //输出
        v.set(sum);
        context.write(key, v);
    }
}

WordcountDriver类

package com.sky.mr.wordcount;

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, ClassNotFoundException, InterruptedException {
        //1、获取配置信息以及job对象
        Configuration conf = new Configuration();
        Job job = Job.getInstance(conf);
        //2、设置jar包路径
        job.setJarByClass(WordcountDriver.class);
        //3、关联自定义mapper和reducer类
        job.setMapperClass(WordcountMapper.class);
        job.setReducerClass(WordcountReducer.class);
        //4、设置Map输出key和value类型
         job.setMapOutputKeyClass(Text.class);
         job.setMapOutputValueClass(IntWritable.class);
        //5、设置最终结果key,value类型
         job.setOutputKeyClass(Text.class);
         job.setOutputValueClass(IntWritable.class);
        //6、设置文件输入输出路径
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));
        //7、将封装了MapReduce程序运行参数的job对象,提交到Yarn集群
        boolean result = job.waitForCompletion(true);
        System.exit(result?0:1);
    }
}

输入文件

import org apache hadoop io
import org apache hadoop io
import org apache hadoop
import java io IOException

创新互联致力于互联网网站建设与网站营销,提供成都做网站、成都网站设计、网站开发、seo优化、网站排名、互联网营销、重庆小程序开发、公众号商城、等建站开发,创新互联网站建设策划专家,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。

输出文件

IOException 1
apache 3
hadoop 3
import 4
io 3
java 1
org 3


新闻名称:MapReduce单词统计
网页网址:http://cdkjz.cn/article/gdgeho.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220