为什么我不能在 Spark 中使用 combineByKey?

Why can't I use combineByKey in Spark?

我在 Spark 2.4.5 中编写这段代码:

df_join is a dataframe.    

var comByKeyResult: Dataset[((String, String), (Double, Int))] = df_join
      .map(x => ((x(1).toString, x(3).toString), (x(9).toString.toDouble, x(1).toString.toInt)))

当我尝试编写 comByKeyResult.combineByKey, 时,方法 combineByKey 不可用。为什么?

我导入了以下库:导入 org.apache.spark.rdd._。我是否必须添加其他库或包?

combineByKey是对PairRDD

的转换操作

您需要将 dataframe/dataset 转换为 rdd,然后将其映射到 pairRdd 在你的情况下,只是一个小的改变:

val yourPairRdd = df_join
.rdd
.map(x => ((x(1).toString, x(3).toString), (x(9).toString.toDouble, x(1).toString.toInt)))

//yourPairRdd.combineByKey