Spark sql 查询到 pandas 问题

Spark sql Query to pandas issue

所以我 运行 Azure Databricks 笔记本中的一些脚本。 我正在使用 pySpark 查询 table 一些数据。

query = """secret sql query""".format(ids)

try:
  DF_sql = spark.sql(query)

查询没有提示任何错误,但我尝试将其转换为 pandas 数据帧

DF_pd = DF_sql.toPandas()

但是我一直收到这个错误:

 UserWarning: toPandas attempted Arrow optimization because 'spark.sql.execution.arrow.enabled' is set to true, but has reached the error below and can not continue. Note that 'spark.sql.execution.arrow.fallback.enabled' does not have an effect on failures in the middle of computation.

dataframe DF_sql 可能是空的,这会导致这个错误吗?如果是这样,你如何优雅地处理它。

如果这是由于数据帧为空,我想你可以检查数据帧是否为空,然后转换为 Pandas 数据帧。

if len(df.head(1)) != 0 : DF_pd = DF_sql.toPandas()