Rom*_*ler 5 scala apache-spark spark-graphx
我正在尝试使用一些可以在此处找到的 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)
Run Code Online (Sandbox Code Playgroud)
不幸的是,我收到此错误:
<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)
Run Code Online (Sandbox Code Playgroud)
那么如何创建 VertexId 对象呢?根据我的理解,传递一个 Long 应该就足够了。
有任何想法吗?
非常感谢!
罗密欧
不完全是。如果您查看apply对象方法的签名Graph,您将看到类似以下内容(有关完整签名,请参阅API 文档):
apply[VD, ED](
vertices: RDD[(VertexId, VD)], edges: RDD[Edge[ED]], defaultVertexAttr: VD)
Run Code Online (Sandbox Code Playgroud)
正如您在描述中所读到的:
从具有属性的顶点和边的集合构造一个图。
因此,您不能简单地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, ""))
Run Code Online (Sandbox Code Playgroud)
注意:
任意选取重复的顶点
在这种情况下,so.distinct()已经过时了。nodes
如果你想创建一个Graph没有属性的,你可以使用Graph.fromEdgeTuples.