【发布时间】:2018-10-26 05:19:23
【问题描述】:
如果现有数据存在,我想根据某些条件更新现有数据(应更新优先级较高的数据),如果不存在则插入新数据。
我已经为此编写了一个查询,但不知何故它复制了行数。以下是对我所拥有的以及我想要实现的目标的完整说明:
我有什么: 表 1 - 列 - id,info,priority
hive> select * from sample1;
OK
1 123 1.01
2 234 1.02
3 213 1.03
5 213423 1.32
Time taken: 1.217 seconds, Fetched: 4 row(s)
表 2:列 - id、info、priority
hive> select * from sample2;
OK
1 1234 1.05
2 23412 1.01
3 21 1.05
4 1232 1.1
2 3432423 1.6
3 34324 1.4
我想要的是最终表应该只有 1 行每个 id 与数据根据最大优先级:
1 1234 1.05
2 3432423 1.6
3 34324 1.4
4 1232 1.1
5 213423 1.32
我写的查询是这样的:
insert overwrite table sample1
select a.id,
case when cast(TRIM(a.prio) as double) > cast(TRIM(b.prio) as double) then a.info else b.info end as info,
case when cast(TRIM(a.prio) as double) > cast(TRIM(b.prio) as double) then a.prio else b.prio end as prio
from sample1 a
join
sample2 b
on a.id=b.id where b.id in (select distinct(id) from sample1)
union all
select * from sample2 where id not in (select distinct(id) from sample1)
union all
select * from sample1 where id not in (select distinct(id) from sample2);
运行此查询后,我得到以下结果:
hive> select * from sample1;
OK
1 1234 1.05
2 234 1.02
3 21 1.05
2 3432423 1.6
3 34324 1.4
5 213423 1.32
4 1232 1.1
如何修改当前查询以获得正确的结果。我可以遵循任何其他方法/过程来实现最终结果。我正在使用 hadoop 2.5.2 和 HIVE 1.2.1 。我正在开发一个有 5 个从属设备和 1 个 NN 的 6 节点集群。
【问题讨论】:
-
因为 p_gender 持有一个双精度值,但数据类型是字符串,我们需要与双精度值进行比较,所以我们需要将其转换为字符串来保存比较。
-
请看我更新的答案
标签: hadoop hive hiveql hadoop2