今天试了很久终于将基于 streaming的 Map/Reduce调通了。看了半天的hadoop streaming 的介绍竟然全部都没有streaming的KV键值对的介绍。
首先讲一下streaming的 Hadoop脚本的程序的执行,Mapper将数据按行读进来,Reduce接收整行的数据。注意此时Reducer处理程序的时候,默认按照第一列数据作为键值的Key并且使用 \t进行划分。下面就是基于streaming的wordcount程序。
使用streaming编写Map/Reduce挺快,但是shell脚本调试确实困难。。。。
首先gen.sh的程序:
#!/bin/bash
date_ymd=`date -d +"%Y%m%d"`
hadoop_home=/opt/hadoop-client/hadoop
HADOOP=${hadoop_home}/bin/hadoop
INPUT=
OUTPUT=
${hadoop_home}/bin/hadoop fs -test -e ${OUTPUT}
if [ $? == 0 ]
then
${HADOOP} fs -rmr ${OUTPUT}
fi
${HADOOP} streaming \
-D mapred.reduce.tasks=23 \
-D mapred.job.name="cc_count" \
-input ${INPUT} \
-output ${OUTPUT} \
-mapper "mapper.sh" -reducer "reducer.sh" \
-file mapper.sh \
-file reducer.sh
mapper.sh 程序:
awk -F"\3|\t" '{
for(i=0;i<NF;i++){
print $i "\t" 0
}
}'
reducer.sh程序:
awk -F"\t|\3" '{
if( url[$1] == "" ){
urlarray[$1]=1
}
else
urlarray[$1]++
}
END{
for url in urlarray
do
print url "\t" urlarray[url]
done
}'