【问题标题】:How to create a VertexId in Apache Spark GraphX using a Long data type?如何使用 Long 数据类型在 Apache Spark GraphX 中创建 VertexId?
【发布时间】:2015-09-20 05:59:37
【问题描述】:

我正在尝试使用可以在此处找到的一些 Google Web Graph 数据创建一个图表:

https://snap.stanford.edu/data/web-Google.html

import org.apache.spark._
import org.apache.spark.graphx._
import org.apache.spark.rdd.RDD



val textFile = sc.textFile("hdfs://n018-data.hursley.ibm.com/user/romeo/web-Google.txt")
val arrayForm = textFile.filter(_.charAt(0)!='#').map(_.split("\\s+")).cache()
val nodes = arrayForm.flatMap(array => array).distinct().map(_.toLong)
val edges = arrayForm.map(line => Edge(line(0).toLong,line(1).toLong))

val graph = Graph(nodes,edges)

很遗憾,我收到了这个错误:

<console>:27: error: type mismatch;
 found   : org.apache.spark.rdd.RDD[Long]
 required: org.apache.spark.rdd.RDD[(org.apache.spark.graphx.VertexId, ?)]
Error occurred in an application involving default arguments.
       val graph = Graph(nodes,edges)

那么我怎样才能创建一个 VertexId 对象呢?据我了解,通过 Long 就足够了。

有什么想法吗?

非常感谢!

罗密欧

【问题讨论】:

    标签: scala apache-spark spark-graphx


    【解决方案1】:

    不完全是。如果您查看Graph 对象的apply 方法的签名,您会看到类似这样的内容(完整签名请参阅API docs):

    apply[VD, ED](
        vertices: RDD[(VertexId, VD)], edges: RDD[Edge[ED]], defaultVertexAttr: VD)
    

    正如您在描述中看到的那样:

    带有属性的顶点和边的集合构造一个图。

    因此,您不能简单地将RDD[Long] 作为vertices 参数传递(RDD[Edge[Nothing]] 作为edges 也不起作用)。

    import scala.{Option, None}
    
    val nodes: RDD[(VertexId, Option[String])] = arrayForm.
        flatMap(array => array).
        map((_.toLong, None))
    
    val edges: RDD[Edge[String]] = arrayForm.
        map(line => Edge(line(0).toLong, line(1).toLong, ""))
    

    注意:

    任意选取重复的顶点

    所以在这种情况下,nodes 上的 .distinct() 已过时。

    如果你想创建一个没有属性的Graph,你可以使用Graph.fromEdgeTuples

    【讨论】:

    • 嗨,回到标题中的问题,有没有办法像使用边一样动态创建顶点对象?例如 val test = Edge(1, 1, 1) 。我试过 val test = Vertex(1, 1) 并没有在网上找到任何构造函数。
    • @mt88 没有必要。 Vertex 只是一个Tuple2[VertexId, T],其中VertexIdLong 的别名。
    【解决方案2】:

    错误消息说nodes 必须是RDD[(Long, anything else)] 的类型。元组中的第一个元素是 vertexId,第二个元素可以是任何东西,例如带有节点描述的字符串。尝试简单地重复vertexId:

    val nodes = arrayForm
                 .flatMap(array => array)
                 .distinct()
                 .map(x =>(x.toLong, x.toLong))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2010-10-23
      相关资源
      最近更新 更多