首页 > 解决方案 > 如何从 Hive 中的两个表中获取不匹配的记录?

问题描述

我有以下两个数据表,我只需要使用 hive 获取不匹配的数据记录。

表格1:

hive> select * from dept;
OK
10      ACCOUNTING      NEW YORK
20      RESEARCH        DALLAS
30      SALES   CHICAGO
40      OPERATIONS      BOSTON

表2:

hive> select * from dept_text;
OK
10      ACCOUNTING      NEW YORK
20      RESEARCH        DALLAS
30      SALES   CHICAGO
40      OPERATIONS      BOSTON
50      Software        Bangalore
60      Housewife       yellandu

输出:我需要得到如下输出。有人可以帮我解决这个问题吗?

50      Software        Bangalore
60      Housewife       yellandu

标签: hivehiveql

解决方案


left joindept_text表上使用,然后仅过滤dept表中的空 id

select dt.* from dept_text dt 
    left join 
dept d 
  on d.id=dt.id 
where d.id is null;

Example:

desc dept;
--id                    int
--desc                  string
--city                  string

select * from dept;
--OK
--dept.id   dept.desc   dept.city
--10    ACCOUNTING  NEW YORK
--20    RESEARCH    DALLAS
--30    SALES   CHICAGO
--40    OPERATIONS  BOSTON

--if you want to join on desc column

select dt.* from dept_text dt 
left join 
dept d 
on d.desc=dt.desc 
where d.id is null;

--or if you want to join on id column

select dt.* from dept_text dt 
left join 
dept d 
on d.id=dt.id 
where d.id is null;

推荐阅读