【问题标题】:add parent name prefix to dataframe structtype fields将父名称前缀添加到数据框结构类型字段
【发布时间】:2020-12-14 12:39:15
【问题描述】:

我们正在生成如下数据帧

val res_df = df.select($"id",$"type",$"key",from_json($"value",schema).as("s")).select("id","type","key","s.*")

但是我们需要将“s.*”生成的所有列重命名为在字段名称前加上前缀“s_”。

【问题讨论】:

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


【解决方案1】:

这是解决您问题的一种方法:

import common.sparkSession
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.sql.functions._

object renameNestedColumn extends App with sparkSession{

  val schema = new StructType()
    .add(StructField("id",StringType))
    .add(StructField("value",new StructType()
        .add("city",StringType)
        .add("age",StringType)
        )
    )

  val data = Seq(Row("1",Row("montreal","30")),Row("2",Row("ny","25")))
  val rdd = spark.sparkContext.parallelize(data)
  val df = spark.createDataFrame(rdd,schema)
  df.printSchema()
  val nestedCols = df.select("value.*").columns.map(c => col(s"value.$c").as(s"prefix_$c")).toSeq++ Seq(col("id"))
  df.select(nestedCols:_*).show(false)

嵌套架构

root
 |-- id: string (nullable = true)
 |-- value: struct (nullable = true)
 |    |-- city: string (nullable = true)
 |    |-- age: string (nullable = true)

带有前缀嵌套列的扁平化输出

+-----------+----------+---+
|prefix_city|prefix_age|id |
+-----------+----------+---+
|montreal   |30        |1  |
|ny         |25        |2  |
+-----------+----------+---+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    相关资源
    最近更新 更多