【问题标题】:How to explode space-separated column?如何爆炸空间分隔的列?
【发布时间】:2018-05-22 02:18:01
【问题描述】:

我在 Spark Scala 中有一个示例数据框,其中包含一列和许多其他列 50+ 并且需要分解 id:

示例数据:

id             name   address
234 435 567    auh    aus
345 123        muji   uk

输出数据:

id             name   address
234            auh    aus
435            auh    aus
567            auh    aus
345            muji   uk
123            muji   uk

【问题讨论】:

    标签: scala apache-spark apache-spark-sql


    【解决方案1】:

    试试这个:

    import org.apache.spark.sql.functions._
    
    scala> df.withColumn("id", explode(split($"id", " "))).show
    +---+----+-------+
    | id|name|address|
    +---+----+-------+
    |234| auh|    aus|
    |435| auh|    aus|
    |567| auh|    aus|
    |345|muji|     uk|
    |123|muji|     uk|
    +---+----+-------+
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-24
      相关资源
      最近更新 更多