[英]Spark request max count
我是spark的初学者,我尝试发出请求,允许我检索访问量最大的网页。
我的要求如下
mostPopularWebPageDF = logDF.groupBy("webPage").agg(functions.count("webPage").alias("cntWebPage")).agg(functions.max("cntWebPage")).show()
使用此请求,我只检索具有最大计数的数据框,但我想检索具有此分数的数据框和保存此分数的网页
像这样的东西:
webPage max(cntWebPage)
google.com 2
我该如何解决我的问题?
非常感谢。
在pyspark + sql中:
logDF.registerTempTable("logDF")
mostPopularWebPageDF = sqlContext.sql("""select webPage, cntWebPage from (
select webPage, count(*) as cntWebPage, max(count(*)) over () as maxcnt
from logDF
group by webPage) as tmp
where tmp.cntWebPage = tmp.maxcnt""")
也许我可以让它变得更干净,但它确实有效。 我会尽力优化它。
我的结果:
webPage cntWebPage
google.com 2
对于数据集:
webPage usersid
google.com 1
google.com 3
bing.com 10
说明:通过分组+计数(*)函数完成正常计数。 所有这些计数中的最大值是通过窗口函数计算的,因此对于上面的数据集,立即DataFrame /而不删除maxCount列/:
webPage count maxCount
google.com 2 2
bing.com 1 2
然后我们选择count等于maxCount的行
编辑:我已经删除了DSL版本 - 它不支持window over()并且订购正在改变结果。 对不起,这个bug。 SQL版本是正确的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.