apache-spark - 使用 Hive 表迭代 Spark 数据帧
问题描述
我有一个非常大的 csv 文件,所以我使用 spark 并将其加载到 spark 数据框中。
我需要从 csv 的每一行中提取纬度和经度,以创建一个叶图。
使用 pandas 我可以通过循环解决我的问题:
for index, row in locations.iterrows():
folium.CircleMarker(location=(row["Pickup_latitude"],
row["Pickup_longitude"]),
radius=20,
color="#0A8A9F",fill=True).add_to(marker_cluster)
我发现与 pandas 数据帧不同,火花数据帧不能通过循环处理 =>如何循环遍历 pyspark 中的每一行数据帧。
所以我认为我可以解决问题并将大数据切割成配置单元表,然后对其进行迭代。
是否可以在 hive 表中剪切巨大的 SPARK 数据框,然后使用循环迭代行?
解决方案
通常,您不需要遍历 DataFrame 或 RDD。您只需创建transformations
(如地图)将应用于每条记录,然后调用一些action
来调用该处理。
你需要类似的东西:
dataframe.withColumn("latitude", <how to extract latitude>)
.withColumn("longitude", <how to extract longitude>)
.select("latitude", "longitude")
.rdd
.map(row => <extract values from Row type>)
.collect() // this will move data to local collection
如果你不能用 SQL 来做,你需要用 RDD 来做:
dataframe
.rdd
.map(row => <create new row with latitude and longitude>)
.collect()
推荐阅读
- cassandra - 如何使用pithos将视频文件存储到cassandraDB并将数据复制到另一台机器的另一个节点
- python - Python/Sqlite3:发生异常:sqlite3.OperationalError
- r - R 加速 sapply
- java - 如何将值传递给 JavaBean 中的 List?
- php - Lumen - 如何创建一个可以由 app('request') 从不同文件访问的请求对象?
- bash - 逐列解析CSV文件时比较数值的问题
- r - 使用 read_lines 阅读 pdf 的多页
- excel - 如何允许对受保护的单元格进行格式化和调整大小 - VBA
- android - 是否可以在设备之间同步 HLS 直播?
- recursion - Erlang 打印 N 次