我正在尝试使用一些可以在此处找到的 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 应该就足够了。
有任何想法吗?
非常感谢!
罗密欧
最佳答案
不完全是。如果你看一下 apply
的签名Graph
的方法你会看到这样的对象(完整签名见 API docs ):
apply[VD, ED](
vertices: RDD[(VertexId, VD)], edges: RDD[Edge[ED]], defaultVertexAttr: VD)
正如您在描述中所读到的:
Construct a graph from a collection of vertices and edges with attributes.
因此,您不能简单地通过
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, ""))
注意:
Duplicate vertices are picked arbitrarily
所以
.distinct()
在 nodes
在这种情况下已过时。如果你想创建一个
Graph
如果没有属性,您可以使用 Graph.fromEdgeTuples
.
关于scala - 如何使用 Long 数据类型在 Apache Spark GraphX 中创建 VertexId?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31189092/