package combiner;
import java.io.IOException;
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.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MyCombiner {
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Job job=Job.getInstance(new Configuration());
job.setJarByClass(MyCombiner.class);
//加载CountMapper
job.setMapperClass(CountMapper.class);
//加载Combiner
job.setCombinerClass(CountReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(LongWritable.class);
FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1]));
System.exit(job.waitForCompletion(true)?1:0);
}
public static class CountMapper extends Mapper<LongWritable,Text ,Text, LongWritable>{
@Override
protected void map(LongWritable key, Text text, Mapper<LongWritable, Text, Text, LongWritable>.Context context)
throws IOException, InterruptedException {
String[] arr=text.toString().split("\\s");
for(String word:arr) {
context.write(new Text(word), new LongWritable(1));
}
}
}
//继承Mapper
public static class CountReducer extends Reducer<Text, LongWritable, Text, LongWritable>{
@Override
protected void reduce(Text text, Iterable<LongWritable> it,
Reducer<Text, LongWritable, Text, LongWritable>.Context context) throws IOException, InterruptedException {
String word=text.toString();
long cunt=0;
for(LongWritable s:it) {
cunt+=s.get();
}
context.write(new Text(word), new LongWritable(cunt));
}
}
}
运行过程:
[root@Hadoop1 apps]# hadoop jar count.jar /a.txt /out11/out001
19/07/16 01:45:29 INFO client.RMProxy: Connecting to ResourceManager at hadoop2/192.168.137.130:8032
19/07/16 01:45:30 WARN mapreduce.JobResourceUploader: Hadoop command-line option parsing not performed. Implement the Tool interface and execute your application with ToolRunner to remedy this.
19/07/16 01:45:30 INFO input.FileInputFormat: Total input paths to process : 1
19/07/16 01:45:31 INFO mapreduce.JobSubmitter: number of splits:1
19/07/16 01:45:31 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1563211866776_0001
19/07/16 01:45:31 INFO impl.YarnClientImpl: Submitted application application_1563211866776_0001
19/07/16 01:45:32 INFO mapreduce.Job: The url to track the job: http://hadoop2:8088/proxy/application_1563211866776_0001/
19/07/16 01:45:32 INFO mapreduce.Job: Running job: job_1563211866776_0001
19/07/16 01:45:42 INFO mapreduce.Job: Job job_1563211866776_0001 running in uber mode : false
19/07/16 01:45:42 INFO mapreduce.Job: map 0% reduce 0%
19/07/16 01:45:51 INFO mapreduce.Job: map 100% reduce 0%
19/07/16 01:46:01 INFO mapreduce.Job: map 100% reduce 100%
19/07/16 01:46:01 INFO mapreduce.Job: Job job_1563211866776_0001 completed successfully
19/07/16 01:46:01 INFO mapreduce.Job: Counters: 49
File System Counters
FILE: Number of bytes read=63
FILE: Number of bytes written=212737
FILE: Number of read operations=0
FILE: Number of large read operations=0
FILE: Number of write operations=0
HDFS: Number of bytes read=124
HDFS: Number of bytes written=25
HDFS: Number of read operations=6
HDFS: Number of large read operations=0
HDFS: Number of write operations=2
Job Counters
Launched map tasks=1
Launched reduce tasks=1
Data-local map tasks=1
Total time spent by all maps in occupied slots (ms)=6264
Total time spent by all reduces in occupied slots (ms)=7145
Total time spent by all map tasks (ms)=6264
Total time spent by all reduce tasks (ms)=7145
Total vcore-milliseconds taken by all map tasks=6264
Total vcore-milliseconds taken by all reduce tasks=7145
Total megabyte-milliseconds taken by all map tasks=6414336
Total megabyte-milliseconds taken by all reduce tasks=7316480
Map-Reduce Framework
Map input records=4
Map output records=8
Map output bytes=98
Map output materialized bytes=63
Input split bytes=90
Combine input records=8
Combine output records=4
Reduce input groups=4
Reduce shuffle bytes=63
Reduce input records=4
Reduce output records=4
Spilled Records=8
Shuffled Maps =1
Failed Shuffles=0
Merged Map outputs=1
GC time elapsed (ms)=161
CPU time spent (ms)=1610
Physical memory (bytes) snapshot=314433536
Virtual memory (bytes) snapshot=1686605824
Total committed heap usage (bytes)=136122368
Shuffle Errors
BAD_ID=0
CONNECTION=0
IO_ERROR=0
WRONG_LENGTH=0
WRONG_MAP=0
WRONG_REDUCE=0
File Input Format Counters
Bytes Read=34
File Output Format Counters
Bytes Written=25
结果如下:
集群上的可用函数限制了MapReduce作业的数量,一次避免map和reduce之间的数据传输是有利的。
combiner函数不能取代reducer但是可以减少map和reduce之间数据的传输,从而减少对带宽的占用。(例如:求最大值时可以使用,但是求平均值时就不能用)。
combiner是通过reducer来定义的
问题提出:
众所周知,Hadoop框架使用Mapper将数据处理成一个<key,value>键值对,再网络节点间对其进行整理(shuffle),然后使用Reducer处理数据并进行最终输出。 在上述过程中,我们看到至少两个性能瓶颈:(引用)
如果我们有10亿个数据,Mapper会生成10亿个键值对在网络间进行传输,但如果我们只是对数据求最大值,那么很明显的Mapper只需要输出它所知道的最大值即可。这样做不仅可以减轻网络压力,同样也可以大幅度提高程序效率。
使用专利中的国家一项来阐述数据倾斜这个定义。这样的数据远远不是一致性的或者说平衡分布的,由于大多数专利的国家都属于美国,这样不仅Mapper中的键值对、中间阶段(shuffle)的键值对等,大多数的键值对最终会聚集于一个单一的Reducer之上,压倒这个Reducer,从而大大降低程序的性能。
目标:
Mapreduce中的Combiner就是为了避免map任务和reduce任务之间的数据传输而设置的,Hadoop允许用户针对map task的输出指定一个合并函数。即为了减少传输到Reduce中的数据量。它主要是为了削减Mapper的输出从而减少网络带宽和Reducer之上的负载。
数据格式转换:
map: (K1, V1) → list(K2,V2)
combine: (K2, list(V2)) → list(K3, V3)
reduce: (K3, list(V3)) → list(K4, V4)
注意:combine的输入和reduce的完全一致,输出和map的完全一致
使用注意:
对于Combiner有几点需要说明的是:
1)有很多人认为这个combiner和map输出的数据合并是一个过程,其实不然,map输出的数据合并只会产生在有数据spill出的时候,即进行merge操作。
2)与mapper与reducer不同的是,combiner没有默认的实现,需要显式的设置在conf中才有作用。
3)并不是所有的job都适用combiner,只有操作满足结合律的才可设置combiner。combine操作类似于:opt(opt(1, 2, 3), opt(4, 5, 6))。如果opt为求和、求最大值的话,可以使用,但是如果是求中值的话,不适用。
4)一般来说,combiner和reducer它们俩进行同样的操作。
但是:特别值得注意的一点,一个combiner只是处理一个结点中的的输出,而不能享受像reduce一样的输入(经过了shuffle阶段的数据),这点非常关键。具体原因查看下面的数据流解释:
融合combiner的数据流
插入了Combiner的MapReduce数据流
Combiner:前面展示的流水线忽略了一个可以优化MapReduce作业所使用带宽的步骤,这个过程叫Combiner,它在Mapper之后Reducer之前运行。Combiner是可选的,如果这个过程适合于你的作业,Combiner实例会在每一个运行map任务的节点上运行。Combiner会接收特定节点上的Mapper实例的输出作为输入,接着Combiner的输出会被发送到Reducer那里,而不是发送Mapper的输出。Combiner是一个“迷你reduce”过程,它只处理单台机器生成的数据(特别重要,作者在做一个矩阵乘法的时候,没有领会到这点,把它当成一个完全的reduce的输入数据来处理,结果出错。)。
词频统计是一个可以展示Combiner的用处的基础例子,上面的词频统计程序为每一个它看到的词生成了一个(word,1)键值对。所以如果在同一个文档内“cat”出现了3次,(”cat”,1)键值对会被生成3次,这些键值对会被送到Reducer那里。通过使用Combiner,这些键值对可以被压缩为一个送往Reducer的键值对(”cat”,3)。现在每一个节点针对每一个词只会发送一个值到reducer,大大减少了shuffle过程所需要的带宽并加速了作业的执行。这里面最爽的就是我们不用写任何额外的代码就可以享用此功能!如果你的reduce是可交换及可组合的,那么它也就可以作为一个Combiner。你只要在driver中添加下面这行代码就可以在词频统计程序中启用Combiner。