Exploding nested Struct in Spark dataframe

后端 未结 3 1688
时光取名叫无心
时光取名叫无心 2020-12-01 14:26

I\'m working through a Databricks example. The schema for the dataframe looks like:

> parquetDF.printSchema
root
|-- department: struct (nullable = true)
|         


        
相关标签:
3条回答
  • 2020-12-01 14:28

    In my opinion the most elegant solution is to star expand a Struct using a select operator as shown below:

    var explodedDf2 = explodedDf.select("department.*","*")
    

    https://docs.databricks.com/spark/latest/spark-sql/complex-types.html

    0 讨论(0)
  • 2020-12-01 14:33

    This seems to work (though maybe not the most elegant solution).

    var explodeDF2 = explodeDF.withColumn("id", explodeDF("department.id"))
    explodeDF2 = explodeDF2.withColumn("name", explodeDF2("department.name"))
    
    0 讨论(0)
  • 2020-12-01 14:55

    You could use something like that:

    var explodeDF = explodeDF.withColumn("id", explodeDF("department.id"))
    explodeDeptDF = explodeDeptDF.withColumn("name", explodeDeptDF("department.name"))
    

    which you helped me into and these questions:

    • Flattening Rows in Spark
    • Spark 1.4.1 DataFrame explode list of JSON objects
    0 讨论(0)
提交回复
热议问题