scala - Spark更改DF架构列从点重命名为下划线

标签 scala apache-spark apache-spark-sql

我有一个数据框,其列名称包含。 示例:df.printSchema

user.id_number
user.name.last
user.phone.mobile

etc,我想通过用 _ 替换 dot 来重命名架构。

user_id_number
user_name_last
user_phone_mobile

注意:此 DF 的输入数据为 JSON 格式(与 NoSQL 等非关系型数据)

最佳答案

使用 .map,.withColumnRenamed. 替换为 _

示例:

val df=Seq(("1","2","3")).toDF("user.id_number","user.name.last","user.phone.mobile")
df.toDF(df.columns.map(x =>x.replace(".","_")):_*).show()

//using replaceAll
df.toDF(df.columns.map(x =>x.replaceAll("\\.","_")):_*).show()
//+--------------+--------------+-----------------+
//|user_id_number|user_name_last|user_phone_mobile|
//+--------------+--------------+-----------------+
//|             1|             2|                3|
//+--------------+--------------+-----------------+

2。使用 selectExpr:

val expr=df.columns.map(x =>col(s"`${x}`").alias(s"${x}".replace(".","_")).toString)

df.selectExpr(expr:_*).show()
//+--------------+--------------+-----------------+
//|user_id_number|user_name_last|user_phone_mobile|
//+--------------+--------------+-----------------+
//|             1|             2|                3|
//+--------------+--------------+-----------------+

3.使用.withColumnRenamed:

df.columns.foldLeft(df){(tmpdf,col) =>tmpdf.withColumnRenamed(col,col.replace(".","_"))}.show()
//+--------------+--------------+-----------------+
//|user_id_number|user_name_last|user_phone_mobile|
//+--------------+--------------+-----------------+
//|             1|             2|                3|
//+--------------+--------------+-----------------+

关于scala - Spark更改DF架构列从点重命名为下划线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62744361/

相关文章:

scala - 在 ScalaQuery 中重用 session ?

scala - 如何将KeyValue列表发送到Kafka?

scala - 创建一个 RDD 来收集迭代计算的结果

python - 错误: Must specify a primary resource (JAR or Python file) - Spark submit Python app

apache-spark - pyspark 将数据添加到 TempTable

Scala play - "not found: value routes"(Eclipse 和 IDEA)

apache-spark - 读取或写入 Parquet 格式数据时出错

scala - Spark 和 Azure-Cosmosdb 的跨版本冲突

postgresql - 如何使用 PySpark 将 JSON 列类型写入 Postgres?

scala - 将多个 map 与 map 值合并为自定义案例类实例