scala - 我如何在 Spark/Scala 中使用 countDistinct?

标签 scala apache-spark dataframe

我正在尝试使用 Scala 聚合 Spark 数据框中的列,如下所示:

import org.apache.spark.sql._

dfNew.agg(countDistinct("filtered"))

但是我得到了错误:

 error: value agg is not a member of Unit

谁能解释一下为什么?

编辑:澄清我正在尝试做的事情: 我有一列是一个字符串数组,我想计算所有行的不同元素,对任何其他列不感兴趣。数据:

+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
|racist|filtered                                                                                                                                                      |
+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------+
|false |[rt, @dope_promo:, crew, beat, high, scores, fugly, frog, 😍🔥, https://time.com/sxp3onz1w8]                                                                      |
|false |[rt, @axolrose:, yall, call, kermit, frog, lizard?, , https://time.com/wdaeaer1ay]                                                                                |

我想计算过滤后的数,给出:

rt:2, @dope_promo:1, crew:1, ...frog:2 etc

最佳答案

您需要先展开您的数组,然后才能计算出现次数:查看每个元素的计数:

dfNew
.withColumn("filtered",explode($"filtered"))
.groupBy($"filtered")
.count
.orderBy($"count".desc)
.show

或者只是为了获取不同元素的数量:

val count = dfNew
.withColumn("filtered",explode($"filtered"))
.select($"filtered")
.distinct
.count

关于scala - 我如何在 Spark/Scala 中使用 countDistinct?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44886831/

相关文章:

scala - 用几列的空值创建DataFrame

r - 将 r 中的数据帧转换为事务或 itemMatrix?

python - 从所有其他行中减去每一行并在 python 中查看为矩阵

Scala 隐式转换和具有值类的 mkNumericOps

apache-spark - spark 中的 RDD 内存占用

scala - 按需 Actor 获取或创建

scala - Spark 窗口分区功能将永远完成

python - Pandas - 比较excel和数据框之间的索引和列以输入值

scala - WholeTextFiles 方法因 ExitCode 52 java.lang.OutOfMemoryError 而失败

scala - 如何记录所有未过滤的请求