【问题标题】:Using ALTER TABLE to add new column into Array(Struct) Column on Databricks使用 ALTER TABLE 将新列添加到 Databricks 上的 Array(Struct) 列中
【发布时间】:2021-09-02 08:04:50
【问题描述】:

我有一个 DeltaTable,其中有几列是 ArrayTypes,包含 StructTypes。

我正在尝试在 StructType 中添加一个额外的列,但我遇到了问题,因为它包含在 ArrayType 中。

希望有人有办法做到这一点,或者类似的方法不涉及我备份表,从头开始重新创建表并从备份中恢复数据。

我目前正在使用这个命令:

spark.sql(f"ALTER TABLE {db}.ads ADD COLUMNS (response.monkey boolean AFTER dq_conveyed)")

如果它只是一个 StructType 会起作用,但因为它是一个数组而失败

这是我正在尝试更改的当前架构:

StructField(response,ArrayType(StructType(
    StructField(encounter_uid,StringType,true),
    StructField(patient_uid,StringType,true),
    StructField(call_sign,StringType,true),
    StructField(time_resource_allocated,StringType,true),
    StructField(time_resource_arrived_at_receiving_location,StringType,true),
    StructField(time_of_patient_handover,StringType,true),
    StructField(time_clear,StringType,true),
    StructField(response_type,StringType,true),
    StructField(time_resource_mobilised,StringType,true),
    StructField(time_resource_arrived_on_scene,StringType,true),
    StructField(time_stood_down,StringType,true),
    StructField(time_resource_left_scene,StringType,true),
    StructField(highest_skill_level_on_vehicle,StringType,true),
    StructField(responding_organisation_type,StringType,true),
    StructField(dq_conveyed,BooleanType,true)),
true),true)

我正在关注此文档:

https://docs.databricks.com/spark/latest/spark-sql/language-manual/sql-ref-syntax-ddl-alter-table.html

【问题讨论】:

    标签: apache-spark databricks delta-lake


    【解决方案1】:

    你可以这样做:

    spark.sql(f"ALTER TABLE {db}.ads ADD COLUMNS (response.element.monkey boolean AFTER dq_conveyed)")
    

    注意element 关键字。如果这对您不起作用,请告诉我!

    【讨论】:

    • 不幸的是,这也没有奏效。我收到以下错误:SQL 语句中的错误:AnalysisException:找不到列 response.element in 然后打印出表的架构,该架构确实包含您建议的元素|-- 响应:数组(可为空=真)| |-- 元素:结构(containsNull = true)
    • 您使用的是哪个版本的 DBR?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    相关资源
    最近更新 更多