WordCount案例
需求1:统计一堆文件中单词出现的个数(WordCount案例)
0)需求:在一堆给定的文本文件中统计输出每一个单词出现的总次数
1)数据准备:
hello.txt
2)分析
按照mapreduce编程规范,分别编写Mapper,Reducer,Driver。
3)编写程序
(1)定义一个mapper类
1. 用户自定义的Mapper要继承自己的父类
2. Mapper的输入数据是KV对的形式(KV的类型可自定义)
3. Mapper中的业务逻辑写在map()方法中
4. Mapper的输出数据是KV对的形式(KV的类型可自定义)
5. map()方法(maptask进程)对每一个<K,V>调用一次
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
/**
* KEYIN:输入数据的key 文件的行号
* VALUEIN:每行的输入数据
*
* KEYOUT:输出数据 的key
* VALUEOUT:输出数据的value类型
* @author Administrator
*/
public class WordcountMapper extends Mapper<LongWritable, Text, Text, IntWritable>{
//hello world
//atguigu atguigu
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
// 1获取这一行数据
String line = value.toString();
// 2 获取每一个单词
String[] words = line.split(" ");
for(String word:words){
// 3 输出每一个单词
context.write(new Text(word), new IntWritable(1));
}
}
}
(2)定义一个reducer类
1.用户自定义的Reducer要继承自己的父类
2.Reducer的输入数据类型对应Mapper的输出数据类型,也是KV
3.Reducer的业务逻辑写在reduce()方法中
4.Reducetask进程对每一组相同k的<k,v>组调用一次reduce()方法
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class WordcountReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
// atguigu 1 atguigu 1
@Override
protected void reduce(Text key, Iterable<IntWritable> values, Context context)
throws IOException, InterruptedException {
// 1 统计所有单词个数
int count = 0;
for (IntWritable value : values) {
count += value.get();
}
// 2输出所有单词个数
context.write(key, new IntWritable(count));
}
}
(3)定义一个主类,用来描述job并提交job(Driver阶段)
整个程序需要一个Drvier来进行提交,提交的是一个描述了各种必要信息的job对象
package com.atguigu.mapreduce.wordcount;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.CombineTextInputFormat;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
// 驱动主程序
public class WordcountDriver {
public static void main(String[] args) throws Exception {
// 1 获取job对象信息
Configuration configuration = new Configuration();
Job job = Job.getInstance(configuration);
// 2 设置加载jar位置
job.setJarByClass(WordcountDriver.class);
// 3 设置mapper和reducer的class类
job.setMapperClass(WordcountMapper.class);
job.setReducerClass(WordcountReducer.class);
// 4 设置输出mapper的数据类型
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
// 5 设置最终数据输出的类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
// 6 设置输入数据和输出数据路径
FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
// 7 提交
boolean result = job.waitForCompletion(true);
System.exit(result ? 0 : 1);
}
}
需求2:把单词按照ASCII码奇偶分区(Partitioner)
见 Hadoop-5 中 partition分区
0)分析
1)自定义分区
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;
public class WordCountPartitioner extends Partitioner<Text, IntWritable>{
// hadoop
@Override
public int getPartition(Text key, IntWritable value, int numPartitions) {
// 需求:按照单词首字母的ASCII的奇偶分区
String line = key.toString();
// 1 截取首字母
String firword = line.substring(0, 1);
// 2 转换成ASCII
char[] charArray = firword.toCharArray();
int result = charArray[0];
// 3 按照奇偶分区
if (result % 2 ==0 ) {
return 0;
}else {
return 1;
}
}
}
2)在驱动中配置加载分区,设置reducetask个数
job.setPartitionerClass(WordCountPartitioner.class);
job.setNumReduceTasks(2);
需求3:对每一个maptask的输出局部汇总(Combiner)
见 Hadoop-5 中 Combiner合并
0)需求:统计过程中对每一个maptask的输出进行局部汇总,以减小网络传输量即采用Combiner功能。
1)数据准备:
hellow.txt
方案一:
(1)增加一个WordcountCombiner类继承Reducer
import java.io.IOException;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;
public class WordCountCombiner extends Reducer<Text, IntWritable, Text, IntWritable>{
// <a, 1> <a, 1>
// <atguigu, 1> <atguigu, 1> <atguigu, 1> 输出为<atguigu,3>
@Override
protected void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
// 计算累加和
int count = 0;
for(IntWritable value:values){
count += value.get();
}
// 写出
context.write(key, new IntWritable(count));
}
}
(2)在WordcountDriver驱动类中指定combiner
//指定需要使用combiner,以及用哪个类作为combiner的逻辑
job.setCombinerClass(WordcountCombiner.class);
方案二:
(1)将WordcountReducer作为combiner在WordcountDriver驱动类中指定
//指定需要使用combiner,以及用哪个类作为combiner的逻辑
job.setCombinerClass(WordcountReducer.class);
运行程序
需求4:大量小文件的切片优化(CombineTextInputFormat)
见 Hadoop-5 中 CombineTextInputFormat切片机制
0)需求:将输入的大量小文件合并成一个切片统一处理。
1)输入数据:准备5个小文件
2)实现过程
(1)不做任何处理,运行需求1中的wordcount程序,观察切片个数为5
INFO [org.apache.hadoop.mapreduce.JobSubmitter] - number of splits:5
(2)在WordcountDriver中增加如下代码,运行程序,并观察运行的切片个数为1
// 9 如果不设置InputFormat,它默认用的是TextInputFormat.class
job.setInputFormatClass(CombineTextInputFormat.class);
CombineTextInputFormat.setMaxInputSplitSize(job, 4194304);// 4m
CombineTextInputFormat.setMinInputSplitSize(job, 2097152);// 2m
INFO [org.apache.hadoop.mapreduce.JobSubmitter] - number of splits:1
(3)注意:如果eclipse打印不出日志,在控制台上只显示
1.log4j:WARN No appenders could be found for logger (org.apache.hadoop.util.Shell).
2.log4j:WARN Please initialize the log4j system properly.
3.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
需要在项目的src目录下,新建一个文件,命名为“log4j.properties”,在文件中填入
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n