How to split multi-value column into separate rows using typed Dataset?

后端 未结 3 1927
無奈伤痛
無奈伤痛 2021-01-05 07:17

I am facing an issue of how to split a multi-value column, i.e. List[String], into separate rows.

The initial dataset has following types: Dataset

3条回答
  •  迷失自我
    2021-01-05 07:32

    explode is often suggested, but it's from the untyped DataFrame API and given you use Dataset, I think flatMap operator might be a better fit (see org.apache.spark.sql.Dataset).

    flatMap[U](func: (T) ⇒ TraversableOnce[U])(implicit arg0: Encoder[U]): Dataset[U]
    

    (Scala-specific) Returns a new Dataset by first applying a function to all elements of this Dataset, and then flattening the results.

    You could use it as follows:

    val ds = Seq(
      (0, "Lorem ipsum dolor", 1.0, Array("prp1", "prp2", "prp3")))
      .toDF("id", "text", "value", "properties")
      .as[(Integer, String, Double, scala.List[String])]
    
    scala> ds.flatMap { t => 
      t._4.map { prp => 
        (t._1, t._2, t._3, prp) }}.show
    +---+-----------------+---+----+
    | _1|               _2| _3|  _4|
    +---+-----------------+---+----+
    |  0|Lorem ipsum dolor|1.0|prp1|
    |  0|Lorem ipsum dolor|1.0|prp2|
    |  0|Lorem ipsum dolor|1.0|prp3|
    +---+-----------------+---+----+
    
    // or just using for-comprehension
    for {
      t <- ds
      prp <- t._4
    } yield (t._1, t._2, t._3, prp)
    

提交回复
热议问题