Error while exploding a struct column in Spark

后端 未结 3 737
孤城傲影
孤城傲影 2021-01-17 16:30

I have a dataframe whose schema looks like this:

event: struct (nullable = true)
|    | event_category: string (nullable = true)
|    | event_name: string (n         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-17 16:40

    as the error message says, you can only explode array or map types, not struct type columns.

    You can just do

    df_json.withColumn("event_properties", $"event.properties")
    

    This will generate a new column event_properties, which is also of struct-type

    If you want to convert every element of the struct to a new column, then you cannot use withColumn, you need to do a select with a wildcard *:

    df_json.select($"event.properties.*")
    

提交回复
热议问题