如何让 Reducer 只发出重复项
How to get the Reducer to emit only duplicates
我有一个 Mapper,它正在处理大量数据并将 ID 号作为值为 1 的键发出。我希望通过 MapReduce 作业完成的是获取已找到的所有 ID 的列表超过所有数据的一次,这是重复 ID 的列表。例如:
映射器发出:
美国广播公司 1
效果图 1
CBA 1
美国广播公司 1
dhh 1
在这种情况下,您可以看到 ID 'abc' 已被 Mapper 发出不止一次。
我如何编辑这个 Reducer 以便它只发出重复项?即值大于 1 的键:
import sys
import codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
inData = codecs.getreader('utf-8')(sys.stdin)
(last_key, tot_cnt) = (None, 0)
for line in inData:
(key, val) = line.strip().split("\t")
if last_key and last_key != key:
sys.stdout.write("%s\t%s\n" % (last_key,tot_cnt))
(last_key, tot_cnt) = (key, int(val))
else:
(last_key, tot_cnt) = (key, tot_cnt + int(val))
if last_key:
sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
你有几个地方犯了错误。
此代码:
if last_key and last_key != key:
sys.stdout.write("%s\t%s\n" % (last_key,tot_cnt))
应改为:
if last_key != key:
if(tot_cnt > 1):
sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
您没有检查 tot_cnt > 1
。
最后两行:
if last_key:
sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
应改为:
if last_key and tot_cnt > 1:
sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
又一次,您没有检查 tot_cnt > 1
。
以下是修改后的代码,对我有用:
import sys
import codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
inData = codecs.getreader('utf-8')(sys.stdin)
(last_key, tot_cnt) = (None, 0)
for line in inData:
(key, val) = line.strip().split("\t")
if last_key != key:
if(tot_cnt > 1):
sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
(last_key, tot_cnt) = (key, int(val))
else:
(last_key, tot_cnt) = (key, tot_cnt + int(val))
if last_key and tot_cnt > 1:
sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
对于您的数据,我得到以下输出:
abc 2
我有一个 Mapper,它正在处理大量数据并将 ID 号作为值为 1 的键发出。我希望通过 MapReduce 作业完成的是获取已找到的所有 ID 的列表超过所有数据的一次,这是重复 ID 的列表。例如:
映射器发出:
美国广播公司 1
效果图 1
CBA 1
美国广播公司 1
dhh 1
在这种情况下,您可以看到 ID 'abc' 已被 Mapper 发出不止一次。
我如何编辑这个 Reducer 以便它只发出重复项?即值大于 1 的键:
import sys
import codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
inData = codecs.getreader('utf-8')(sys.stdin)
(last_key, tot_cnt) = (None, 0)
for line in inData:
(key, val) = line.strip().split("\t")
if last_key and last_key != key:
sys.stdout.write("%s\t%s\n" % (last_key,tot_cnt))
(last_key, tot_cnt) = (key, int(val))
else:
(last_key, tot_cnt) = (key, tot_cnt + int(val))
if last_key:
sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
你有几个地方犯了错误。
此代码:
if last_key and last_key != key: sys.stdout.write("%s\t%s\n" % (last_key,tot_cnt))
应改为:
if last_key != key: if(tot_cnt > 1): sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
您没有检查
tot_cnt > 1
。最后两行:
if last_key: sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
应改为:
if last_key and tot_cnt > 1: sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
又一次,您没有检查
tot_cnt > 1
。
以下是修改后的代码,对我有用:
import sys
import codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
inData = codecs.getreader('utf-8')(sys.stdin)
(last_key, tot_cnt) = (None, 0)
for line in inData:
(key, val) = line.strip().split("\t")
if last_key != key:
if(tot_cnt > 1):
sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
(last_key, tot_cnt) = (key, int(val))
else:
(last_key, tot_cnt) = (key, tot_cnt + int(val))
if last_key and tot_cnt > 1:
sys.stdout.write("%s\t%s\n" % (last_key, tot_cnt))
对于您的数据,我得到以下输出:
abc 2