wordcount完整总结

本文详细介绍使用Hadoop实现WordCount的过程,包括在IDEA中创建Maven项目、编写WordCountMapper和WordCountReducer类、Driver类的设置,以及如何在虚拟机上运行打包后的jar文件。

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

1.idea创建maven项目(百度查找)

2.编写wordcount代码

WordCountMapper类

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>{
    @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) {
            context.write(new Text(word), new LongWritable(1));
        }
    }
}

WordCountReducer类

 

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

import java.io.IOException;

public class WordCountReducer extends Reducer<Text, LongWritable, Text, LongWritable> {

    @Override
    protected void reduce(Text key, Iterable<LongWritable> value,Context context)
            throws IOException, InterruptedException {
        // TODO Auto-generated method stub
        //计数
        int count = 0;
        //值累加
        for (LongWritable l : value) {
            count += l.get();
        }
        context.write(key, new LongWritable(count));
    }
}

Driver类

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 Driver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        // 配置信息
        Configuration conf = new Configuration();
        //跑一个job
        Job job = Job.getInstance(conf);
        //设置程序入口
        job.setJarByClass(Driver.class);
        //指定mapper
        job.setMapperClass(WordCountMapper.class);
        job.setMapOutputKeyClass(Text .class);
        job.setMapOutputValueClass(LongWritable .class);
        //指定reducer
        job.setReducerClass(WordCountReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(LongWritable.class);

        //指定输入路径和输出路径
        FileInputFormat.setInputPaths(job, new Path(args[0]));
        FileOutputFormat.setOutputPath(job, new Path(args[1]));

        //提交作业
            job.waitForCompletion(true);
    }

}

3.idea打包成jar包

https://www.cnblogs.com/blog5277/p/5920560.html

4.虚拟机运行wordcount

hadoop jar 打成的jar包名  输入路径  输出路径

举例: hadoop jar /aa.txt /output

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值