我是 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
说明:正常计数是通过分组+count(*)函数完成的。所有这些计数的最大值是通过窗口函数计算的,因此对于上面的数据集,直接 DataFrame /不删除 maxCount 列/是:
webPage count maxCount
google.com 2 2
bing.com 1 2
然后我们选择计数等于 maxCount 的行
编辑:我已经删除了 DSL 版本 - 它不支持 window over () 并且排序正在改变结果。对此错误感到抱歉。 SQL版本正确
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)