(1)mapreduce程序是被提交给LocalJobRunner在本地以单进程的形式运行。在本地运行mapreduce程序可以更快地运行,并且可以使用debug进行跟踪代码,方便查错,在本地运行主要是看mapreduce的业务逻辑是不是正确,如果在本地运行错误的话,那么在集群上肯定也是错的
(2)处理的数据及输出结果可以在本地文件系统,也可以在hdfs上
(3)本地模式非常便于进行业务逻辑的debug,只要在eclipse中打断点即可
(4)怎样实现本地运行?写一个程序,不要带集群的配置文件(本质是你的mr程序的conf中是否有mapreduce.framework.name=local以及yarn.resourcemanager.hostname参数)
如果在windows下想运行本地模式来测试程序逻辑,需要在windows中配置环境变量:
%HADOOP_HOME% = d:/hadoop-2.6.1
%PATH% = %HADOOP_HOME%\bin
并且要将d:/hadoop-2.6.1的lib和bin目录替换成windows平台编译的版本
无jar版windows平台hadoop-2.6.1.zip
https://pan.baidu.com/s/1uu8tKq3FvoemccoUiBob0Q
在linux下编译的hadoop:
https://pan.baidu.com/s/1qtXrWnKjk_klRdPj8DaSYA
只需要替换一下就可以正常使用了
Configuration conf = new Configuration();
//是否运行为本地模式,就是看这个参数值是否为local,默认就是local
conf.set("mapreduce.framework.name", "local");
//本地模式运行mr程序时,输入输出的数据可以在本地,也可以在hdfs上
//到底在哪里,就看以下两行配置你用哪行,默认就是file:///
/*conf.set("fs.defaultFS", "hdfs://mini1:9000/");*/
conf.set("fs.defaultFS", "file:///");
conf什么都不写,就是使用默认的配置,默认的配置就是在本地。
可以看一下默认的配置
默认的配置文件:
https://pan.baidu.com/s/1xKT7_SPv8qsSQmbze8O6wQ
可以自己在源码中分别找到这些配置文件:
所以可以直接在本地运行
代码地址:
https://gitee.com/tanghongping/hadoopMapReduce/tree/master/src/com/thp/bigdata/wcdemo
Name | Value |
---|---|
key | 0 |
value | we are family |
context | org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context@1b7d8bfa |
然后就是接下的一行的数据
map里面的参数:
Name | Value |
---|---|
key | 15 |
value | hello sa |
context | org.apache.hadoop.mapreduce.lib.map.WrappedMapper$Context@1b7d8bfa |
之后的input文件的每一行的数据都会经历这个过程
现在还是处在map阶段
当input文件夹里面的第一个文件处理完毕,接下来就会处理第二个文件:
我们的input文件夹里面总共是有7个文件的,这7个文件都是同一个文件:
2018-11-14 09:12:17,123 INFO [localfetcher#1] reduce.LocalFetcher (LocalFetcher.java:copyMapOutput(141)) - localfetcher#1 about to shuffle output of map attempt_local1550344450_0001_m_000005_0 decomp: 211 len: 215 to MEMORY
2018-11-14 09:12:17,128 INFO [localfetcher#1] reduce.InMemoryMapOutput (InMemoryMapOutput.java:shuffle(100)) - Read 211 bytes from map-output for attempt_local1550344450_0001_m_000005_0
2018-11-14 09:12:17,131 INFO [localfetcher#1] reduce.MergeManagerImpl (MergeManagerImpl.java:closeInMemoryFile(315)) - closeInMemoryFile -> map-output of size: 211, inMemoryMapOutputs.size() -> 1, commitMemory -> 0, usedMemory ->211
2018-11-14 09:12:17,138 INFO [localfetcher#1] reduce.LocalFetcher (LocalFetcher.java:copyMapOutput(141)) - localfetcher#1 about to shuffle output of map attempt_local1550344450_0001_m_000002_0 decomp: 211 len: 215 to MEMORY
2018-11-14 09:12:17,140 INFO [localfetcher#1] reduce.InMemoryMapOutput (InMemoryMapOutput.java:shuffle(100)) - Read 211 bytes from map-output for attempt_local1550344450_0001_m_000002_0
2018-11-14 09:12:17,141 INFO [localfetcher#1] reduce.MergeManagerImpl (MergeManagerImpl.java:closeInMemoryFile(315)) - closeInMemoryFile -> map-output of size: 211, inMemoryMapOutputs.size() -> 2, commitMemory -> 211, usedMemory ->422
2018-11-14 09:12:17,159 INFO [localfetcher#1] reduce.LocalFetcher (LocalFetcher.java:copyMapOutput(141)) - localfetcher#1 about to shuffle output of map attempt_local1550344450_0001_m_000006_0 decomp: 211 len: 215 to MEMORY
2018-11-14 09:12:17,160 INFO [localfetcher#1] reduce.InMemoryMapOutput (InMemoryMapOutput.java:shuffle(100)) - Read 211 bytes from map-output for attempt_local1550344450_0001_m_000006_0
2018-11-14 09:12:17,161 INFO [localfetcher#1] reduce.MergeManagerImpl (MergeManagerImpl.java:closeInMemoryFile(315)) - closeInMemoryFile -> map-output of size: 211, inMemoryMapOutputs.size() -> 3, commitMemory -> 422, usedMemory ->633
2018-11-14 09:12:17,170 INFO [localfetcher#1] reduce.LocalFetcher (LocalFetcher.java:copyMapOutput(141)) - localfetcher#1 about to shuffle output of map attempt_local1550344450_0001_m_000003_0 decomp: 211 len: 215 to MEMORY
2018-11-14 09:12:17,172 INFO [localfetcher#1] reduce.InMemoryMapOutput (InMemoryMapOutput.java:shuffle(100)) - Read 211 bytes from map-output for attempt_local1550344450_0001_m_000003_0
2018-11-14 09:12:17,173 INFO [localfetcher#1] reduce.MergeManagerImpl (MergeManagerImpl.java:closeInMemoryFile(315)) - closeInMemoryFile -> map-output of size: 211, inMemoryMapOutputs.size() -> 4, commitMemory -> 633, usedMemory ->844
2018-11-14 09:12:17,180 INFO [localfetcher#1] reduce.LocalFetcher (LocalFetcher.java:copyMapOutput(141)) - localfetcher#1 about to shuffle output of map attempt_local1550344450_0001_m_000000_0 decomp: 211 len: 215 to MEMORY
2018-11-14 09:12:17,181 INFO [localfetcher#1] reduce.InMemoryMapOutput (InMemoryMapOutput.java:shuffle(100)) - Read 211 bytes from map-output for attempt_local1550344450_0001_m_000000_0
2018-11-14 09:12:17,182 INFO [localfetcher#1] reduce.MergeManagerImpl (MergeManagerImpl.java:closeInMemoryFile(315)) - closeInMemoryFile -> map-output of size: 211, inMemoryMapOutputs.size() -> 5, commitMemory -> 844, usedMemory ->1055
2018-11-14 09:12:17,189 INFO [localfetcher#1] reduce.LocalFetcher (LocalFetcher.java:copyMapOutput(141)) - localfetcher#1 about to shuffle output of map attempt_local1550344450_0001_m_000001_0 decomp: 211 len: 215 to MEMORY
2018-11-14 09:12:17,190 INFO [localfetcher#1] reduce.InMemoryMapOutput (InMemoryMapOutput.java:shuffle(100)) - Read 211 bytes from map-output for attempt_local1550344450_0001_m_000001_0
2018-11-14 09:12:17,190 INFO [localfetcher#1] reduce.MergeManagerImpl (MergeManagerImpl.java:closeInMemoryFile(315)) - closeInMemoryFile -> map-output of size: 211, inMemoryMapOutputs.size() -> 6, commitMemory -> 1055, usedMemory ->1266
2018-11-14 09:12:17,196 INFO [localfetcher#1] reduce.LocalFetcher (LocalFetcher.java:copyMapOutput(141)) - localfetcher#1 about to shuffle output of map attempt_local1550344450_0001_m_000004_0 decomp: 211 len: 215 to MEMORY
2018-11-14 09:12:17,197 INFO [localfetcher#1] reduce.InMemoryMapOutput (InMemoryMapOutput.java:shuffle(100)) - Read 211 bytes from map-output for attempt_local1550344450_0001_m_000004_0
2018-11-14 09:12:17,197 INFO [localfetcher#1] reduce.MergeManagerImpl (MergeManagerImpl.java:closeInMemoryFile(315)) - closeInMemoryFile -> map-output of size: 211, inMemoryMapOutputs.size() -> 7, commitMemory -> 1266, usedMemory ->1477
2018-11-14 09:12:17,198 INFO [EventFetcher for fetching Map Completion Events] reduce.EventFetcher (EventFetcher.java:run(76)) - EventFetcher is interrupted.. Returning
当所有的map都运行完成,那么就要走到reduce阶段,
protected void reduce(Text key, Iterable values, Context context) throws IOException, InterruptedException {
int count = 0;
for(IntWritable value : values) {
count += value.get(); // 这个count 最后就是某一个单词的汇总的值
}
context.write(key, new IntWritable(count));
}
reduce的参数:
Name | Value |
---|---|
key | are |
values | org.apache.hadoop.mapreduce.task.ReduceContextImpl$ValueIterable@6e4a6525 |
context | org.apache.hadoop.mapreduce.lib.reduce.WrappedReducer$Context@79cda784 |
count | 0 |
接下来的阶段就是不断地重复这个过程,但是需要注意的一点是这个reduce的过程是已经排序了的过程,按照字典排序好的。