scala - 在 Spark Scala 中重命名 DataFrame 的列名称

标签 scala apache-spark dataframe apache-spark-sql

我正在尝试转换 Spark-Scala 中 DataFrame 的所有标题/列名称。截至目前,我想出了以下代码,它仅替换单个列名称。

for( i <- 0 to origCols.length - 1) {
  df.withColumnRenamed(
    df.columns(i), 
    df.columns(i).toLowerCase
  );
}

最佳答案

如果结构是扁平的:

val df = Seq((1L, "a", "foo", 3.0)).toDF
df.printSchema
// root
//  |-- _1: long (nullable = false)
//  |-- _2: string (nullable = true)
//  |-- _3: string (nullable = true)
//  |-- _4: double (nullable = false)

您可以做的最简单的事情就是使用 toDF 方法:

val newNames = Seq("id", "x1", "x2", "x3")
val dfRenamed = df.toDF(newNames: _*)

dfRenamed.printSchema
// root
// |-- id: long (nullable = false)
// |-- x1: string (nullable = true)
// |-- x2: string (nullable = true)
// |-- x3: double (nullable = false)

如果您想重命名各个列,可以使用 selectalias:

df.select($"_1".alias("x1"))

可以很容易地推广到多列:

val lookup = Map("_1" -> "foo", "_3" -> "bar")

df.select(df.columns.map(c => col(c).as(lookup.getOrElse(c, c))): _*)

withColumnRenamed:

df.withColumnRenamed("_1", "x1")

foldLeft 一起使用来重命名多个列:

lookup.foldLeft(df)((acc, ca) => acc.withColumnRenamed(ca._1, ca._2))

对于嵌套结构(structs),一种可能的选择是通过选择整个结构来重命名:

val nested = spark.read.json(sc.parallelize(Seq(
    """{"foobar": {"foo": {"bar": {"first": 1.0, "second": 2.0}}}, "id": 1}"""
)))

nested.printSchema
// root
//  |-- foobar: struct (nullable = true)
//  |    |-- foo: struct (nullable = true)
//  |    |    |-- bar: struct (nullable = true)
//  |    |    |    |-- first: double (nullable = true)
//  |    |    |    |-- second: double (nullable = true)
//  |-- id: long (nullable = true)

@transient val foobarRenamed = struct(
  struct(
    struct(
      $"foobar.foo.bar.first".as("x"), $"foobar.foo.bar.first".as("y")
    ).alias("point")
  ).alias("location")
).alias("record")

nested.select(foobarRenamed, $"id").printSchema
// root
//  |-- record: struct (nullable = false)
//  |    |-- location: struct (nullable = false)
//  |    |    |-- point: struct (nullable = false)
//  |    |    |    |-- x: double (nullable = true)
//  |    |    |    |-- y: double (nullable = true)
//  |-- id: long (nullable = true)

请注意,它可能会影响可空性元数据。另一种可能性是通过转换重命名:

nested.select($"foobar".cast(
  "struct<location:struct<point:struct<x:double,y:double>>>"
).alias("record")).printSchema

// root
//  |-- record: struct (nullable = true)
//  |    |-- location: struct (nullable = true)
//  |    |    |-- point: struct (nullable = true)
//  |    |    |    |-- x: double (nullable = true)
//  |    |    |    |-- y: double (nullable = true)

或者:

import org.apache.spark.sql.types._

nested.select($"foobar".cast(
  StructType(Seq(
    StructField("location", StructType(Seq(
      StructField("point", StructType(Seq(
        StructField("x", DoubleType), StructField("y", DoubleType)))))))))
).alias("record")).printSchema

// root
//  |-- record: struct (nullable = true)
//  |    |-- location: struct (nullable = true)
//  |    |    |-- point: struct (nullable = true)
//  |    |    |    |-- x: double (nullable = true)
//  |    |    |    |-- y: double (nullable = true)

关于scala - 在 Spark Scala 中重命名 DataFrame 的列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35592917/

相关文章:

scala - 使用 Flink 计算流中有状态实体的最新状态

scala - scala 'match' 编译规则的误解

scala - Intellij : "Error running Scala Console: Cannot Start Process"

scala - 子类型以外的模糊隐式解决方案

apache-spark - 使用内置插件在 spark SQL 中将一组 map 合并/连接成一个 map

pandas - 替换列名称中的部分字符串

hadoop - Spark : Out Of Memory Error when I save to HDFS

apache-spark - spark中每个工作节点运行多少个执行程序进程?

python-3.x - 使用 np.where 或 loc 更新 pandas 数据框的多列

python - 将字典转换为数据帧的一列,同时将字典行名称保留在另一列中(python)