Hive中各種join的兩表關聯測試

2021-09-21 18:30:48 字數 3254 閱讀 5510

本篇博文主要是針對hive中join的部分,自己跑2張demo表,直觀檢視一下各種join的查詢結果,便於理解。

目錄

1. join 和 inner join

2. left join 和 left outer join

2.2. left outer join ... where xx is null

3. right join 和 right outer join

3.2 right outer join ... where xx is null

4. full outer join

4.2 full outer join ... where a.xx is null or b.xx is null 總結

首先建2張demo表,

表tmp1的字段a,b;

hive> select * from tmp1;

a b1 7

2 83 9

4 7

表tmp2的字段c,d;

hive> select * from tmp2;

c d1 10

4 12

5 11

hive> select * from tmp1 join tmp2 on tmp1.a=tmp2.c;

a b c d

1 7 1 10

4 7 4 12

hive> select * from tmp1 inner join tmp2 on tmp1.a=tmp2.c;

a b c d

1 7 1 10

4 7 4 12

結論:join和inner join完全等價,都是求兩張表的交集。

hive> select * from tmp1 left join tmp2 on tmp1.a=tmp2.c;

a b c d

1 7 1 10

2 8 null null

3 9 null null

4 7 4 12

hive> select * from tmp1 left outer join tmp2 on tmp1.a=tmp2.c;

a b c d

1 7 1 10

2 8 null null

3 9 null null

4 7 4 12

結論:left join和left outer join完全等價。

hive> select * from tmp1 left outer join tmp2 on tmp1.a=tmp2.c where tmp2.c is null;

a b c d

2 8 null null

3 9 null null

結論:left outer join...where xx is null,實現功能為去重,在tmp1表中,去除了on的字段相等的,tmp2表中的記錄。

求兩張表的差集。

hive> select * from tmp1 right join tmp2 on tmp1.a=tmp2.c;

a b c d

1 7 1 10

4 7 4 12

null null 5 11

hive> select * from tmp1 right outer join tmp2 on tmp1.a=tmp2.c;

a b c d

1 7 1 10

4 7 4 12

null null 5 11

結論:right join和right outer join完全等價。

hive> select * from tmp1 right outer join tmp2 on tmp1.a=tmp2.c where tmp1.a is null;

a b c d

null null 5 11

結論:與left outer join where xx is null類似,求兩張表的差集。

hive> select * from tmp1 full outer join tmp2 on tmp1.a=tmp2.c;

a b c d

1 7 1 10

2 8 null null

3 9 null null

4 7 4 12

null null 5 11

結論:full outer join是求兩張表的並集

hive> select * from tmp1 full outer join tmp2 on tmp1.a=tmp2.c where tmp1.a is null or tmp2.c is null;

a b c d

2 8 null null

3 9 null null

null null 5 11

結論:取並集中,a或者b的字段為null的記錄

(1)inner join和join等價,求兩表交集,關聯字段匹配且同時存在的記錄;

(2)a left join b和a left outer join b等價,返回記錄數與表a一致,關聯欄位不匹配的b表部分用null補全;

(2.2)a left outer join b where b.xx is null,求a表中剔除了b表中關聯字段匹配的部分,求兩表差集

(3)a right join b和a right outer join b等價,返回記錄數與表b一致,關聯欄位不匹配的a表部分用null補全;

(3.2)a right outer join b where a.xx is null,求b表中剔除了a表中關聯字段匹配的部分,求兩表差集

(4)full outer join,求兩表並集,關聯欄位不匹配的部門用null補全;

(5)full outer join ... where a.xx is null or b.xx is null,求兩表並集中,關聯字段任一表為null的部分。

hive兩個大表join操作

1 第一次優化,on 字段準換成型別相同 2 第二次優化,on 後面欄位的缺失率很高 為空 字段長度為零 字段填充了非整數 關聯欄位為無效字段,則不需要關聯 3 雖然設定了左表關聯欄位為空不去關聯右表,但是這樣做,左表中未關聯的記錄 欄位為空 將會全部聚集在乙個reduce中進行處理,體現為redu...

Hadoop 中的兩表join

作為資料分析中經常進行的join 操作,傳統dbms 資料庫已經將各種演算法優化到了極致,而對於hadoop 使用的mapreduce 所進行的join 操作,去年開始也是有各種不同的算 文出現,討論各種演算法的適用場景和取捨條件,本文討論hive 中出現的幾種join 優化,然後討論其他演算法實現...

Hadoop 中的兩表join

作為資料分析中經常進行的join 操作,傳統dbms 資料庫已經將各種演算法優化到了極致,而對於hadoop 使用的mapreduce 所進行的join 操作,去年開始也是有各種不同的算 文出現,討論各種演算法的適用場景和取捨條件,本文討論hive 中出現的幾種join 優化,然後討論其他演算法實現...