Spark Scala: 检查列是否存在[重复]。

问题描述 投票:0回答:1

我有这样的要求:如果列没有出现,我们需要创建新的列,如果列已经存在,那么让它是相同的。

如何通过使用spark scala来实现这个要求。

scala apache-spark apache-spark-sql contains
1个回答
0
投票

你可以使用 if else 列的条件


scala> tbl.printSchema
root
 |-- name: string (nullable = true)
 |-- age: integer (nullable = true)
 |-- joining_dt: date (nullable = true)


scala> val columns = tbl.columns
scala> val df = if(columns contains "id") tbl else tbl.withColumn("id", lit(1))
scala> df.printSchema
root
 |-- name: string (nullable = true)
 |-- age: integer (nullable = true)
 |-- joining_dt: date (nullable = true)
 |-- id: integer (nullable = false)

© www.soinside.com 2019 - 2024. All rights reserved.