教程地址:http://www.showmeai.tech/tutorials/84
本文地址:http://www.showmeai.tech/article-detail/182
声明:版权所有,转载请联系平台与作者并注明出处
由Databricks、UC Berkeley以及MIT联合为Apache Spark开发了一款图处理类库,名为GraphFrames。该类库构建在DataFrame之上,既能利用DataFrame良好的扩展性和强大的性能,同时也为Scala、Java和Python提供了统一的图处理API。
Spark从最开始的关系型数据查询,到图算法实现,到GraphFrames库可以完成图查询。
GraphFrames是类似于Spark的GraphX库,支持图处理。但GraphFrames建立在Spark DataFrame之上,具有以下重要的优势:
以航班分析为例,我们需要构建GraphFrames:
# Create Vertices (airports) and Edges (flights)
tripVertices=airports.withColumnRenamed("IATA","id").distinct()
tripEdges=departureDelays
.select("tripid","delay","src","dst","city_dst","state_dst")
# This GraphFrame builds upon the vertices and edges based on our trips (flights)
tripGraph=GraphFrame(tripVertices, tripEdges)
# 查询机场个数和行程个数(查询节点和边的个数)
print("Airports:", tripGraph.vertices.count())
print("Trips:", tripGraph.edges.count())
# 查询最长延误时间(通过分组统计完成)
longestDelay = tripGraph.edges.groupby().max("delay")
# 晚点与准点航班分析(通过数据选择与过滤,进行边的分析)
print "On-time / Early Flights: %d" % tripGraph.edges.filter("delay <= 0").count()
print "Delayed Flights: %d" % tripGraph.edges.filter("delay > 0").count()
# 从旧金山出发的飞机中延迟最严重的航班(数据选择+边分析+分组统计)
tripGraph.edges.filter(“src = ‘SFO’ and delay > 0”).groupBy(“src”, “dst”).avg(“delay”).sort(desc(“avg(delay)”))
在航班案例中:入度:抵达本机场的航班数量;出度:从本机场出发的航班数量;度:连接数量。
display(tripGraph.degrees.sort(desc("degree")).limit(20))
边的分析,通常是对成对的数据进行统计分析的
import pyspark.sql.functions as func
topTrips = tripGraph.edges.groupBy("src", "dst").agg(func.count("delay").alias("trips"))
通过入度和出度分析中转站:入度/出度≈1,中转站;入度/出度>1,出发站;入度/出度<1,抵达站。
# Calculate the inDeg (flights into the airport) and outDeg (flights leaving the airport)
inDeg = tripGraph.inDegrees
outDeg = tripGraph.outDegrees
# Calculate the degreeRatio (inDeg/outDeg)
degreeRatio = inDeg.join(outDeg, inDeg.id == outDeg.id).drop(outDeg.id).selectExpr("id", "double(inDegree)/double(outDegree) as degreeRatio").cache()
# Join back to the `airports` DataFrame (instead of registering temp table as above)
nonTransferAirports = degreeRatio.join(airports, degreeRatio.id == airports.IATA) \
.selectExpr("id", "city", "degreeRatio").filter("degreeRatio < .9 or degreeRatio > 1.1")
# List out the city airports which have abnormal degree ratios.
display(nonTransferAirports)
# Join back to the `airports` DataFrame (instead of registering temp table as above)
transferAirports = degreeRatio.join(airports, degreeRatio.id == airports.IATA) \
.selectExpr("id", "city", "degreeRatio").filter("degreeRatio between 0.9 and 1.1")
# List out the top 10 transfer city airports
display(transferAirports.orderBy("degreeRatio").limit(10))
通过广度优先搜索,可以对图中的两个点进行关联查询:比如我们查询从旧金山到布法罗,中间有一次中转的航班。
# Example 1: Direct Seattle to San Francisco
filteredPaths = tripGraph.bfs(fromExpr = "id = 'SEA'", toExpr = "id = 'SFO'", maxPathLength = 1)
display(filteredPaths)
# Example 2: Direct San Francisco and Buffalo
filteredPaths = tripGraph.bfs(fromExpr = "id = 'SFO'", toExpr = "id = 'BUF'", maxPathLength = 1)
display(filteredPaths)
# Example 2a: Flying from San Francisco to Buffalo
filteredPaths = tripGraph.bfs(fromExpr = "id = 'SFO'", toExpr = "id = 'BUF'", maxPathLength = 2)
display(filteredPaths)
可以通过pagerank算法进行机场排序:每个机场都会作为始发站和终点站很多次,可以通过pagerank算法对其重要度进行排序。
# Determining Airport ranking of importance using `pageRank`
ranks = tripGraph.pageRank(resetProbability=0.15, maxIter=5)
display(ranks.vertices.orderBy(ranks.vertices.pagerank.desc()).limit(20))
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。