GraphX笔记

来源:互联网 发布:南充广电网络客服电话 编辑:程序博客网 时间:2024/06/05 07:30
val graph=GraphLoader.edgeListFile(sc,"/home/spark/spark/graphx/data/followers.txt")//加载边时顶点是边上出现的点,定点默认数据是1,注意文件格式:1 2,中间是空格graphx只会读取两列分别作为源顶点和目标顶点,如:1 2 other,第三列的other直接被忽略val users = sc.textFile("/home/spark/spark/graphx/data/users.txt").map { line=>val fields = line.split(",")    (fields(0).toLong,(fields(1),fields(2)))//解析顶点数据:ID(一定转成Long型),fisrt name,full name        } val myGraph=Graph.apply(users,graph.edges)//由于graph默认将顶点数据设为1,将顶点数据users和边数据graph.edges重构为新图,如果边edges中的顶点A在顶点集合users中没有,则该顶点A将会以默认值初始化,可以添加默认值   如:val defaultUsers=("first name","full name")      val myGraph=Graph.apply(users,graph.edges,defaultUsers)graph.vertices.filter{case(id,(firstName,fullName)=>firstName==”BarackObama”}//针对每个顶点进行filter操作,id是顶点ID,(firstName,fullName)是顶点数据graph.edges.filter{case(src,dst,prop)=>prop==1}//边数据的过滤,src表示源顶点ID,dst表示目标顶点ID,prop表示边上数据graph.triplet.map(tri=>tri.srcId+” “+tri.srcAttr+” “+tri.dstId+” “+tri.dstAttr+” “+tri.attr+”\n”) //每个triplet进行map操作,srcId,srcAttr,dstId,dstAttr,attr分别表示源顶点标号ID、源顶点数据、目标顶点ID、目标顶点数据、边数据graph.vertices.mapValues[Int]((id:VertexId,attr:(String,String))=>10)//针对VertexRDD操作graph.vertices.saveAsTextFile(“pathToFile”) //顶点数据存入图文件,参数是一个目录,类似graph.edges.saveAsTextFilegraph.mapVertices[VD2])((id:VertexId,attr:VD)=>VD2) //VD2要和图的VD匹配(VD是图的顶点数据不包括ID,ED是边的数据不包括srcID和dstID)如:myGraph.mapVertices[(String,String)]((id,(fistName,fullName))=>(firstName.toUpperCase,fullName))//将顶点数据的firstName改为大写graph.mapEdges(e=>e.attr+10) //针对每条边进行map操作并返回边集合,e的类型类似于(src,dst,prop)包含了源顶点ID,目标顶点ID,边数据graph.mapTriplets(triplet=>triplet.attr+10) //针对图中每个triplet进行map操作graph.reverse //所有边反向graph.mask[VD2,ED2](other:Graph[VD2,ED2]):Graph[Vd,ED]//返回graph和other的交集graph.subgraph(edge=>true,(id,prop)=>prop==1)//返回边满足条件(这里是true全部边都满足)及顶点数据为1的子图graph.joinVertices(other:RDD[(VertexId,U))(map:(VertexId,VD,U)=>VD):Graph[VD,ED] //graph和other相交的顶点执行map函数,在graph中但是不在other中的地点保持不变graph.outerJoinVertices[U,VD2](other:RDD[(VertexId,U))(map:(VertexId,VD,option)=>VD2):Graph[VD2,ED] //和joinVertices类似,但不同的是在graph中但不在other中的顶点也要执行map函数graph.mapReduceTriplets[A](map:EdgeTriplet[VD,ED]=>Iterator[(VertexId,A)],reduce:(A,A)=>A):VertexRDD[A] //针对每个triplet执行map函数(发送消息,暂时只能单向传递消息,即所有的triplet执行map时要么都是想源顶点发送消息,要么都向目标顶点发送消息),并由reduce收集发送给顶点的消息graph.inDegrees //返回图的入度,类型为VertexRDD[Int] graph.outDegrees //返回出度,(VertexID,Int)graph.Degrees //返回顶点的度graph.collectNeighbors(edgeDirection:EdgeDirection):VertexRDD[Array[(VertexId,VD)]] //收集每个顶点的邻居顶点数据,返回的是一个数组,数组元素是邻居顶点ID和其顶点数据EdgeDirection.Out //出边方向EdgeDirection.In //入边方向EdgeDirection.Either //出边或入边方向EdgeDirection.Both //出边和入边方向graph.pregel[A]      (        initialMsg:A,//初始消息maxIter:Int=Int.MaxValue,//最大迭代次数activeDir:EdgeDirection=EdgeDirection.Out      )//消息传递方向      (        vprog:(VertexId,VD,A)=>VD,//顶点程序        sendMsg:EdgeTriplet[VD,ED]=>Iterator[(VertexId,A)],//发送消息        mergeMsg:(A,A)=>A      ):Graph[VD,ED]//汇集消息graph.pageRank(0.0001) //计算PageRank值,针对非联通图也可以前面我所引用的数据格式如下:users.txt:  第一列为ID,第二列为fistName,第二列为fullName 1,BarackObama,Barack Obama2,ladygaga,Goddess of Love3,jeresig,John Resig4,justinbieber,Justin Bieber6,matei_zaharia,Matei Zaharia7,odersky,Martin Odersky8,anonsys,xxoofollowers.txt如下:2 14 11 26 37 37 66 73 7

0 0