import org.apache.spark.graphx._
import org.apache.spark.graphx.lib._
import org.apache.spark.rdd.RDD
val user=Array((1l,("Lakshman","vinukonda")),
| (2l,("Ashok","Vijayawada")),
| (3l,("Sai","Markapur")),
| (4l,("gnr","guntur")),
| (5l,("srinu","vijayawada")),
| (6l,("mahesh","guntur")))
val rship=Array(Edge(1l,2l,"Bfnds"),
| Edge(1l,5l,"sameBatch"),
| Edge(3l,5l,"badFnds"),
| Edge(1l,4l,"sameBatch"),
| Edge(4l,6l,"brother"))
val ruser=sc.parallelize(user)
val rrship=sc.parallelize(rship)
val grap=Graph(ruser,rrship)
grap.vertices.collect
grap.edges.collect
Filtering Vertices From Graph
-----------------------------------------------------
val res=grap.vertices.filter{x=>
| val id=x._1
| val name=x._2._1
| val loc=x._2._2
| loc=="guntur"
| }
val res1=grap.vertices.filter(x=>x._2._2=="guntur")
val res2=grap.vertices.filter(x=>(x._2._2).toLowerCase=="vijayawada")
val res3=grap.vertices.filter{case(id,(name,loc))=>loc=="guntur"}
Filtering Edges From Graph
---------------------------------------------------------
grap.edges.filter(x=>x.dstId==3l).collect
grap.edges.filter(x=>x.attr=="Bfnds").collect
// Note:Edge(srcId,dstId,attr)
Triplets
-------------------------------------------------------------
grap.triplets.collect
grap.triplets.map(x=>(x.srcId,x.srcAttr._1,x.srcAttr._2,x.dstId,x.dstAttr._1,x.dstAttr._2,x.attr)).collect
val fres=grap.triplets.map(x=>x.srcAttr._1+" and "+x.dstAttr._1 +" are located in "+x.srcAttr._2+" , "+x.dstAttr._2)
fres.collect.foreach(println)
val fres1=grap.triplets.map{x=>
| if (x.srcAttr._2==x.dstAttr._2)
| x.srcAttr._1+" and "+x.dstAttr._1 +" were located in "+x.srcAttr._2
| else
| x.srcAttr._1+" and "+x.dstAttr._1 +" are located in "+x.srcAttr._2+" , "+x.dstAttr._2 }
fres1.collect.foreach(println)
fres.saveAsTextFile("/user/cloudera/sparkgrapx") //3 Partitions
val res5=sc.parallelize(fres.collect)
res5.saveAsTextFile("/user/cloudera/sparkgrapx1"
import org.apache.spark.graphx.lib._
import org.apache.spark.rdd.RDD
val user=Array((1l,("Lakshman","vinukonda")),
| (2l,("Ashok","Vijayawada")),
| (3l,("Sai","Markapur")),
| (4l,("gnr","guntur")),
| (5l,("srinu","vijayawada")),
| (6l,("mahesh","guntur")))
val rship=Array(Edge(1l,2l,"Bfnds"),
| Edge(1l,5l,"sameBatch"),
| Edge(3l,5l,"badFnds"),
| Edge(1l,4l,"sameBatch"),
| Edge(4l,6l,"brother"))
val ruser=sc.parallelize(user)
val rrship=sc.parallelize(rship)
val grap=Graph(ruser,rrship)
grap.vertices.collect
grap.edges.collect
Filtering Vertices From Graph
-----------------------------------------------------
val res=grap.vertices.filter{x=>
| val id=x._1
| val name=x._2._1
| val loc=x._2._2
| loc=="guntur"
| }
val res1=grap.vertices.filter(x=>x._2._2=="guntur")
val res2=grap.vertices.filter(x=>(x._2._2).toLowerCase=="vijayawada")
val res3=grap.vertices.filter{case(id,(name,loc))=>loc=="guntur"}
Filtering Edges From Graph
---------------------------------------------------------
grap.edges.filter(x=>x.dstId==3l).collect
grap.edges.filter(x=>x.attr=="Bfnds").collect
// Note:Edge(srcId,dstId,attr)
Triplets
-------------------------------------------------------------
grap.triplets.collect
grap.triplets.map(x=>(x.srcId,x.srcAttr._1,x.srcAttr._2,x.dstId,x.dstAttr._1,x.dstAttr._2,x.attr)).collect
val fres=grap.triplets.map(x=>x.srcAttr._1+" and "+x.dstAttr._1 +" are located in "+x.srcAttr._2+" , "+x.dstAttr._2)
fres.collect.foreach(println)
val fres1=grap.triplets.map{x=>
| if (x.srcAttr._2==x.dstAttr._2)
| x.srcAttr._1+" and "+x.dstAttr._1 +" were located in "+x.srcAttr._2
| else
| x.srcAttr._1+" and "+x.dstAttr._1 +" are located in "+x.srcAttr._2+" , "+x.dstAttr._2 }
fres1.collect.foreach(println)
fres.saveAsTextFile("/user/cloudera/sparkgrapx") //3 Partitions
val res5=sc.parallelize(fres.collect)
res5.saveAsTextFile("/user/cloudera/sparkgrapx1"
nice blog
ReplyDelete