Hadoop Streaming 非常友好的一点是可以map 和reduce可以用python或者shell来写。狼厂的大boss在安装Hadoop的时候就说道一定要安装Hadoop streaming!于是开启了我Hadoop streaming 新世界的大门。
现在的我可以shell和python无缝切换写map和reduce啦,但也记录一下踩过的坑。
1. 查询bug问题:
我发现,如果map或者reduce有bug的话,hadoop streaming 在执行的时候并不会告诉你具体的内容,e.g 你的json读到了\n,list越界….. 这一点非常的不好,有时候本地模拟测试map reduce:
cat file1.txt file2.txt |python map.py |sort -k1,2 |awk -f reduce.awk
但有时候本地测试通过,真正在集群上跑了就报错:
Running a job using hadoop streaming and mrjob: PipeMapRed.waitOutputThreads(): subprocess failed with code 1
完全看不出问题来好吗,于是我最开始只有非常笨的用排除法debug代码….效率真的超级低…
2. map和reduce,如果用python写,需要在.py文件加入:
#!/usr/bin/python
3. 如果想对第一列和第二列排序,其中第一列相同时,第二列逆排序
-D mapred.output.key.comparator.class=org.apache.hadoop.mapred.lib.KeyFieldBasedComparator \
-D mapred.text.key.comparator.options="-k1,1 -k2rn" \
-k1,1表示对第一列到第一列正排序(即对第一列正排序)
-k2rn 表示对第二列逆排序
4. hadoop streaming shell 中指定:cacheArchive 是将上传到hdfs上的jar包解压并分发到其他datanode节点
cd test
tar –zcvf test.tar.gz * //本地打包时要进入目录test而不是在test的上层目录打包
hadoop fs –put test.tar.gz /user/hadoop/test/
在shell里面加入:
-cacheArchive hdfs://host:port/user/hadoop/test/test.tar.gz#test
-mapper “./test/mapper.sh ./test/hello.txt” -reducer ./test/reducer.sh
// -mapper选项指定为mapper程序及其运行参数,-reducer选项指定reducer程序