计算每年提供的评分记录数

发布于 2025-02-07 23:14:49 字数 739 浏览 3 评论 0原文

我的映射器是:

import sys

for line in sys.stdin:
    # split line into the four fields
    fields = line.strip().split("\t")

    value = fields[2] #rating
    key = fields[3] #timestamp in unix seconds
    
    print(key, value, sep="\t")

我的还原器是:

import sys
                                     
(last_key , count) = (None, 0)

for line in sys.stdin:

    (key, value) = line.strip().split("\t")
   
    if  (last_key  and last_key  !=key):
          print(last_key, count, sep="\t")
          count=0
    
    last_key  = key
    count += int(value)
    
          
print(last_key, count, sep="\t")  

如何获得评级数?映射器工作正常。我什么时候应该转换时间戳(在这种情况下为last_key)

输出应为(年度\ t评级记录的数量)

My mapper is:

import sys

for line in sys.stdin:
    # split line into the four fields
    fields = line.strip().split("\t")

    value = fields[2] #rating
    key = fields[3] #timestamp in unix seconds
    
    print(key, value, sep="\t")

My reducer is:

import sys
                                     
(last_key , count) = (None, 0)

for line in sys.stdin:

    (key, value) = line.strip().split("\t")
   
    if  (last_key  and last_key  !=key):
          print(last_key, count, sep="\t")
          count=0
    
    last_key  = key
    count += int(value)
    
          
print(last_key, count, sep="\t")  

How to I get the number of ratings? The mapper works fine. And when should I convert the timestamp (last_key in this case)

Output should be (year-month \t number of rating records)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯 2025-02-14 23:14:49

如果您想减少(分组)年度字符串,那需要是映射器的钥匙,而不是具有第二个精确效果。

之后,您只需要计算还原器中该键的值数量即可。 (映射器的值只能是1,而不是实际值,如果您只需要 count ,则还原器可以sum值)

我建议使用 而不是从sys.stdin中手动阅读,或者您可以在Pyspark中重写代码,并以更少的行进行相同的操作。

If you want to reduce (group by) the year-month string, that needs to be the key of the mapper, rather than having second-precision.

After that, you just need to count the number of values for that key in the reducer. (The mapper's value can just be 1, not the actual value, if you only need to count the ratings, then the reducer can sum the values)

I'd recommend using mrjob library rather than manually reading from sys.stdin, or you can re-write your code in PySpark and do the same operation in less lines.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文