如何使用 Long 数据类型在 Apache Spark GraphX 中创建 VertexId?
How to create a VertexId in Apache Spark GraphX using a Long data type?
我正在尝试使用一些 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 就足够了。
有什么想法吗?
非常感谢!
罗密欧
错误消息说 nodes
必须是 RDD[(Long, anything else)]
的类型。元组中的第一个元素是 vertexId,第二个元素可以是任何东西,例如带有节点描述的字符串。尝试简单地重复 vertexId:
val nodes = arrayForm
.flatMap(array => array)
.distinct()
.map(x =>(x.toLong, x.toLong))
不完全是。如果您查看 Graph
对象的 apply
方法的签名,您会看到类似这样的内容(完整签名请参见 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
所以 nodes
上的 .distinct()
在这种情况下已过时。
如果你想创建一个没有属性的 Graph
你可以使用 Graph.fromEdgeTuples
.
我正在尝试使用一些 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 就足够了。
有什么想法吗?
非常感谢!
罗密欧
错误消息说 nodes
必须是 RDD[(Long, anything else)]
的类型。元组中的第一个元素是 vertexId,第二个元素可以是任何东西,例如带有节点描述的字符串。尝试简单地重复 vertexId:
val nodes = arrayForm
.flatMap(array => array)
.distinct()
.map(x =>(x.toLong, x.toLong))
不完全是。如果您查看 Graph
对象的 apply
方法的签名,您会看到类似这样的内容(完整签名请参见 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
所以 nodes
上的 .distinct()
在这种情况下已过时。
如果你想创建一个没有属性的 Graph
你可以使用 Graph.fromEdgeTuples
.