我在工作,我需要立即帮助,请我有一个镶木地板文件,我需要将其转换为 csv。你能帮帮我吗?

错误:

AnalysisException: CSV data source does not support array<struct<company:string,dateRange:string,description:str,apache-spark,pyspark,parquet"/>
	














首页 > 解决方案 > AnalysisException:CSV 数据源不支持数组

我在工作,我需要立即帮助,请我有一个镶木地板文件,我需要将其转换为 csv。你能帮帮我吗?

错误:

AnalysisException: CSV data source does not support array<struct<company:string,dateRange:string,description:str

问题描述

我在工作,我需要立即帮助,请我有一个镶木地板文件,我需要将其转换为 csv。你能帮帮我吗?

错误:

AnalysisException: CSV data source does not support array<struct<company:string,dateRange:string,description:string,location:string,title:string>> data type.

我从来没有使用过这种格式,所以我什至不能打印模式。对不起

printshema:

root
 |-- _id: string (nullable = true)
 |-- Locale: string (nullable = true)
 |-- workExperience: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- company: string (nullable = true)
 |    |    |-- dateRange: string (nullable = true)
 |    |    |-- description: string (nullable = true)
 |    |    |-- location: string (nullable = true)
 |    |    |-- title: string (nullable = true)

您无法将包含数组/结构类型列的数据框保存到 CSV。您需要在写入之前将列转换为字符串。

df.withColumn('workExperience', col('workExperience').cast('string')).write.csv('path')

标签: apache-sparkpysparkparquet

解决方案


parquet 模式可以使用explode 展平:

df=spark.read.parquet(...)
flattened_df = df.withColumn("tmp", F.explode("workExperience")) \
    .selectExpr("_id", "Locale", "tmp.*")
flattened_df.write.csv(...)

推荐阅读