【发布时间】:2019-10-09 18:57:01
【问题描述】:
我正在重构一个与 Spark 接口的 Scala 库,以便在有意义的地方使用 Vectors。我想提供直接与 Spark 接口的函数,能够使用数组或向量。这些函数之一是转置函数。然而,我似乎无法获得正确的类型签名以使类型推断起作用。
我已尝试以以下方式定义函数签名,但这似乎不起作用,而是在我以字符串向量 RDD 为例运行测试时给我以下有关类型推断的消息。
def transpose[T, Abs <: IndexedSeq[T] : ClassTag](rdd: RDD[Abs]): RDD[Abs] = {
rdd
.zipWithIndex // give the columns an index
.flatMap{
case (row, row_idx) => row.zipWithIndex.map{ // give the rows an index
case (el, col_idx) => (col_idx, (row_idx, el)) // each element now has a column and row index
}
}
.groupBy(_._1)
.sortBy(_._1)
.map{ case (_, els) => els.map(_._2).toIndexedSeq.sortBy(_._1) }
.map( row => row.map(_._2))
.map(_.asInstanceOf[Abs])
}
Error:(26, 5) inferred type arguments [Nothing,scala.collection.immutable.Vector[String]] do not conform to method transpose's type parameter bounds [T,Abs <: IndexedSeq[T]]
transpose(subset)
Error:(26, 15) type mismatch;
found : org.apache.spark.rdd.RDD[scala.collection.immutable.Vector[String]]
required: org.apache.spark.rdd.RDD[Abs]
transpose(subset)
【问题讨论】:
标签: scala apache-spark types type-inference