【问题标题】:How do i create a Graph in GraphX with this [closed]我如何使用这个 [关闭] 在 GraphX 中创建图表
【发布时间】:2017-05-02 14:53:35
【问题描述】:

我很难理解我将如何在 Apache spark 的 GraphX 中创建以下内容。我得到以下信息:

一个 hdfs 文件,其中包含以下形式的大量数据:

节点:ConnectingNode1、ConnectingNode2..

例如:

123214:521345、235213、657323

我需要以某种方式将这些数据存储在 EdgeRDD 中,以便我可以在 GraphX 中创建我的图表,但我不知道我将如何去做。

【问题讨论】:

    标签: scala hadoop apache-spark mapreduce spark-graphx


    【解决方案1】:

    在您阅读 hdfs 源并将数据保存在 rdd 后,您可以尝试以下操作:

    import org.apache.spark.rdd.RDD
    import org.apache.spark.graphx.Edge
    // Sample data
    val rdd = sc.parallelize(Seq("1: 1, 2, 3", "2: 2, 3"))
    
    val edges: RDD[Edge[Int]] = rdd.flatMap {
      row => 
        // split around ":"
        val splitted = row.split(":").map(_.trim)
        // the value to the left of ":" is the source vertex:
        val srcVertex = splitted(0).toLong
        // for the values to the right of ":", we split around "," to get the other vertices
        val otherVertices = splitted(1).split(",").map(_.trim)
        // for each vertex to the right of ":", we create an Edge object connecting them to the srcVertex:
        otherVertices.map(v => Edge(srcVertex, v.toLong, 1))
    }
    

    编辑

    此外,如果您的顶点具有恒定的默认权重,您可以直接从边创建图形,因此您不需要创建 verticesRDD:

    import org.apache.spark.graphx.Graph
    val g = Graph.fromEdges(edges, defaultValue = 1)
    

    【讨论】:

    • 感谢您的所有帮助!我按照你说的做了一个 val 图,只是想找到一种方法来看看它是否有效!
    • 我试着按照你说的方式做,唯一没用的是 RDD[Edge[Int] 所以我只使用了 RDD。但不断收到以下错误: :43: error: not found: value Edge otherVertices.map(v => Edge(srcVertex, v.toLong, 1)) ^ :43: error: type mismatch;发现:Array[Nothing] required: TraversableOnce[?] otherVertices.map(v => Edge(srcVertex, v.toLong, 1))
    • 您是否导入了 Edge 类? import org.apache.spark.graphx.Edge。这可能是问题所在,也是 RDD[Edge[Int]] 不起作用的原因
    • @RhysCopperthwaite
    • 谢谢我现在试试!,当我使用 edge[Int] 时,它给了我以下错误... :40: error: value rdd of type org.apache.spark。 rdd.RDD[String] 不接受类型参数。 val 边缘 = rdd[Edge[Int]].flatMap {
    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    相关资源
    最近更新 更多