title: how to build a graph from tuples in graphx and label the nodes after ?
body: <p>Some context can be found <a href="https://stackoverflow.com/questions/31928133/how-to-create-a-graph-from-arrayany-any-using-graph-fromedgetuples/31929807?noredirect=1#comment51792711_31929807">here</a>, the idea is that I have created a graph from tuples collected from a request on a Hive table. Those correspond to trade relations between countries.
  Having built the graph this way, the vertices are not labelled. I want to study the distribution of degrees and get the most connected countries' names. I tried 2 options :</p>
  <ul>
  <li><em>First</em> : I tried to map the index of the vertices with the string names of the vertices with the function idMapbis inside the function which is collecting and printing the ten top connected degrees.</li>
  <li><em>Second</em> : I tried to add label to the vertices of the graph itself.</li>
  </ul>
  <p>In both cases I get the following error : the task is not serializable</p>
  <h3>Global code :</h3>
  <pre><code>import org.apache.spark.SparkContext
  import org.apache.spark.graphx._
  import org.apache.spark.rdd.RDD
  
  val sqlContext= new org.apache.spark.sql.hive.HiveContext(sc)
  val data = sqlContext.sql(&quot;select year, trade_flow, reporter_iso, partner_iso, sum(trade_value_us) from comtrade.annual_hs where length(commodity_code)='2' and not partner_iso='WLD' group by year, trade_flow, reporter_iso, partner_iso&quot;).collect()
  val data_2010 = data.filter(line =&gt; line(0)==2010)
  val couples = data_2010.map(line=&gt;(line(2),line(3))) //pays-&gt;pays 
  </code></pre>
  <p>couples look like this: Array[(Any, Any)] = Array((MWI,MOZ), (WSM,AUS), (MDA,CRI), (KNA,HTI), (PER,ERI), (SWE,CUB),...</p>
  <pre><code>val idMap = sc.broadcast(couples 
  .flatMap{case (x: String, y: String) =&gt; Seq(x, y)}
  .distinct 
  .zipWithIndex  
  .map{case (k, v) =&gt; (k, v.toLong)}  
  .toMap) 
  
  val edges: RDD[(VertexId, VertexId)] = sc.parallelize(couples
  .map{case (x: String, y: String) =&gt; (idMap.value(x), idMap.value(y))})
  
  val graph = Graph.fromEdgeTuples(edges, 1)
  </code></pre>
  <p>built this way, vertices look like (68,1) for example</p>
  <pre><code>val degrees: VertexRDD[Int] = graph.degrees.cache()
  
  //Most connected vertices 
  def topNamesAndDegrees(degrees: VertexRDD[Int], graph: Graph[Int, Int]): Array[(Int, Int)] = {
  val namesAndDegrees = degrees.innerJoin(graph.vertices) {
   (id, degree, k) =&gt; (id.toInt, degree)}
  val ord = Ordering.by[(Int, Int), Int](_._2)
  namesAndDegrees.map(_._2).top(10)(ord)}
  topNamesAndDegrees(degrees, graph).foreach(println)
  </code></pre>
  <p>We get : (79,1016),(64,912),(55,889)...</p>
  <h3>First option to retrieve the names :</h3>
  <pre><code>val idMapbis = sc.parallelize(couples
  .flatMap{case (x: String, y: String) =&gt; Seq(x, y)} 
  .distinct 
  .zipWithIndex  
  .map{case (k, v) =&gt; (v,k)}  
  .toMap)
  
  def topNamesAndDegrees(degrees: VertexRDD[Int], graph: Graph[Int, Int]):  Array[(String, Int)] = {
  val namesAndDegrees = degrees.innerJoin(graph.vertices) {
   (id, degree, name) =&gt; (idMapbis.value(id.toInt), degree)}
  val ord = Ordering.by[(String, Int), Int](_._2)
  namesAndDegrees.map(_._2).top(10)(ord)}
  topNamesAndDegrees(degrees, graph).foreach(println)
  </code></pre>
  <p>The task is not serializable but the function idMapbis is working since there is no error with idMapbis.value(graph.vertices.take(1)(0)._1.toInt)</p>
  <h3>Option 2:</h3>
  <pre><code>graph.vertices.map{case (k, v) =&gt; (k,idMapbis.value(k.toInt))}
  </code></pre>
  <p>The task is not serializable again (for context here is how topNamesAndDegrees is modified to obtain the names of the most connected vertices in this option)</p>
  <pre><code>def topNamesAndDegrees(degrees: VertexRDD[Int], graph: Graph[Int, Int]): Array[(String, Int)] = {
  val namesAndDegrees = degrees.innerJoin(graph.vertices) {
   (id, degree, name) =&gt; (name, degree)}
  val ord = Ordering.by[(String, Int), Int](_._2)
  namesAndDegrees.map(_._2).top(10)(ord)}
  topNamesAndDegrees(degrees, graph).foreach(println)
  </code></pre>
  <p>I am interested in understanding how to improve one of this option, maybe both if someone see how.</p>