同一查询中的火花计数和过滤计数

Geo*_*ler 2 sql count apache-spark apache-spark-sql

在 SQL 中类似

SELECT  count(id), sum(if(column1 = 1, 1, 0)) from groupedTable
Run Code Online (Sandbox Code Playgroud)

可以制定为在单次通过中对总记录和过滤记录进行计数。

如何在 spark-data-frame API 中执行此操作?即无需将计数之一连接回原始数据框。

zer*_*323 9

count用于两种情况:

df.select(count($"id"), count(when($"column1" === 1, true)))
Run Code Online (Sandbox Code Playgroud)

如果列是,nullable您应该对此进行更正(例如使用coalesceIS NULL,取决于所需的输出)。