MapReduce学习(八)wordcount实现单词计数

本文介绍了一个使用Hadoop MapReduce实现的WordCount程序。该程序读取文本文件,统计每个单词出现的次数,并将结果输出到指定目录。示例展示了如何配置Job参数,设置Mapper与Reducer类以及具体实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class Wordcount111 {
    public static void main(String[] args) throws Exception {
        Configuration conf = new Configuration();
//        conf.set("fs.defaultFS","hdfs:potter2:9000");
//        System.setProperty("HADOOP_USER_NAME","potter");
        FileSystem fs = FileSystem.get(conf);

        Job job = Job.getInstance();
        job.setJarByClass(Wordcount111.class);

        job.setMapperClass(WordcountMapper.class);
        job.setReducerClass(WordcountReaducer.class);

        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(IntWritable.class);

        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);

        Path input = new Path("D:\\111\\222\\hello.txt");
        Path output = new Path("D:\\aaa");

        FileInputFormat.setInputPaths(job,input);
        FileOutputFormat.setOutputPath(job,output);

        if (fs.exists(output)){
            fs.delete(output,true);
        }
        boolean isdone = job.waitForCompletion(true);
        System.exit(isdone ? 0 :1);

    }

    public static class WordcountMapper extends Mapper<LongWritable,Text,Text,IntWritable>{
        Text text = new Text();
        IntWritable ii = new IntWritable();
        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String[] split = value.toString().split(",");

            for (String word : split){
                text.set(word);
                ii.set(1);
                context.write(text,ii);
            }
        }
    }


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

        }
    }
}

运行结果:

i	1
jump	2
you	1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值