地图操作中的Scala类型不匹配

时间:2019-01-18 15:18:16

标签: scala apache-spark spark-streaming

我正在以下代码中尝试在Spark DStream上执行映射操作:

val hashesInRecords: DStream[(RecordKey, Array[Int])] = records.map(record => {
      val hashes: List[Int] = calculateIndexing(record.fields())
      val ints: Array[Int] = hashes.toArray(Array.ofDim[Int](hashes.length))
      (new RecordKey(record.key, hashes.length), ints)
    })

代码在IntelliJ中看起来不错,但是当我尝试构建时,出现了一个我不太了解的错误:

Error:(53, 61) type mismatch;
 found   : Array[Int]
 required: scala.reflect.ClassTag[Int]
      val ints: Array[Int] = hashes.toArray(Array.ofDim[Int](hashes.length))

即使像这样在地图操作中添加了类型,此错误仍然存​​在:

records.map[(RecordKey, Array[Int])](record => {...

1 个答案:

答案 0 :(得分:1)

这应该可以解决您的问题,并且避免调用{strong> O( N )的List.length,而使用Array.length代替是 O( 1

val hashesInRecords: DStream[(RecordKey, Array[Int])] = records.map { record =>
  val ints = calculateIndexing(record.fields()).toArray
  (new RecordKey(record.key, ints.length), ints)
}