<分区>
我很难理解我将如何在 Apache spark 的 GraphX 中创建以下内容。我得到以下内容:
一个 hdfs 文件,其中包含以下形式的大量数据:
node: ConnectingNode1, ConnectingNode2..
例如:
123214: 521345, 235213, 657323
我需要以某种方式将这些数据存储在 EdgeRDD 中,以便我可以在 GraphX 中创建我的图表,但我不知道我将如何去做。
<分区>
我很难理解我将如何在 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)
关于scala - 我如何用这个在 GraphX 中创建一个图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41190668/
相关文章:
java - Spark的scala代码如何暴露为Java API?
scala - 使用宏转换树后重新建立类型一致性的最佳方法是什么
apache-spark - 将 Spark Dataframe 直接写入 HIVE 需要花费太多时间
list - Scala根据另一个列表中的值对一个列表进行排序
python - hadoop-streaming:当 mapred.reduce.tasks=1 时,reducer 似乎没有运行
hadoop - 是否可以在docker文件中间执行CMD?
json - Spark 运行错误 java.lang.NoClassDefFoundError : org/codehaus/jackson/annotate/JsonClass