首页 > 解决方案 > 使用 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 数据框,然后使用循环迭代行?

标签: apache-sparkhivepysparkapache-spark-sqlhiveql

解决方案


通常,您不需要遍历 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()

推荐阅读