# Spark GraphX图算法

2018-11-26 16:34 更新

## Spark GraphX图算法

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

### PageRank算法

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

``````// Load the edges as a graph
// Run PageRank
val ranks = graph.pageRank(0.0001).vertices
// Join the ranks with the usernames
val users = sc.textFile("graphx/data/users.txt").map { line =>
val fields = line.split(",")
(fields(0).toLong, fields(1))
}
}
// Print the result

### 连通体算法

``````/ Load the graph as in the PageRank example
// Find the connected components
val cc = graph.connectedComponents().vertices
// Join the connected components with the usernames
val users = sc.textFile("graphx/data/users.txt").map { line =>
val fields = line.split(",")
(fields(0).toLong, fields(1))
}
}
// Print the result

### 三角形计数算法

``````// Load the edges in canonical order and partition the graph for triangle count
val graph = GraphLoader.edgeListFile(sc, "graphx/data/followers.txt", true).partitionBy(PartitionStrategy.RandomVertexCut)
// Find the triangle count for each vertex
val triCounts = graph.triangleCount().vertices
// Join the triangle counts with the usernames
val users = sc.textFile("graphx/data/users.txt").map { line =>
val fields = line.split(",")
(fields(0).toLong, fields(1))
}
}
// Print the result