考虑包含列aid
和bid
的 Hive 表tbl
| aid | bid |
---------------
| | 12 |
| 24 | 13 |
| 18 | 3 |
| | 7 |
---------------
要求是当aid
为 null 或空字符串时,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
显示正确的数据,如上表所示
问题是将数据写回 Hive 时
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;
我仍然可以看到从查询返回多行,辅助为空
如果之前更新列值,如何将数据覆盖回 hive,就像上面的示例一样?
我会试试这个
df02.write
.orc
.mode(SaveMode.Overwrite)
.option("compression", "snappy")
.insertInto(tbl)