hive查詢語句

Hive查詢

1、hive模糊搜索表
show tables like '*name*';
2、查看錶結構信息
desc formatted tablename;
desc table_name
3、查看分區信息
show partitions tablename;
4、根據分區查詢數據
select table_coulm from tablename where partitionname = '2016-02-25';
5、 刪除分區
alter table test drop partition(dt='2016-03-01');
alter table  test drop partition(dt='2016-01-17')
6、 殺死某個任務  不在hive shell中執行
Hadoop job -kill job_201403041453_58315
7、 hive命令行操作
執行一個查詢,在終端上顯示mapreduce的進度,執行完畢後,最後把查詢結果輸出到終端上,接着hive進程退出,不會進入交互模式。
hive -e 'select table_cloum from table'
-S,終端上的輸出不會有mapreduce的進度,執行完畢,只會把查詢結果輸出到終端上。這個靜音模式很實用,,通過第三方程序調用,第三方程序通過hive的標準輸出獲取結果集。
hive -S -e 'select tablecloum from table'
執行sql文件
hive -f hive_sql.sql
8、查看文件大小及刪除文件
Hive>dfs –du /xx/xx
Hive>dfs –rmr /xx/xx
9、mapjoin的使用 應用場景:1.關聯操作中有一張表非常小 2、不等值的鏈接操作
select /*+ mapjoin(A)*/ f.a,f.b from A t join B f  on ( f.a=t.a and f.ftime=20110802) 
10、hive開啓簡單模式不啓用mr
set hive.fetch.task.conversion=more;
11、hive修改表名
ALTER TABLE oldtablename RENAME TO newtablename;
12、 hive添加字段
alter table temp add columns(current_session_timelenth_count bigint comment '頁面停留總時長');
ALTER temp CHANGE current_session_timelenth current_session_timelenth bigint comment '當前會話停留時間';
------------------------------------------------------------------------------------------ 
Hive支持大量SQL數據定義語言(Data Manipulation Language,DML)中的現有功能,包括以下各種語句:
使用where條件過濾表的行
使用列或子查詢的select表達式
使用等值連接,連接多張表
合併表的所有行或子查詢
針對多個“分組”的列進行聚合計算
將查詢結果存入另一張表
導出表中的內容到本地目錄或HDFS目錄中
以下參考自點擊打開鏈接
1、只查詢前兩條:
select  * from  student  limit  2 ;
2、統計一個表的行數:
select  count(*)  from student ;
3、求一個表id字段的id 之和:
select  sum(id)  from  student ;
4、查詢分區表
select  *  from  beauties  where  nation='China' ;     
5、多表關聯:
select  t . account ,  u . name , t . income , t . expenses , t . surplus from  user_info  u  join  (select  account ,  sum(income)  as  income ,  sum(expenses) as  expenses , sum(income-expenses)  as  surplus  from 
trade_detail  group  by  account)  t  on  u . account  =  t . account ;
別名
select count(distinct e.uid) from (select * from tablenamewhere
rank <=3 and order =1) e;
小括號中返回的也是一個表,它只是臨時的 別名爲e
查搜索過"奧巴馬" 的用戶所搜過的關鍵字
select  m.uid,m.keyword  from (select  distinct n.uid from
tablenamewhere keyword like '%奧巴馬%' n ) m  
where m.uid=n.uid;
查搜索過"奧巴馬" 的用戶所搜過的不包含"奧巴馬"本身的關鍵字
select m.uid,m.keyword from sogou_20111230 m join (select distinct uid from sogou_20111230 where keyword like '%奧巴馬%') n on m.uid=n.uid where m.keyword not like '%奧巴馬%';
UNION ALL可以將2個或多個表進行合併
select count(distinct e.uid)from(
select * from tablename where rank<11 
union all 
select * from ext_sogou_20111230_limit3 where rank < 11) e;
6、去重查詢:group   by的使用
select  *  from  mytable  group  by  uid ;
Group by 語句通常會和聚合函數一起使用,按照一個或者多個對結果進行分組,然後對每個組執行聚合操作
select year(ts), avg(rank) from tablename where ts like '%2011' group by year(ts);
7、獨立UID總數:
select  count(distinct(uid)) from  mytable ; (高效) 或者    select  count(*) from(select  *  from mytable  group  by  uid)  a ;
8、查詢頻度排名(頻度最高的前50):
select keyword,count(*) as cnt from test group by keyword order by cnt desc limit 50;
9、 添加防止刪除的保護:
alter table tablename
> partition (day='0925') enable no_drop;
刪除添加的"刪除"保護:
alter table tablename
> partition (day='20161207') disable no_drop;
10、添加防止查詢的保護:
alter table tablename
> partition (day=20161207') enable offline;
刪除防止查詢的保護:
alter table tablename
> partition (day='20161207') disable offline;
11、強轉:
select cast(rank as DOUBLE) from tablename limit 10;
12、 拼接:
select concat(uid,url) from tablename limit 10;
13、 查找url字符串中的5位置之後字符串str第一次出現的位置
select locate("str",url,5) from tablename limit 100;
14、 抽取字符串str中符合正則表達式url的第5個部分的子字符串
select regexp_extract("str",url,5) from tablename limit 100; 
15、  按照正則表達式"0"分割字符串uid,並將分割後的部分以字符串數組的方式返回
select split(uid,"0") from tablename limit 100;
16、  對字符串url,從0處開截取長度爲3的字符串,作爲其子字符串
select substr(url,0,3) from tablename limit 3; 
17、 將字符串url中所有的字母轉換成大寫字母
select upper(url) from tablename limit 3;
18、 where  ..and  或者 where ....or   where的 兩種條件查詢 
select * from  tablename where rank<=3 and order =1 limit 3;
select * from  tablenamewhere rank !=0 or order =1 limit 3;
19、like 過濾字符串 
select *  from  tablename where url like '%http%' limit 10;
rlike 通過Java的正則表達式過濾  *與%功能一樣 ,它是hive中擴展功能的操作符
select * from tablenamewhere url rlike  ' .*http.* ' limit 3; 
20、left semi-join 左半表 semi 半掛的 半獨立的
select * from be where rank in(1,2,5);
select  * from  tablenamem left semi join  ext_sogou_20111230_limit3  n on m.rank=n.rank;
21、視圖 hive只支持邏輯視圖 作用降低查詢複雜度
創建視圖
create view sogou_view  as 
select * from tablenamewhere rank <=3;
22、 索引 
Hive的索引需要單獨創建表實現
創建索引
CREATE INDEX employees_index ON TABLE employees (name) AS 
'org.apache.hadoop.hive.ql.index.compact.CompactIndexHandler'
WITH DEFERRED REBUILD IDXPROPERTIES('creator' = 'me','
created_at '='some  time') IN TABLE employees_index_table;
23、上午7-9點之間,搜索過“百度”的用戶,哪些用戶直接點擊了百度的URL
老師:
select distinct n.uid from (select * from sogou_view where keyword ='百度')
and  substr(ts,9,2) in ('07','08','09')) n where n.url like '%baidu.com%';
select uid  from sogou_view where (cast(substr(ts,9,2) 
as int)>7  or cast(substr(ts,9,2) as int)<9) and url 
like '%www.ganji.com%' or keyword like '%百度%' ;
select uid  from sogou_view where substr(ts,9,2) in ('07','08','09') and url
like '%www.ganji.com%' and  keyword like '%百度%' ;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章