scala: how to rectify “option” type after leftOuterJoin

喜你入骨 提交于 2020-03-04 15:37:52

问题


Given

scala> val rdd1 = sc.parallelize(Seq(("a",1),("a",2),("b",3)))
scala> val rdd2 = sc.parallelize(Seq("a",5),("c",6))
scala> val rdd3 = rdd1.leftOuterJoin(rdd2)
scala> rdd3.collect()
res: Array[(String, (Int, Option[Int]))] = Array((a,(1,Some(5))), (a,(2,Some(5))), (b,(3,None)))

We can see that the data type of "Option[Int]" in rdd3. Is there a way to rectify this so that rdd3 can be Array[String, (Int, Int)]? Suppose we can specify a value (e.g. 999) for the "None".


回答1:


scala> val result = rdd3.collect() 
scala> result.map(t => (t._1, (t._2._1, t._2._2.getOrElse(999))))

This should do it.



来源:https://stackoverflow.com/questions/60124121/scala-how-to-rectify-option-type-after-leftouterjoin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!