读书人

awk and hadoop之地图per

发布时间: 2013-08-10 21:14:06 作者: rapoo

awk and hadoop之mapper
awk -F "\t" '{ filename = ENVIRON["mapreduce_map_input_file"]; if (index(filename, "xxxx") > 0) { // xxx } else { //xxxx }}

??这样来取文件的名字,来判断当前处理的行属于哪个文件,以此进行相应的处理。

?

2. ?在hadoop 中我们经常需要对两个文件做一个join操作,即取两个文件的交集,或者在一个集合中过滤掉特定的集合,如果这个一个集合很小, 我们可以把这个集合加入到一个字典中,然后过滤, 在mapper 中这么写。

awk -F "\t" -v file=${smail_set} 'BEGIN{  while (getline < file > 0) {    dict[$1] = 1;   }  }{   if($1 in dict)     //xxxx   else      print xxxx}'

?reducer 直接 uniq 即可

?

3. ?如果两个集合做 join 或者补集的操作,那么只能对集合打标签,在mapper中我们这么写:

awk -F "\t" '{  filename = ENVIRON["mapreduce_map_input_file"];  if (index(filename, "xxxx") > 0) {     print  $1"\t0\t"$0  }  else {     print $1"\t1\t"$0  }}

?第二列 一个0 一个1 ?用$1 让他们combine的时候到一起去,结合shuffle时候的二次排序,可以搞定

读书人网 >移动开发

热点推荐