mapreduce实现简单计数

本文详细介绍了一种使用Hadoop MapReduce进行数据统计的实战案例,包括必要的库文件、程序实现、打包上传及执行流程,同时提供了常见问题解决方案。

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

准备工作:

 

1).添加程序所需要依赖的jar包

1.commons-cli:主要提供了解析命令行的库

2.commons-logging:常用的日志相关库

3.guava: guava的中文意思其实是石榴嘛,是google的一个开源项目。其中包含了很多java的常用库

4.hadoop-common:hadoop的基础依赖库,包括配置文件,文件系统,通信,安全等。

5.hadoop-mapreduce-client-core:顾名思义,这是编写mapreduce程序的核心依赖库了。

 

2)程序

package hadoop;

 

import org.apache.hadoop.conf.Configuration;

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.input.TextInputFormat;

import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;

 

import java.io.IOException;

import java.util.StringTokenizer;

 

/**

* Created by admin on 2018/8/13.

*/

public class TestWord {

public static class WordMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

 

@Override

public void map(LongWritable key, Text values, Mapper<LongWritable, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

String line = values.toString();

StringTokenizer st = new StringTokenizer(line);

while (st.hasMoreTokens()) {

String word = st.nextToken();

context.write(new Text(word), new IntWritable(1));

}

}

}

 

public static class WordReduce extends Reducer<Text,IntWritable,Text,IntWritable>{

@Override

public void reduce(Text key,Iterable<IntWritable> iterable,Reducer<Text,IntWritable,Text,IntWritable>.Context context) throws IOException,InterruptedException{

int sum = 0;

for(IntWritable it:iterable){

sum = sum + it.get();

}

context.write(key,new IntWritable(sum));

}

}

 

public static void main(String[] args) throws Exception{

Configuration con = new Configuration();

Job job = new Job(con);

job.setJarByClass(TestWord.class);

job.setJobName("words count!");

 

job.setMapOutputKeyClass(Text.class);

job.setOutputValueClass(IntWritable.class);

 

job.setInputFormatClass(TextInputFormat.class);

job.setOutputFormatClass(TextOutputFormat.class);

 

job.setMapperClass(WordMapper.class);

job.setReducerClass(WordReduce.class);

 

FileInputFormat.addInputPath(job,new Path("hdfs://hadoop1:9000/test1.txt"));

FileOutputFormat.setOutputPath(job,new Path("hdfs://hadoop1:9000/result/"));

 

job.waitForCompletion(true);

}

}

3)将程序打包并上传到hadoop3,如何打包下面链接有详细教程

https://blog.youkuaiyun.com/huangzhichang13/article/details/53580320

 

4)执行程序

 

./hadoop jar /home/hadoop/hadoop/test/wordcount.jar hadoop/TestWord

 

5)问题

1.

 

 

提示目录已存在,因为是防止用户出现误操作,替换掉原先的文件,所以不能覆盖已经存在的目录,将hdfs上的目录删除即可

 

2.org.apache.hadoop.yarn.exceptions.InvalidAuxServiceException: The auxService:mapreduce_shuffle does not exist

 

是因为这个配置错了,多了一个aux-services,去掉最后一个即可(3个节点的配置都需要)

 

正确结果,部分截图

 

 

数据统计出来了

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值