concept two graph in category graphx

This is an excerpt from Manning's book Spark GraphX in Action.
Unfortunately, LPA often doesn’t converge. Figure 5.11 shows an example of non-convergence—the graph in step 5 is the same as in step 3, and the algorithm continues forever ping-ponging between the two graphs that look like steps 4 and 5. For that reason, GraphX only provides a static version that runs for a number of iterations you specify and doesn’t provide a dynamic version with a tolerance-terminating condition.
This section lays the groundwork for the next, which is about reading RDF files. Before reading RDF files, though, we need to look at a couple of basic graph operations that GraphX doesn’t provide (as of Spark 1.6). The first is taking subgraphs in a common sense fashion, and the second is merging two graphs together.
If you have two graphs with vertex type of String, it’s sometimes useful to merge two such graphs on common vertex names, as shown in figure 8.2. Merging means treating equally named vertices from the two graphs as the same vertex and merging the adjacent edges together for those vertices. All the other vertices and edges that are unique to one graph or the other are also brought in. This is useful if you have two graphs constructed from two different data sources and you want to make one big graph out of them to do further analysis.
Figure 8.2. Merging two graphs together based on commonly named vertices is a useful operation that isn’t built into GraphX (as of Spark 1.6).
![]()
The next listing shows how it can be done. The function takes two graphs that we want to merge and returns a single graph with any common vertices merged.
Listing 8.3. Merge two graphs into one
import org.apache.spark.graphx._ def mergeGraphs(g1:Graph[String,String], g2:Graph[String,String]) = { val v = g1.vertices.map(_._2).union(g2.vertices.map(_._2)).distinct .zipWithIndex def edgesWithNewVertexIds(g:Graph[String,String]) = g.triplets .map(et => (et.srcAttr, (et.attr,et.dstAttr))) .join(v) .map(x => (x._2._1._2, (x._2._2,x._2._1._1))) .join(v) .map(x => new Edge(x._2._1._1,x._2._2,x._2._1._2)) Graph(v.map(_.swap), edgesWithNewVertexIds(g1).union(edgesWithNewVertexIds(g2))) }