【问题标题】:How to fix "AssertionError: keyType should be DataType" error如何修复“AssertionError:keyType 应该是 DataType”错误
【发布时间】:2019-07-19 01:02:57
【问题描述】:

来自 Kafka-console-producer 的 JSON 字节数据流 PySpark - 有一个解析器 json 数据到数据帧。

我试图通过使用给定的模式来解析这个 json。但是它给了我一个关于“AssertionError:keyType 应该是 DataType”的错误 使用自定义模式解析 json 需要做什么?

schema = StructType()\
    .add("contact_id", LongType())\
    .add("first_name", StringType())\
    .add("last_name", StringType())\
    .add("contact_number", MapType(StringType,
                                   StructType()
                                   .add("home", LongType())
                                   .add("contry_code", StringType())))

期待这种格式的 JSON 数据: {"contact_id":"23","first_name":"John","last_name":"Doe","contact_number":{"home":4564564567,"country_code":"+1"}}

【问题讨论】:

    标签: json pyspark pyspark-sql


    【解决方案1】:
    I have found the solution. This should be the correct schema definition.
    
    schema = StructType([
        StructField('contactId', LongType(), True),
        StructField('firstName', StringType(), True),
        StructField('lastName', StringType(), True),
        StructField("contactNumber", ArrayType(
            StructType([
                    StructField("type", StringType(), True),
                    StructField("number", LongType(), True),
                    StructField("countryCode", StringType(), True)
                ])
            ), True)
        ])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 2017-03-31
      • 2023-02-16
      • 1970-01-01
      • 2021-01-26
      • 2021-12-30
      相关资源
      最近更新 更多