Hive之資料查詢

2022-07-27 22:51:13 字數 2312 閱讀 6281

發布於:2013 年 10 月 11 日 

由 aaron 

發布於: hive

一,排序和聚合

對於排序有兩種方式,一種是order by 一種是sort by

order by 會對所有的資料進行排序,所以最後會只有乙個reducer來處理,如果資料量非常大,效率會非常差勁

sort by是部分排序,只是對乙個reducer的資料進行排序

from records2

select year, temperature

distribute by year

sort by year asc, temperature desc;

1949 111

1949 78

1950 22

1950 0

1950 -11

關鍵字distribute主要是控制特定的行會分發到同乙個reducer裡面去處理,這樣後面再進行聚合操作就很方便。

二,連線查詢

hive> select * from sales; -- name 購買者的名字,id購買的商品id

joe 2

hank 4

ali 0

eve 3

hank 2

hive> select * from things; --name 商品名稱 id 商品id

2 tie

4 coat

3 hat

1 scarf

1,內連線

hive> select sales.*, things.*

> from sales join things on (sales.id = things.id);

select sales.*, things.*

from sales, things

where sales.id = things.id;

需要注意的是對於mysql和oracel裡面常用的等值連線方式,hive是不支援的。

2,外連線

hive> select sales.*, things.*

> from sales left outer join things on (sales.id = things.id);

ali 0 null null

joe 2 2 tie

hank 2 2 tie

eve 3 3 hat

hank 4 4 coat

hive> select sales.*, things.*

> from sales right outer join things on (sales.id = things.id);

null null 1 scarf

joe 2 2 tie

hank 2 2 tie

eve 3 3 hat

hank 4 4 coat

hive> select sales.*, things.*

> from sales full outer join things on (sales.id = things.id);

ali 0 null null

null null 1 scarf

joe 2 2 tie

hank 2 2 tie

eve 3 3 hat

hank 4 4 coat

3,semi joins

先來看乙個查詢:

select *

from things

where things.id in (select id from sales);

注意:hive不支援這種在in中使用子查詢的語法,但是下面的查詢是同樣的意思

hive> select *

> from things left semi join sales on (sales.id = things.id);

但是使用semi join有一定的限制,就是右邊的表不允許出現在select中只能出現在on從句中

4,子查詢

對於子查詢hive有一些限制,只能在from裡面使用子查詢

例如:select total from

(select c1+c2 as total from table) my_sub_query;

子查詢必須指定乙個名字

Hive的資料查詢

與mysql和oracle中的sql語句不一樣地方會特別標註 hive簡單查詢 宣告如下 select all distinct select expr,select expr,from table reference where where conditon group by col list c...

hive的資料查詢

hive的資料查詢 查詢語法 select all distinct select expr,select expr,from table reference where where condition group by col list cluster by col list distribute...

Hive 05 資料查詢

select from table name select sid,sname from table name select sid sname sal sal 12from table name select sid sname sal comm,sal 12 nvl comm,0 from ta...