考虑具有列tbl
和aid
的配置单元表bid
| aid | bid |
---------------
| | 12 |
| 24 | 13 |
| 18 | 3 |
| | 7 |
---------------
要求是当aid
为空或空字符串时,aid
应该被bid
的值覆盖
| aid | bid |
---------------
| 12 | 12 |
| 24 | 13 |
| 18 | 3 |
| 7 | 7 |
---------------
代码很简单
val df01 = spark.sql("select * from db.tbl")
val df02 = df01.withColumn("aid", when(col("aid").isNull || col("aid") <=> "", col("bid")) otherwise(col("aid")))
并且在spark-shell
中运行时,df02.show
就像上表一样显示正确的数据
问题是将数据写回配置单元时
df02.write
.format("orc")
.mode("Overwrite")
.option("header", "false")
.option("orc.compress", "snappy")
.insertInto(tbl)
没有错误,但是当我验证数据时
select * from db.tbl where aid is null or aid= '' limit 10;
我仍然可以看到查询返回了多行,并且帮助为null
如果像以前的示例那样以前更新列值,如何将数据覆盖回配置单元?
我将尝试如下
df02.write
.orc
.mode(SaveMode.Overwrite)
.option("compression", "snappy")
.insertInto(tbl)