3 个回答
主要使用merge into函数,例如:
spark.sql(
"""
|merge into test1.default.users1 t1
|using (select id,name,age,cts from test1.default.users2) t2
|on t1.id = t2.id
|when matched and t2.tp = 'delete' then delete
|when matched and t2.tp = 'update' then update set t1.name = t2.name,t1.age = t2.age
|when not matched then insert (id,name,age) values (t2.id,t2.name,t2.age)
""".stripMargin)
发布于:8个月前 (01-25) IP属地:未知
解释下这个sql:
spark.sql(
"""
|merge into test1.default.users1 t1 //准备更新或者删除的表数据
|using (select id,name,age,cts,type from test1.default.users2) t2 //待删除的数据(这里简单称为临时表),相当于做join
|on t1.id = t2.id //相当于做join
|when matched and t2.type = 'delete' then delete //如果临时表对应的值是删除,则直接删除表数据
|when matched and t2.type = 'update' then update set t1.name = t2.name,t1.age = t2.age //如果临时表对应的值是更新,则直接删除表数据
|when not matched then insert (id,name,age) values (t2.id,t2.name,t2.age) //如果临时表对应的值不匹配,则进行插入数据
""".stripMargin)
发布于:8个月前 (01-25) IP属地:未知
我来回答
您需要 登录 后回答此问题!