使用IDEA进行MapReduce编程(WordCount字符统计)

新建WordCountMapper.java

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

    Text txt=new Text();
    IntWritable intWritable=new IntWritable();

    /**
     *
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     *
     *      key:位置信息    value:hello jdk hello java
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        System.out.println("WordCountMapper     key:"+key+"     value:"+value);
        String[] words = value.toString().split(" ");
        for (String word:
             words) {
            txt.set(word);
            intWritable.set(1);
            context.write(txt,intWritable);
        }
    }
}

新建WordCountReduce.java

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

import java.io.IOException;

public class WordCountReduce extends Reducer<Text, IntWritable,Text, LongWritable> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int count=0;
        for (IntWritable intWritable:
             values) {
            count+=intWritable.get();
        }
        LongWritable longWritable=new LongWritable(count);
        System.out.println("WordCountReduce     key:"+key+"     value:"+longWritable.get());
        context.write(key,longWritable);
    }
}

新建WordCountDriver.java,使用本地进行测试

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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();
        Job job = Job.getInstance(conf);

        job.setJarByClass(WordCountDriver.class);

        // 配置当前job  执行的mapper类
        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 配置当前job  执行的reduce类
        job.setReducerClass(WordCountReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        // 指定读取文件夹的路径
        Path pathin = new Path("G:\\hadoopstu\\in\\demo1");
        FileInputFormat.setInputPaths(job,pathin);

        // 指定执行任务完成后的输出路径
        Path pathout = new Path("G:\\hadoopstu\\in\\out1");
        FileSystem fileSystem = FileSystem.get(pathout.toUri(),conf);
        if(fileSystem.exists(pathout)){
            fileSystem.delete(pathout,true);
        }
        FileOutputFormat.setOutputPath(job,pathout);
        
        job.waitForCompletion(true);
    }
}

将需要统计字符的文本文件放到项目的./in/demo1文件夹下
在这里插入图片描述
进行本地测试
在这里插入图片描述
将要统计字符的文本文件上传到HDFS

[root@mihaoyu151 shelldemo]# ll
total 8
-rw-r--r--. 1 root root 2918 Oct 27  2021 hello2.txt
-rw-r--r--. 1 root root   57 Oct 27  2021 hello.txt
[root@mihaoyu151 shelldemo]# hdfs dfs -put ./* /input
21/10/27 15:06:12 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable
[root@mihaoyu151 shelldemo]# 

在这里插入图片描述
上传到HDFS,使用args参数
在这里插入图片描述
args[0]=hdfs://mihaoyu151:9000/input
args[1]=hdfs://mihaoyu151:9000/output

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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();
        Job job = Job.getInstance(conf);

        job.setJarByClass(WordCountDriver.class);

        // 配置当前job  执行的mapper类
        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        // 配置当前job  执行的reduce类
        job.setReducerClass(WordCountReduce.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        // 指定读取文件夹的路径
//        Path pathin=new Path("hdfs://mihaoyu151:9000/input");
        Path pathin=new Path(args[0]);
        FileInputFormat.setInputPaths(job,pathin);

        // 指定执行任务完成后的输出路径
//        Path pathout=new Path("hdfs://mihaoyu151:9000/output");
        Path pathout=new Path(args[1]);
        FileSystem fileSystem = FileSystem.get(pathout.toUri(),conf);
        if(fileSystem.exists(pathout)){
            fileSystem.delete(pathout,true);
        }
        FileOutputFormat.setOutputPath(job,pathout);

        job.waitForCompletion(true);
    }
}

导入包的时候需要注意是org.apache.hadoop.mapreduce包,而不是org.apache.hadoop.mapred包。
在这里插入图片描述
进行测试
在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

honconM

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

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

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

打赏作者

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

抵扣说明:

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

余额充值