【发布时间】:2021-08-05 22:18:15
【问题描述】:
为了在大型数据集上获得一些异常值图,我需要将 spark DataFrame 转换为 pandas。 Turing to Apache Arrow 一个简单的运行在将 x 转换为字符串时使我的 pyspark 控制台崩溃(没有转换它可以正常工作),为什么?
Using Python version 3.8.9 (default, Apr 10 2021 15:47:22)
Spark context Web UI available at http://6d0b1018a45a:4040
Spark context available as 'sc' (master = local[*], app id = local-1621164597906).
SparkSession available as 'spark'.
>>> import time
>>> from pyspark.sql.functions import rand
>>> from pyspark.sql import functions as F
>>> spark = SparkSession.builder.appName("Console_Test").getOrCreate()
>>> spark.conf.set("spark.sql.execution.arrow.enabled", "true")
21/05/16 11:31:03 WARN SQLConf: The SQL config 'spark.sql.execution.arrow.enabled' has been deprecated in Spark v3.0 and may be removed in the future. Use 'spark.sql.execution.arrow.pyspark.enabled' instead of it.
>>> a_df = spark.range(1 << 25).toDF("id").withColumn("x", rand())
>>> a_df = a_df.withColumn("id", F.col("id").cast("string"))
>>> start_t = time.time()
>>> a_pd = a_df.toPandas()
Killed
#
此外,我注意到 spark.conf.set("spark.sql.execution.arrow.maxRecordsPerBatch", "5000") 之类的选项似乎没有效果,因为 Web ui 显示分配给任务的记录明显超过 5000。
任何关于如何解决 pyspark 控制台崩溃或更直接渲染大散点图的指示将不胜感激 - 我(未成功)试图找到一种方法来应用 Table.to_pandas(split_blocks=True, self_destruct=True),但没有从火花中获得有效的结构DataFrame.
【问题讨论】:
-
这个来自the docs的sn-p可能是相关的,
Note that even with Arrow, DataFrame.toPandas() results in the collection of all records in the DataFrame to the driver program and should be done on a small subset of the data. -
您应该能够使用
pandas_udf来减少内存使用量。这就是maxRecordsPerBatch属性的用途。 -
@Pace 谢谢。我不知道该限制仅适用于 pandas_udf。这解释了观察到的行为。
标签: dataframe pyspark pyarrow apache-arrow