我试图在reduceByKey
结果上执行reduceByKey
。目标是看看我们每年是否有长尾效应-a长尾在这里的意思是我想看到的每年(分别),如果今年65%或更多的销售额来自20%或更少的产品。
这是我的数据集:数据集-年份和asin(它是一个ID)
我想先减少一年,然后每年(分别)减少一年。因此,我会得到每年,每个产品发生了多少次。
我试着这样做:
data_rdd.map(lambda x: (x.Year,(x.asin,1))).groupByKey().mapValues(list).sortBy(lambda x: x[0]).map(lambda x: x[1])
但我不明白如何对每一行都执行reducebykey
谢谢
1条答案
按热度按时间z4iuyo4d1#
在这种情况下,我会使用
SparkSQL
API,因为windows将非常有用。对于每一年,让我们计算实现至少65%销售额所需的产品百分比:现在,让我们使用windows来计算回答您的问题所需的内容:
product_count
total_sales
cum_sales
product_index From there,
product_peris the percentage of product,
sales_perthe percentage of sales so that we can see if at least 65% of the sales were made by less than 20% of the products. We can finally compute
dist',销售百分比与65%之间的距离。我们将使用该列保留允许达到65%以上销售额的第一行。一个二个一个一个
因此,在本例中,我们需要40%的产品(5个中的2个)才能达到至少65%的销售额,我们只保留这一行:
这将持续一年以上-)