gpt4 book ai didi

scala - 如何使用 Graph.fromEdgeTuples 从 Array[(Any, Any)] 创建图形

转载 作者:行者123 更新时间:2023-12-04 14:42:57 25 4
gpt4 key购买 nike

我对 spark 很陌生,但我想根据从 Hive 表中获得的关系创建一个图表。我发现了一个函数,它应该允许在不定义顶点的情况下执行此操作,但我无法让它工作。

我知道这不是一个可重复的例子,但这是我的代码:

import org.apache.spark.SparkContext
import org.apache.spark.graphx._
import org.apache.spark.rdd.RDD
val sqlContext= new org.apache.spark.sql.hive.HiveContext(sc)
val data = sqlContext.sql("select year, trade_flow, reporter_iso, partner_iso, sum(trade_value_us) from comtrade.annual_hs where length(commodity_code)='2' and not partner_iso='WLD' group by year, trade_flow, reporter_iso, partner_iso").collect()
val data_2010 = data.filter(line => line(0)==2010)
val couples = data_2010.map(line=>(line(2),line(3)) //country to country

val graph = Graph.fromEdgeTuples(couples, 1)

最后一行生成以下错误:
val graph = Graph.fromEdgeTuples(sc.parallelize(couples), 1)
<console>:31: error: type mismatch;
found : Array[(Any, Any)]
required: Seq[(org.apache.spark.graphx.VertexId,org.apache.spark.graphx.VertexId)]
Error occurred in an application involving default arguments.
val graph = Graph.fromEdgeTuples(sc.parallelize(couples), 1)

夫妻看起来像这样:
couples: Array[(Any, Any)] = Array((MWI,MOZ), (WSM,AUS), (MDA,CRI), (KNA,HTI), (PER,ERI), (SWE,CUB), (DEU,PRK), (THA,DJI), (BIH,SVK), (RUS,THA), (SGP,BLR), (MEX,TGO), (TUR,ZAF), (ZWE,SYC), (UGA,GHA), (OMN,SVN), (NZL,SYR), (CHE,SLV), (CZE,LUX), (TGO,COM), (TTO,WLF), (NGA,PAN), (FJI,UKR), (BRA,ECU), (EGY,SWE), (ITA,ARG), (MUS,MLT), (MDG,DZA), (ARE,SUR), (CAN,GUY), (OMN,COG), (NAM,FIN), (ITA,HMD), (SWE,CHE), (SDN,NER), (TUN,USA), (THA,GMB), (HUN,TTO), (FRA,BEN), (NER,TCD), (CHN,JPN), (DNK,ZAF), (MLT,UKR), (ARM,OMN), (PRT,IDN), (BEN,PER), (TTO,BRA), (KAZ,SMR), (CPV,""), (ARG,ZAF), (BLR,TJK), (AZE,SVK), (ITA,STP), (MDA,IRL), (POL,SVN), (PRY,ETH), (HKG,MOZ), (QAT,GAB), (THA,MUS), (PHL,MOZ), (ITA,SGS), (ARM,KHM), (ARG,KOR), (AUT,GMB), (SYR,COM), (CZE,GBR), (DOM,USA), (CYP,LAO), (USA,LBR)

如何转换为合适的格式?

最佳答案

首先你不能使用 String作为 VertexId所以你必须将标签映射到 Long .然后,我们需要准备一个从标签到 id 的映射。只要唯一值的数量比较少,最简单的方法就是创建一个广播变量:

val idMap = sc.broadcast(couples // -> Array[(Any, Any)]
// Make sure we use String not Any returned from Row.apply
// And convert to Seq so we can flatten results
.flatMap{case (x: String, y: String) => Seq(x, y)} // -> Array[String]
// Get different keys
.distinct // -> Array[String]
// Create (key, value) pairs
.zipWithIndex // -> Array[(String, Int)]
// Convert values to Long so we can use it as a VertexId
.map{case (k, v) => (k, v.toLong)} // -> Array[(String, Long)]
// Create map
.toMap) // -> Map[String,Long]

接下来我们可以使用上面的来进行映射:
val edges: RDD[(VertexId, VertexId)] = sc.parallelize(couples
.map{case (x: String, y: String) => (idMap.value(x), idMap.value(y))}
)

最后我们得到一个图形:
val graph = Graph.fromEdgeTuples(edges, 1)

关于scala - 如何使用 Graph.fromEdgeTuples 从 Array[(Any, Any)] 创建图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31928133/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com