繁体   English   中英

如何使用案例类属性将 RDD 转换为其他 RDD?

[英]how to convert a RDD to other RDD using case class property?

我有一个如下名称的 RDD: other_nodes

(4,(1,true))
(22,(1,true))
(14,(1,true))
(3,(1,true))
(8,(1,true))
(18,(1,true))

我写了一个如下的案例类并将其应用在图表上,它给出了我想要的结果:

case class nodes_properties(label:Int, isVisited:Boolean=false)

当我在图表上应用 case 时,其结果如下所示:

(1,nodes_properties(15,false))
(2,nodes_properties(11,false))
(3,nodes_properties(9,false))

问题:如何在other_nodes RDD 上应用我定义的 case 类以获得如下结果:

(4,nodes_properties(1,true))
(22,nodes_properties(1,true))
(14,nodes_properties(1,true))
(3,nodes_properties(1,true))
(8,nodes_properties(1,true))
(18,nodes_properties(1,true))

此解决方案可能有效:

scala> val data = sc.parallelize(Seq((4,(1, true)),(22,(1,true))))
data: org.apache.spark.rdd.RDD[(Int, (Int, Boolean))] = ParallelCollectionRDD[72] at parallelize at <console>:39

scala> data.take(2)
res27: Array[(Int, (Int, Boolean))] = Array((4,(1,true)), (22,(1,true)))

scala> val data1 = data.map(elem => (elem._1, nodes_properties(elem._2._1, elem._2._2)))
data1: org.apache.spark.rdd.RDD[(Int, nodes_properties)] = MapPartitionsRDD[73] at map at <console>:42

scala> data1.take(2)
res28: Array[(Int, nodes_properties)] = Array((4,nodes_properties(1,true)), (22,nodes_properties(1,true)))

编辑

问题是others_rdd中的每个元素都是 Type (VertexId, Any) 您需要转换为(VertexId, (Int, Boolean))类型才能应用您的案例类。 这样做的方法是

val newRdd = others_rdd.map(elem => (elem._1, elem._2.asInstanceOf[(Int,Boolean)]))

执行此操作后,您可以通过映射到node_properties类来应用如上所示的解决方案。

让我知道它是否有帮助!!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM