Scala 如何根据映射结果过滤spark RDD?

Scala 如何根据映射结果过滤spark RDD?,scala,apache-spark,filter,functional-programming,set,Scala,Apache Spark,Filter,Functional Programming,Set,我需要根据映射结果过滤RDD。 最初,我有诊断的RDD: 诊断(000140966-012008-07-06250.00) 诊断(202009464-012009-09-29,V70.0) 诊断(202009464-012009-09-29590.80) 诊断(818009099-012014-12-11592.0) 诊断(545360000-012005-12-09584.9) 诊断(000012631-012013-09-23,V70.0) 诊断(666071437-012006-11-29

我需要根据映射结果过滤RDD。 最初,我有诊断的RDD:

诊断(000140966-012008-07-06250.00) 诊断(202009464-012009-09-29,V70.0) 诊断(202009464-012009-09-29590.80) 诊断(818009099-012014-12-11592.0) 诊断(545360000-012005-12-09584.9) 诊断(000012631-012013-09-23,V70.0) 诊断(666071437-012006-11-29496) 诊断(000681687-012006-06-28250.01) 诊断(497910000-012009-04-07584.9) 诊断(022001344-012011-11-28584.9) 诊断(285060000-012-03-28584.9)

其中: 病例分类诊断(patientID:String,日期:date,代码:String)

我将患者分组:

val grouped_patients = diagnostic.groupBy(_.patientID)
grouped_patients.take(50).foreach(println)
(000644947-01,CompactBuffer(诊断(000644947-012010-09-22584.9),诊断(000644947-012007-02-02584.9),诊断(000644947-012014-06-15250.01),诊断(000644947-012009-01-02250.01),…) (000124665-01,CompactBuffer(诊断(000124665-012006-09-05,V70.0),诊断(000124665-012011-11-21585.9),诊断(000124665-012009-10-14585.9),…)

我需要筛选出一些特定代码的患者(我有一组代码T1DM_DX)

我可以说:

val grouped_patient_fil_1 = diagnostic.groupBy(_.patientID)
    .map(x => x._2.map(y => y.code))
    .map(x=>x.toSet.intersect(T1DM_DX).size>0)
    .take(100).foreach(println)
。。。 假的 假的 假的 真的 假的 真的 假的 真的 假的 假的 假的

我如何筛选我们有“True”的分组患者? 我认为应该是这样的:

val grouped_patient_fil_1 = grouped_patients
    .filter(x => x._2.map(y => y.code)
          .map(x=> x.toSet.intersect(T1DM_DX).size>0))
但我得到了一个错误:

T2dmPhenotype.scala:71:37: type mismatch;
[error]  found   : scala.collection.immutable.Set[String]
[error]  required: scala.collection.GenSet[Any]
[error] Note: String <: Any, but trait GenSet is invariant in type A.
[error] You may wish to investigate a wildcard type such as `_ <: Any`. (SLS 3.2.10)
[error]         .map(x => x.toSet.intersect(T1DM_DX).size > 0))
t2dm表型。scala:71:37:类型不匹配;
找到[错误]:scala.collection.immutable.Set[String]
[错误]必需:scala.collection.GenSet[任何]
[错误]注意:字符串0)

如果您已经有一个包含
布尔
对象的数组,那么只需将
映射
更改为流中的
过滤器
,这将只留下真值:

val grouped_patient_fil_1 = diagnostic
    .groupBy(_.patientID)
    .filter(x => x._2.map(y => y.code).toSet.intersect(T1DM_DX).size>0)
grouped_patient_fil_1.take(100).foreach(println)