美文网首页
Hive中的各种join

Hive中的各种join

作者: ForgetThatNight | 来源:发表于2018-04-21 23:13 被阅读115次

    准备数据

    1,a
    2,b
    3,c
    4,d
    7,y
    8,u
    
    2,bb
    3,cc
    7,yy
    9,pp
    

    建表:

    create table a(id int,name string)
    row format delimited fields terminated by ',';
    
    create table b(id int,name string)
    row format delimited fields terminated by ',';
    

    导入数据:

    load data local inpath '/home/hadoop/a.txt' into table a;
    load data local inpath '/home/hadoop/b.txt' into table b;
    

    实验:

    inner join

    select * from a inner join b on a.id=b.id;
    +-------+---------+-------+---------+--+
    | a.id  | a.name  | b.id  | b.name  |
    +-------+---------+-------+---------+--+
    | 2     | b       | 2     | bb      |
    | 3     | c       | 3     | cc      |
    | 7     | y       | 7     | yy      |
    +-------+---------+-------+---------+--+
    
    

    left join

    select * from a left join b on a.id=b.id;
    +-------+---------+-------+---------+--+
    | a.id  | a.name  | b.id  | b.name  |
    +-------+---------+-------+---------+--+
    | 1     | a       | NULL  | NULL    |
    | 2     | b       | 2     | bb      |
    | 3     | c       | 3     | cc      |
    | 4     | d       | NULL  | NULL    |
    | 7     | y       | 7     | yy      |
    | 8     | u       | NULL  | NULL    |
    +-------+---------+-------+---------+--+
    
    

    right join

    select * from a right join b on a.id=b.id;
    

    full outer join

    select * from a full outer join b on a.id=b.id;
    +-------+---------+-------+---------+--+
    | a.id  | a.name  | b.id  | b.name  |
    +-------+---------+-------+---------+--+
    | 1     | a       | NULL  | NULL    |
    | 2     | b       | 2     | bb      |
    | 3     | c       | 3     | cc      |
    | 4     | d       | NULL  | NULL    |
    | 7     | y       | 7     | yy      |
    | 8     | u       | NULL  | NULL    |
    | NULL  | NULL    | 9     | pp      |
    +-------+---------+-------+---------+--+
    

    left semi join--inner join只取左边的一部分

    select * from a left semi join b on a.id = b.id;
    +-------+---------+--+
    | a.id  | a.name  |
    +-------+---------+--+
    | 2     | b       |
    | 3     | c       |
    | 7     | y       |
    +-------+---------+--+
    

    相关文章

      网友评论

          本文标题:Hive中的各种join

          本文链接:https://www.haomeiwen.com/subject/yyoflftx.html