【问题标题】:MapReduce count the number of occurrence rowsMapReduce 统计出现的行数
【发布时间】:2020-06-29 06:11:53
【问题描述】:

目前,我有一个 user_follower.csv 例如:

user  follower
a       b
a       c
a       b
b       a
b       c

我正在尝试执行 mapreduce,在应用 mapreduce 后,我可以得到如下输出:

user   follower  counts
a         b        2
a         c        1
b         a        1
b         c        1

我是一个使用 mapreduce 概念并在命令行中用 vim 编写 python 脚本的初学者。这是我必须得到的输出,但是得到了ValueError: too many values to unpack

import sys
from collections import Counter

counts= Counter('user')   

for line in sys.stdin: 
    data = line.strip()
    user, follower = data
    counts[line] += 1
    sys.stdout.write("{0}\t{1}\t{2}\n".format(user,follower,counts))

任何帮助或建议将不胜感激。

【问题讨论】:

    标签: python hadoop mapreduce


    【解决方案1】:

    此错误出现在user, follower = data 行中。在这里,它希望您输入两个值,以便它可以像user, follower = data[0],data[1] 一样解压。这样user = data[0]follower=data[1]。希望你能理解。

    这类似于a,b = x,y,因此 a 可以具有值 x 并且 b 可以具有值 y .

    【讨论】:

      【解决方案2】:

      一些见解 -

      • 当您在循环内打印sys.stdout.write("{0}\t{1}\t{2}\n".format(user,follower,counts)) 时,它将为所有数据打印counts 的当前值。
      • 当你初始化counts= Counter('user')时,结果是Counter({'u': 1, 's': 1, 'e': 1, 'r': 1})
      • line.strip() 之后,data 只是一个没有多余空格的字符串。您需要拆分它才能将其分配给userfollower'

      一种可能的解决方案 -

      counts= Counter()   
      
      for line in sys.stdin: 
          data = line.strip()
          user, follower = data.split("\t")
          counts[(user, follower)] += 1
      
      for (user, follower), cnt in counts.items()
          print(f"{user}\t{follower}\t{cnt}")
      

      【讨论】:

        【解决方案3】:
        data=line.split(" ")
        user, follower = data[0], data[1]
        

        在 mapreduce 中,我们通常使用 2 个文件。在每个用户关注者对的映射文件输出 1 中,在 reduce 文件中执行聚合。

        【讨论】:

        • 感谢您的建议,但我试过了,仍然收到错误“IndexError: list index out of range”
        猜你喜欢
        • 2017-01-14
        • 1970-01-01
        • 2019-06-10
        • 1970-01-01
        • 2017-01-24
        • 1970-01-01
        • 1970-01-01
        • 2013-03-28
        • 1970-01-01
        相关资源
        最近更新 更多