【发布时间】:2021-11-23 02:16:09
【问题描述】:
我正在尝试按键分组并在 scala 中汇总值!
但是当我执行以下操作时,我得到的返回类型是 List[(String, Long)] 而不是 List[InputRecord]
case class InputRecord(FeeDescription: String, FeeAmount: Long)
val reconInput : List[InputRecord] = List(InputRecord("Visa Auth Fee", 30), InputRecord("Visa Auth Fee", 40),
InputRecord("Master Network Fee", 50))
我尝试过的命令
reconInput.groupBy(_.FeeDescription).mapValues(_.foldLeft(0L)(_+_.FeeAmount)).toList
我在结果中得到了预期的数据,但列表的类型与我的预期不同
List(InputRecord("Visa Auth Fee", 70), InputRecord("Master Network Fee", 50))
但我得到的返回类型为
List[(String, Long)]
而不是
List[InputRecord]
当我尝试使用以下命令将列表转换为预期的列表时
val outputRecord = reconInput.groupBy(_.FeeDescription).mapValues(_.foldLeft(0L)(_+_.FeeAmount)).toList.asInstanceOf[ReconRecord]
我收到类转换异常
Exception in thread "main" java.lang.ClassCastException: scala.collection.immutable.$colon$colon cannot be cast to
Sample$InputRecord
【问题讨论】:
标签: scala apache-spark arraylist spark-streaming scala-collections