Spark DataFrame exploding a map with the key as a member

前端 未结 1 438
野性不改
野性不改 2021-01-20 07:11

I\'ve found a map exploding example at databrick\'s blog:

// input
{
  \"a\": {
    \"b\": 1,
    \"c\": 2
  }
}

Python: events.select(explode(\"a\").alias(         


        
相关标签:
1条回答
  • 2021-01-20 07:47

    Although I don't know whether its possible to explode the map with one single explode, there is a way to it with a UDF. The trick is to use Row#schema.fields(i).name to get the name of the "key"

    def mapStructs = udf((r: Row) => {
      r.schema.fields.map(f => (
        f.name,
        r.getAs[Row](f.name).getAs[Long]("d"),
        r.getAs[Row](f.name).getAs[Long]("e"))
      )
    })
    
    df
      .withColumn("udfResult", explode(mapStructs($"a")))
      .withColumn("x", $"udfResult._1")
      .withColumn("d", $"udfResult._2")
      .withColumn("e", $"udfResult._3")
      .drop($"udfResult")
      .drop($"a")
      .show
    

    gives

    +---+---+---+---+
    | id|  x|  d|  e|
    +---+---+---+---+
    |  0|  b|  1|  2|
    |  0|  c|  3|  4|
    +---+---+---+---+
    
    0 讨论(0)
提交回复
热议问题