Spark 请求最大计数
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
说明:正常的计数是通过分组+count(*)函数完成的。所有这些计数的最大值是通过 window 函数计算的,因此对于上面的数据集,直接 DataFrame /不删除 maxCount 列/是:
webPage count maxCount
google.com 2 2
bing.com 1 2
然后我们 select 行计数等于 maxCount
编辑:我已经删除了 DSL 版本 - 它不支持 window over () 并且排序正在改变结果。对不起这个错误。 SQL 版本正确
我是 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(*)函数完成的。所有这些计数的最大值是通过 window 函数计算的,因此对于上面的数据集,直接 DataFrame /不删除 maxCount 列/是:
webPage count maxCount
google.com 2 2
bing.com 1 2
然后我们 select 行计数等于 maxCount
编辑:我已经删除了 DSL 版本 - 它不支持 window over () 并且排序正在改变结果。对不起这个错误。 SQL 版本正确