fdhfgh 发表于 2016-12-8 07:04:25

awk and hadoop之mapper

  1.  在awk 中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时候的二次排序,可以搞定
页: [1]
查看完整版本: awk and hadoop之mapper