WordCount案例练习

源数据

hadoop hadoop
ss ss
cls cls
jiao
banzhang
xue

1、WordCountMapper.java

package com.fjh.mr;

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> {

    private Text text = new Text();
    private LongWritable i = new LongWritable(1);

    protected void map(LongWritable key,Text value,Context context) throws IOException, InterruptedException {

        //将hadoop 内置的数据类型Text 转换为String类型
        String line = value.toString();
        //对字符串进行切分
        String[] arr = line.split(" ");
        //遍历切分好的数据
        for (String str : arr) {
            text.set(str);
            context.write(text,i);
        }
    }
}

2、WordCountReducer.java

package com.fjh.mr;


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

import java.io.IOException;

public class WordCoundReducer extends Reducer<Text, LongWritable,Text,LongWritable> {
    private LongWritable total = new LongWritable();
    public void reduce(Text key,Iterable<LongWritable> values,Context context) throws IOException, InterruptedException {
        //用来做累加
        int sum = 0;
        for (LongWritable value : values) {
            long i=value.get();
            sum+=i;
        }
        total.set(sum);
        //输出到文件
        context.write(key,total);
    }
}

3、WordCountDriver.java

package com.fjh.mr;

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;

import java.io.IOException;

public class WordCountDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //获取当前的默认配置
        Configuration conf=new Configuration();
        //获取代表当前mr作业的job对象
        Job job= Job.getInstance(conf);
        //指定一下当前程序的入口类
        job.setJarByClass(WordCountDriver.class);

        //指定当前Mapper任务的类
        job.setMapperClass(WordCountMapper.class);
        //指定当前Reducer任务的类
        job.setReducerClass(WordCoundReducer.class);

        //设置切片 如果不设置InputFormat,它默认用的是TextInputFormat.class
        //切片数决定 mapTask启动数量 任务并行度
        //job.setInputFormatClass(CombineTextInputFormat.class);
        //虚拟存储切片最大值设置20m
        //CombineTextInputFormat.setMaxInputSplitSize(job,20971520);

        //设置Mapper的结果类型

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(LongWritable.class);
        //设置Reducer的结果类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //设置路径,包括了输入文件和输出路径

        FileInputFormat.setInputPaths(job,new Path("hdfs://bigdata01:9000/input/hello.txt"));
        //输出路径的文件夹不能存在,若存在,则报错。
        FileOutputFormat.setOutputPath(job,new Path("src/main/resources/output/wordcount"));

        if(!job.waitForCompletion(true)){
            return;
        }
    }
}

结果如图

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雲小妖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值