You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Pandas DataFrame创建Spark DataFrame时遇StructType无encode属性错误

问题分析与解决方案

错误原因

你的mySchema定义格式错误:它是一个元组,第一个元素是StructType对象,后续是独立的StructField对象。Spark处理这种格式时,会将其识别为list/tuple类型的schema,尝试对每个元素执行encode("utf-8")操作,但StructType没有encode方法,因此触发AttributeError。

正确的Spark Schema必须是一个完整的StructType实例,所有字段(包括嵌套字段)都要包含在StructType的字段列表中。

修改后的代码

from pyspark.sql.types import (
    StructType, StructField, StringType, DateType, IntegerType, ArrayType
)

mySchema = StructType(
    [
        StructField("country_code", StringType(), True),
        StructField("unit_id", StringType(), True),
        StructField("date", DateType(), True),
        StructField("health_category_car_door", StringType(), True),
        StructField("reason_car", StringType(), True),
        StructField("reason_landing", StringType(), True),
        StructField(
            "reasonDetails_car_door",
            StructType(
                [
                    StructField(
                        "car_doors",
                        ArrayType(
                            StructType(
                                [
                                    StructField("opmode", StringType(), True),
                                    StructField("count", IntegerType(), True),
                                    StructField("window_length", IntegerType(), True),
                                ]
                            ),
                            True,
                        ),
                        True,
                    ),
                    StructField("landing_doors", StringType(), True),
                ]
            ),
            True,
        ),
        # 将原来元组中独立的字段全部移到StructType的字段列表内
        StructField("health_category_landing_door", StringType(), True),
        StructField("num_yellow_preds_in_last_14_days", IntegerType(), True),
        StructField(
            "reasonDetails_landing_door",
            ArrayType(
                StructType(
                    [
                        StructField("id", StringType(), True),
                        StructField(
                            "causes",
                            ArrayType(
                                StructType(
                                    [
                                        StructField("opmode", StringType(), True),
                                        StructField("count", IntegerType(), True),
                                        StructField("window_length", IntegerType(), True),
                                    ]
                                ),
                                True,
                            ),
                            True,
                        ),
                        StructField("num_yellow_preds_in_last_14_days", IntegerType(), True),
                    ]
                ),
                True,
            ),
        ),
    ]
)

sparkDF = spark.createDataFrame(df_new, mySchema)
sparkDF.printSchema()

关键修改点

  • 移除了外层的元组包裹,将所有StructField统一放入StructType的构造参数列表中
  • 确保整个Schema是一个单一的StructType实例,符合Spark对Schema格式的要求

执行修改后的代码后,printSchema()会输出正确的嵌套结构,不会再触发AttributeError。

内容的提问来源于stack exchange,提问作者Saswat Ray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 03:35:54