课程表

Spark 基础

Spark RDDs

Spark Streaming

Spark SQL

GraphX编程指南

工具箱
速查手册

Spark GraphX图算法

当前位置:免费教程 » 数据库/运维 » Spark

GraphX包括一组图算法来简化分析任务。这些算法包含在org.apache.spark.graphx.lib包中,可以被直接访问。

PageRank算法

PageRank度量一个图中每个顶点的重要程度,假定从u到v的一条边代表v的重要性标签。例如,一个Twitter用户被许多其它人粉,该用户排名很高。GraphX带有静态和动态PageRank的实现方法,这些方法在PageRank object中。静态的PageRank运行固定次数的迭代,而动态的PageRank一直运行,直到收敛。[GraphOps]()允许直接调用这些算法作为图上的方法。

GraphX包含一个我们可以运行PageRank的社交网络数据集的例子。用户集在graphx/data/users.txt中,用户之间的关系在graphx/data/followers.txt中。我们通过下面的方法计算每个用户的PageRank。

  1. // Load the edges as a graph
  2. val graph = GraphLoader.edgeListFile(sc, "graphx/data/followers.txt")
  3. // Run PageRank
  4. val ranks = graph.pageRank(0.0001).vertices
  5. // Join the ranks with the usernames
  6. val users = sc.textFile("graphx/data/users.txt").map { line =>
  7. val fields = line.split(",")
  8. (fields(0).toLong, fields(1))
  9. }
  10. val ranksByUsername = users.join(ranks).map {
  11. case (id, (username, rank)) => (username, rank)
  12. }
  13. // Print the result
  14. println(ranksByUsername.collect().mkString("\n"))

连通体算法

连通体算法用id标注图中每个连通体,将连通体中序号最小的顶点的id作为连通体的id。例如,在社交网络中,连通体可以近似为集群。GraphX在ConnectedComponents object中包含了一个算法的实现,我们通过下面的方法计算社交网络数据集中的连通体。

  1. / Load the graph as in the PageRank example
  2. val graph = GraphLoader.edgeListFile(sc, "graphx/data/followers.txt")
  3. // Find the connected components
  4. val cc = graph.connectedComponents().vertices
  5. // Join the connected components with the usernames
  6. val users = sc.textFile("graphx/data/users.txt").map { line =>
  7. val fields = line.split(",")
  8. (fields(0).toLong, fields(1))
  9. }
  10. val ccByUsername = users.join(cc).map {
  11. case (id, (username, cc)) => (username, cc)
  12. }
  13. // Print the result
  14. println(ccByUsername.collect().mkString("\n"))

三角形计数算法

一个顶点有两个相邻的顶点以及相邻顶点之间的边时,这个顶点是一个三角形的一部分。GraphX在TriangleCount object中实现了一个三角形计数算法,它计算通过每个顶点的三角形的数量。需要注意的是,在计算社交网络数据集的三角形计数时,TriangleCount需要边的方向是规范的方向(srcId < dstId),并且图通过Graph.partitionBy分片过。

  1. // Load the edges in canonical order and partition the graph for triangle count
  2. val graph = GraphLoader.edgeListFile(sc, "graphx/data/followers.txt", true).partitionBy(PartitionStrategy.RandomVertexCut)
  3. // Find the triangle count for each vertex
  4. val triCounts = graph.triangleCount().vertices
  5. // Join the triangle counts with the usernames
  6. val users = sc.textFile("graphx/data/users.txt").map { line =>
  7. val fields = line.split(",")
  8. (fields(0).toLong, fields(1))
  9. }
  10. val triCountByUsername = users.join(triCounts).map { case (id, (username, tc)) =>
  11. (username, tc)
  12. }
  13. // Print the result
  14. println(triCountByUsername.collect().mkString("\n"))

Spark GraphX例子

假定我们想从一些文本文件中构建一个图,限制这个图包含重要的关系和用户,并且在子图上运行page-rank,最后返回与top用户相关的属性。可以通过如下方式实现。

  1. // Connect to the Spark cluster
  2. val sc = new SparkContext("spark://master.amplab.org", "research")
  3. // Load my user data and parse into tuples of user id and attribute list
  4. val users = (sc.textFile("graphx/data/users.txt")
  5. .map(line => line.split(",")).map( parts => (parts.head.toLong, parts.tail) ))
  6. // Parse the edge data which is already in userId -> userId format
  7. val followerGraph = GraphLoader.edgeListFile(sc, "graphx/data/followers.txt")
  8. // Attach the user attributes
  9. val graph = followerGraph.outerJoinVertices(users) {
  10. case (uid, deg, Some(attrList)) => attrList
  11. // Some users may not have attributes so we set them as empty
  12. case (uid, deg, None) => Array.empty[String]
  13. }
  14. // Restrict the graph to users with usernames and names
  15. val subgraph = graph.subgraph(vpred = (vid, attr) => attr.size == 2)
  16. // Compute the PageRank
  17. val pagerankGraph = subgraph.pageRank(0.001)
  18. // Get the attributes of the top pagerank users
  19. val userInfoWithPageRank = subgraph.outerJoinVertices(pagerankGraph.vertices) {
  20. case (uid, attrList, Some(pr)) => (pr, attrList.toList)
  21. case (uid, attrList, None) => (0.0, attrList.toList)
  22. }
  23. println(userInfoWithPageRank.vertices.top(5)(Ordering.by(_._2._1)).mkString("\n"))
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号