scala - 如何使用 Long 数据类型在 Apache Spark GraphX 中创建 VertexId?

标签 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)

不幸的是,我收到此错误:
<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/

相关文章:

scala - 在 Apache Spark DataFrame 中,如何删除所有非 None 值都相同的所有列?

scala - 如何在scala中对元组进行切片

apache-spark - 无法加载 Parquet 文件(不支持 Parquet 类型 : INT32 (UINT_8);) with pyspark

apache-spark - spark-shell 系统找不到指定的路径

scala - 如何生成元组集合的传递闭包?

scala - 如何在 Scala Spark 项目中使用 PySpark UDF?

apache-spark - spark SQL(PySpark)如何实现自增

java - Spark GraphX 的 DFS 性能与简单 Java DFS 实现的比较

java - Apache Spark 中的分层数据操作

java - 创建数据并将其附加到 Spark graphx java