我有这样的要求:如果列没有出现,我们需要创建新的列,如果列已经存在,那么让它是相同的。
如何通过使用spark scala来实现这个要求。
你可以使用 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)