學習筆記(3):大數據之Hive-連接查詢

立即學習:https://edu.csdn.net/course/play/8005/164135?utm_source=blogtoedu

建表

create table customers(id int,name string,age int);
insert into customers(id,name,age) values(1,'gxf',23);
create table orders(id int,cid int,orderno int,price float);
insert into orders(id,cid,orderno,price) values(1,1,1,1.2);
insert into orders(id,cid,orderno,price) values(1,1,2,3.2);

左半連接

左半連接left semi-join, select 和 where 子句不能引用到右邊表字段。

左表的記錄在右表中一旦找到對應的記錄,右側表立即停止,效率比內連接效率高

hive不支持右半連接操作

select c.id,c.name from customers c left semi join orders o on c.id = o.cid

笛卡爾鏈接m*n

select c.id,c.name from customers c join orders o;

map端連接

map端連接,通過mapper的手段,將一張小表完全載入內存中。

Hive中的 Map Join 即map side join

工作原理是在Map端把小表加載到內存中,然後讀取大表,和內存中的小表完成連接操作。MapJoin使用了分佈式緩存技術。

Map Join的優點:

  1. 不消耗集羣的reduce資源。
  2. 減少了reduce操作,加快了程序執行。
  3. 降低網絡負載。

Map Join的缺點:

  1. 佔用內存(所以加載到內存中的表不能過大,因爲每個計算節點都會加載一次)。
  2. 生成較多的小文件。
select /*+mapjoin(c)*/ c.id,c.name,o.orderno from customers c join orders o;
select /*+mapjoin(o)*/ c.id,c.name,o.orderno from customers c join orders o;

set hive.mapjoin.smalltable.filesize=25000000; --設置小表閥值

注意: set 命令只對當前會話有用,要持久化需要修改 hive-site.xml

union all 聯合操作

select id, name from customers 
union all 
select id, orderno from orders;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章