在同一查询中的火花计数和过滤后的计数 [英] spark count and filtered count in same query
本文介绍了在同一查询中的火花计数和过滤后的计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在SQL中,类似
SELECT count(id), sum(if(column1 = 1, 1, 0)) from groupedTable
可以制定为一次记录总数和已过滤记录的计数.
could be formulated to perform a count of the total records as well as filtered records in a single pass.
如何在spark-data-frame API中执行此操作?即无需将计数之一重新加入到原始数据帧中.
How can I perform this in spark-data-frame API? i.e. without needing to join back one of the counts to the original data frame.
推荐答案
在两种情况下都请使用count
:
Just use count
for both cases:
df.select(count($"id"), count(when($"column1" === 1, true)))
如果列是nullable
,则应对此进行更正(例如,使用coalesce
或IS NULL
,具体取决于所需的输出).
If column is nullable
you should correct for that (for example with coalesce
or IS NULL
, depending on the desired output).
这篇关于在同一查询中的火花计数和过滤后的计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文