mapVertices
节点与边的变换操作
def mapVertices[VD2](map: (VertexID, VD) => VD2): Graph[VD2, ED]
def mapEdges[ED2](map: Edge[ED] => ED2): Graph[VD, ED2]
def mapEdges[ED2](map: (PartitionID, Iterator[Edge[ED]]) => Iterator[ED2]): Graph[VD, ED2]
def mapTriplets[ED2](map: EdgeTriplet[VD, ED] => ED2): Graph[VD, ED2]
def mapTriplets[ED2](map: (PartitionID, Iterator[EdgeTriplet[VD, ED]]) => Iterator[ED2]) : Graph[VD, ED2]
package graphx import org.apache.log4j.{Level, Logger} import org.apache.spark.graphx._ import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} //转换操作 object test03 { def main(args:Array[String]){ //屏蔽日志 Logger.getLogger("org.apache.spark").setLevel(Level.WARN) Logger.getLogger("org.eclipse.jetty.server").setLevel(Level.OFF) //设置运行环境 val conf: SparkConf = new SparkConf().setAppName("my graphx").setMaster("local") val sc = new SparkContext(conf) //TODO 构造vertextArray和edgeArray //顶点的数据类型 VD:(String,Int) val vertexArray = Array( (1L, ("Alice", 28)), (2L, ("Bob", 27)), (3L, ("Charlie", 65)), (4L, ("David", 42)), (5L, ("Ed", 55)), (6L, ("Fran", 50)) ) //边的数据类型ED:(Int) val edgeArray = Array( Edge(2L, 1L, 7), Edge(2L, 4L, 2), Edge(3L, 2L, 4), Edge(3L, 6L, 3), Edge(4L, 1L, 1), Edge(5L, 2L, 2), Edge(5L, 3L, 8), Edge(5L, 6L, 3) ) //TODO 构造vertextRDD和edgeRDD val vertextRDD: RDD[(VertexId, (String, PartitionID))] = sc.makeRDD(vertexArray) val edgeRDD: RDD[Edge[PartitionID]] = sc.makeRDD(edgeArray) //TODO 构造graph val graph:Graph[(String,Int),Int] = Graph(vertextRDD,edgeRDD) println("顶点的转换操作,顶点age + 10:") // val vertices: VertexRDD[(VertexId, (String, PartitionID))] = graph.mapVertices({ // case (id, (name, age)) => (id, (name, age + 10)) // }).vertices val graphVertex: Graph[(VertexId, (String, PartitionID)), PartitionID] = graph.mapVertices({ case (id, (name, age)) => (id, (name, age + 10)) }) val vertices: VertexRDD[(VertexId, (String, PartitionID))] = graphVertex.vertices vertices.collect().foreach({ case (vertexId, (id, (name,age))) => println(id, name, age) }) println("边的转换操作,边的属性*2:") // val edges: EdgeRDD[PartitionID] = graph.mapEdges(e=>e.attr*2).edges val graphEdge: Graph[(String, PartitionID), PartitionID] = graph.mapEdges(e=>e.attr*2) val edges: EdgeRDD[PartitionID] = graphEdge.edges edges.collect().foreach(e=>println(e.srcId,e.dstId,e.attr)) edges.collect().foreach({ case Edge(src,dst,attr)=>println(src,dst,attr) }) println("triplets的转换操作,triplets的属性*2:") // val triplets: RDD[EdgeTriplet[(String, PartitionID), PartitionID]] = graph.mapTriplets({ // t => t.attr * 2 // }).triplets val graphTriplets: Graph[(String, PartitionID), PartitionID] = graph.mapTriplets({ t => t.attr * 2 }) val triplets: RDD[EdgeTriplet[(String, PartitionID), PartitionID]] = graphTriplets.triplets triplets.collect().foreach( t=>println(t.srcId,t.srcAttr._1,t.srcAttr._2,t.attr,t.dstId,t.dstAttr) ) } }
posted on 2020-10-27 18:04 happygril3 阅读(149) 评论(0) 收藏 举报
浙公网安备 33010602011771号