操作 reduce() MapReduce 模型
Manipulating the reduce() MapReduce Model
我一直在开发一个简单的 wordcount 程序,给定一个文本输入打印出每个单词的出现次数。
reduce 函数如下所示:
def reducer(self, word, count):
yield(word, sum(count))
上面的 reducer() 可以正确计算输入文本文件中每个单词的出现次数。
现在,我想调整 reduce() 函数,以便在输出文件中只打印出出现次数为 10 或更多的单词。我想,它可能看起来像这样:
def reducer(self, word, count):
if sum(count)>10:
emit(word,sum(count))
但是这不起作用。相反,生成的输出文件按每个单词打印 0。我很确定需要调整的是 reducer() 函数而不是 map 函数。但是,除了包含 if 语句之外,我想不出任何东西。非常感谢您的建议。
你可以试试这个:
def threshold(x, y, n=10):
return True if y >= n else False
filter(threshold, reducer)
count
是一个可迭代对象,您将它迭代两次,第二次它是空的,总和将为零。
您需要存储结果,然后检查并输出。否则逻辑正确
def reducer(self, word, count):
_count = sum(count)
if _count > 10:
emit(word, _count)
我一直在开发一个简单的 wordcount 程序,给定一个文本输入打印出每个单词的出现次数。
reduce 函数如下所示:
def reducer(self, word, count):
yield(word, sum(count))
上面的 reducer() 可以正确计算输入文本文件中每个单词的出现次数。
现在,我想调整 reduce() 函数,以便在输出文件中只打印出出现次数为 10 或更多的单词。我想,它可能看起来像这样:
def reducer(self, word, count):
if sum(count)>10:
emit(word,sum(count))
但是这不起作用。相反,生成的输出文件按每个单词打印 0。我很确定需要调整的是 reducer() 函数而不是 map 函数。但是,除了包含 if 语句之外,我想不出任何东西。非常感谢您的建议。
你可以试试这个:
def threshold(x, y, n=10):
return True if y >= n else False
filter(threshold, reducer)
count
是一个可迭代对象,您将它迭代两次,第二次它是空的,总和将为零。
您需要存储结果,然后检查并输出。否则逻辑正确
def reducer(self, word, count):
_count = sum(count)
if _count > 10:
emit(word, _count)