【发布时间】:2021-11-05 10:36:08
【问题描述】:
我想用 Spark SQL 和 DataFrame 复制 Pandas nunique 函数。我有以下内容:
%spark
import org.apache.spark.sql.functions.countDistinct
import org.apache.spark.sql.functions._
val df = spark.read
.format("csv")
.option("delimiter", ";")
.option("header", "true") //first line in file has headers
.load("target/youtube_videos.csv")
println("Distinct Count: " + df.distinct().count())
val df2 = df.select(countDistinct("likes"))
df2.show(false)
这有效并打印了喜欢列的唯一计数,如下所示:
Distinct Count: 109847
+---------------------+
|count(DISTINCT likes)|
+---------------------+
|27494 |
+---------------------+
如何在一个 SQL 中执行此操作,以便获得所有单独列的摘要?
【问题讨论】:
标签: scala apache-spark