【发布时间】:2019-07-16 01:18:05
【问题描述】:
我有一个对象的 Rdd“labResults”:
case class LabResult(patientID: String, date: Long, labName: String, value: String)
我想转换这个 rdd,使它只包含一个用于每个 patientID 和 labName 组合的行。此行应该是该患者 ID 和 labName 组合的最新行(我只对患者进行此实验室的最新日期感兴趣)。我是这样做的:
//group rows by patient and lab and take only the last one
val cleanLab = labResults.groupBy(x => (x.patientID, x.labName)).map(_._2).map { events =>
val latest_date = events.maxBy(_.date)
val lab = events.filter(x=> x.date == latest_date)
lab.take(1)
}
我想从这个 RDD 创建边缘:
val edgePatientLab: RDD[Edge[EdgeProperty]] = cleanLab
.map({ lab =>
Edge(lab.patientID.toLong, lab2VertexId(lab.labName), PatientLabEdgeProperty(lab).asInstanceOf[EdgeProperty])
})
我得到一个错误:
value patientID is not a member of Iterable[edu.gatech.cse6250.model.LabResult]
[错误] 边缘(lab.patientID.toLong,lab2VertexId(lab.labName),PatientLabEdgeProperty(lab).asInstanceOf[EdgeProperty]) [错误] ^ [错误] /hw4/stu_code/src/main/scala/edu/gatech/cse6250/graphconstruct/GraphLoader.scala:94:53:值labName不是Iterable的成员[edu.gatech.cse6250.model.LabResult] [错误] 边缘(lab.patientID.toLong,lab2VertexId(lab.labName),PatientLabEdgeProperty(lab).asInstanceOf[EdgeProperty]) [错误] ^ [错误] /hw4/stu_code/src/main/scala/edu/gatech/cse6250/graphconstruct/GraphLoader.scala:94:86:类型不匹配; [错误] 发现:Iterable[edu.gatech.cse6250.model.LabResult] [错误] 必需:edu.gatech.cse6250.model.LabResult [错误] Edge(lab.patientID.toLong, lab2VertexId(lab.labName), PatientLabEdgeProperty(lab).asInstanceOf[EdgeProperty])
所以,看起来问题是“cleanLab”也不是我预期的 LabResult 的 RDD,而是 Iterable[edu.gatech.cse6250.model.LabResult] 的 RDD [edu.gatech.cse6250.model.LabResult]
我该如何解决?
【问题讨论】:
-
你可以用
lab.head代替lab.take(1)。
标签: scala apache-spark rdd