我如何用这个在 GraphX 中创建一个图形
How do i create a Graph in GraphX with this
我很难理解我将如何在 Apache spark 的 GraphX 中创建以下内容。我得到以下信息:
一个包含大量数据的 hdfs 文件,其格式为:
node: ConnectingNode1, ConnectingNode2..
例如:
123214: 521345, 235213, 657323
我需要以某种方式将这些数据存储在 EdgeRDD 中,以便我可以在 GraphX 中创建我的图形,但我不知道我将如何处理这个。
阅读 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)
我很难理解我将如何在 Apache spark 的 GraphX 中创建以下内容。我得到以下信息:
一个包含大量数据的 hdfs 文件,其格式为:
node: ConnectingNode1, ConnectingNode2..
例如:
123214: 521345, 235213, 657323
我需要以某种方式将这些数据存储在 EdgeRDD 中,以便我可以在 GraphX 中创建我的图形,但我不知道我将如何处理这个。
阅读 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)