如何让Delta Live Tables正确读取嵌套JSON数据?
环境信息
- 使用Databricks DLT
- DBR版本:ML 13.3 LTS
尝试的两种Schema定义
选项1
json_schema = StructType( [ StructField(name="someId", dataType=IntegerType(), nullable=False), StructField("parameterSetId", StringType(), False), StructField("data", ArrayType(StructType([ StructField("key", IntegerType()), StructField("key2", StringType()), StructField("beginDate", TimestampType()), StructField("endDate", TimestampType()), StructField("data", ArrayType(StructType([ StructField("parameter", IntegerType()), StructField("value", IntegerType()), StructField("value2", IntegerType()), ]))), ])), False), ] )
选项2
json_schema = StructType( [ StructField(name="someId", dataType=IntegerType(), nullable=False), StructField("parameterSetId", StringType(), False), StructField("data", StringType(), False), ] )
错误详情
无论使用上述哪种Schema,都会触发相同类型的Schema不兼容错误(仅错误信息中声明的Schema部分有差异),具体错误提示如下:
org.apache.spark.sql.AnalysisException: 表 'raw_data' 的用户指定Schema与查询推断出的Schema不兼容。
流表是有状态的,会记录已处理的数据。如果想要从头重新计算表,请执行全量刷新操作。
声明的Schema:
root
-- someId: integer (nullable = false)
-- parameterSetId: string (nullable = false)
-- data: array (nullable = false)
|-- element: struct (containsNull = true)
| |-- key: integer (nullable = true)
| |-- key2: string (nullable = true)
| |-- beginDate: timestamp (nullable = true)
| |-- endDate: timestamp (nullable = true)
| |-- data: array (nullable = true)
| | |-- element: struct (containsNull = true)
| | | |-- parameter: integer (nullable = true)
| | | |-- value: integer (nullable = true)
| | | |-- value2: integer (nullable = true)推断的Schema:
root
-- value: string (nullable = true)
-- parameter: string (nullable = true)
-- value2: string (nullable = true)
-- _rescued_data: string (nullable = true)
内容的提问来源于stack exchange,提问作者Climbs_lika_Spyder

