我有两个数据框df1
和ip2Country
。
df1
包含 IP 地址,我正在尝试将 IP 地址映射到地理位置信息,例如经度和纬度,其中是 ip2Country
中的列。
我将其作为 Spark 提交作业运行,但尽管 df1
只有不到 2500 行,但操作仍花费了很长时间。
我的代码:
val agg =df1.join(ip2Country, ip2Country("network_start_int")=df1("sint") , "inner") .select($"src_ip" ,$"country_name".alias("scountry") ,$"iso_3".alias("scode") ,$"longitude".alias("slong") ,$"latitude".alias("slat") ,$"dst_ip",$"dint",$"count") .filter($"slong".isNotNull) val agg1 =agg.join(ip2Country, ip2Country("network_start_int")=agg("dint") , "inner") .select($"src_ip",$"scountry" ,$"scode",$"slong" ,$"slat",$"dst_ip" ,$"country_name".alias("dcountry") ,$"iso_3".alias("dcode") ,$"longitude".alias("dlong") ,$"latitude".alias("dlat"),$"count") .filter($"dlong".isNotNull)
还有其他方法可以连接两个表吗?还是我的方法不对?
最佳答案
如果您有一个大数据帧需要与一个小数据帧连接 - 广播连接非常有效。阅读此处:Broadcast Joins (aka Map-Side Joins)
bigdf.join(broadcast(smalldf))
关于scala - Spark : Faster way to join two dataframe?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45975620/