1. 程式人生 > >hive基礎命令和經典題總結

hive基礎命令和經典題總結

1.建庫:
create database mybd;
create database if not exists mydb;
create database if not exists mydb location ‘a’

2.查詢資料庫:
查詢庫列表:show databases;
查詢庫詳細資訊:desc database [extended] mydb

3.刪除資料庫:
drop database mydb;
drop database if exists mydb;
drop database if exists mydb [restrict cascade];

4.進入我們要操作的資料庫/切換庫
use mydb;

5.檢視資料庫裡面的表
show tables;
show tables in mydb;

6.新增表
1)建立內部表(Managered table)
create table mingxing( id int, name string, sex string) row format delimited fields terminated by ‘,’
2)建立外部表(External table)
create external table mingxing (id int, name string)
row format delimited fields terminated by ‘,’ location
‘/hive/qyl/hivedata’
3)建立分割槽表
create table mingxing_ptn(id int, name string) partitioned by (city string) row format delimited fields terminated bu ‘,’
注意:分割槽欄位不能是表中宣告的欄位,必須是一個新欄位。
4)建立分桶表
create table mingxing_bck(id int, name string,age int) clustered by(id) sorted by(age desc) into 4 buckets row format delimited fields terminated by ‘,’
注意:clustered裡面的欄位必須是表字段中出現的欄位,分桶欄位和排序欄位可以不一樣

7.刪除表
drop table mingxing;
drop table if exists mingxing;

8.對錶進行重新命名
alter table mingxing rename to student;

9.對錶的欄位進行操作(add,drop,change,replace)
增加欄位:
alter table mingxing add columns(province string);
修改欄位:
alter table mingxing change newage string;
alter table mingxing change newage string after id;
alter table mingxing change age newage string first;
替換欄位:
alter table mingxing replace columns(id int, name string) 替換所有欄位

10.對錶中的分割槽進行操作
增加分割槽:
alter table mingxing_ptn add partition(city=‘beijing’)
刪除分割槽:
alter table mingxing_ptn drop partition(city=“beijg”)

11.查詢顯示命令
檢視庫: show databases;
查看錶: show tables;
檢視建表完整語法:show create table mingxing
檢視內建函式庫:show functions
檢視函式的詳細手冊:desc function extended concat;
檢視分割槽: show partitions mingxing
查看錶的欄位: desc mingxing
查看錶的詳細資訊: desc extended mingxing
查看錶格式化之後的詳細資訊:desc formatted mingxing

12.load方式匯入資料
匯入本地相對路徑的資料:
load data local inpath ‘./student.txt’ into table mi;
load data local inpath './student.txt’overwrite into table student(覆蓋匯入)
匯入本地絕對路徑資料:
load data local inpath “/home/qyl/hivedata/student” into table mingxing;
匯入HDFS上的資料:
load data inpath ‘/student’ into table student;
注意:
1.匯入HDFS上的資料到hive表,表示截切,移動
2.匯入本地資料,相當於複製或者上傳

13、利用insert關鍵字往表中插入資料
單條資料插入:
insert into table mingxing values (001,‘huangbo’,‘male’,50,‘MA’);
單重插入模式: insert … select …
insert into table student select id,name,sex,age,department from mingxing;
注意:查詢出的欄位必須是student表中存在的欄位

多重插入模式:
from mingxing
insert into table student1 select id,name,sex,age
insert into table student2 select id,department;

靜態分割槽插入:
需要手動的建立分割槽
alter table student add partition (city=“zhengzhou”)
load data local inpath ‘/root/hivedata/student.txt’ into table student partition(city=‘zhengzhou’);

動態分割槽插入:
開啟動態分割槽的開關:set hive.exec.dynamic.partition = true;
設定動態分割槽插入模式:set hive.exec.dynamic.partition.mode = nonstrict
create table student(name string, department string) partitioned by (id int) …
insert into table student partition(id) select name,department,id from mingxing2;
student表字段:name,department, 分割槽欄位是id
查詢欄位是:name,department,id,分割槽欄位
注意:動態分割槽插入的分割槽欄位必須是查詢語句當中出現的欄位中的最後一個

CTAS(create table ... as select ...)(直接把查詢出來的結果儲存到新建的一張表裡)
create table student as select id,name,age,department from mingxing;
注意:自動新建的表中的欄位和查詢語句出現的欄位的名稱,型別,註釋一模一樣
限制:
1、不能建立外部表
2、不能建立分割槽表
3、不能建立分桶表

分桶插入:
建立分桶表:
create table mingxing(id int, name string, sex string, age int, department string)
clustered by(id) sorted by(age desc) into 4 buckets
row format delimited fields terminated by ‘,’;

插入資料:
insert into table mingxing select id,name,sex,age,department from mingxing2
distribute by id sort by age desc;
注意:查詢語句中的分桶資訊必須和分桶表中的資訊一致

14.like關鍵字使用:複製表結構
create table student like mingxing;

15.利用inset匯出資料到本地或者hdfs
單模式匯出資料到本地:
insert overwrite local directory ‘/root/outputdata’ select id,name,sex,age,department from mingxing;

16.清空資料庫表中的資料
truncate table mingxing2;

17.select 查詢
order by:全域性排序
sort by:區域性排序
distribute by:指定分桶欄位
cluster by :既分桶,也排序

18.join查詢
限制:
支援等值連線,不支援非等值連線
支援and操作,不支援or
支援超過2個表的連線
經驗:
當出現多個表進行連線是,最好把小表放置在前面!!
join分類:
inner join
left outer join
right outer join
full outer join
left semi join 它是in、exists的高效實現
select a.* from a left semi join b on a.id=b.id
等價於:
select a.* from a where a.id in (select b.id from b);

二、hive的高階語法
1.內建函式
日期函式 :
1. UNIX時間戳轉日期函式: from_unixtime
2. 獲取當前UNIX時間戳函式: unix_timestamp
3. 日期轉UNIX時間戳函式: unix_timestamp
4. 指定格式日期轉UNIX時間戳函式: unix_timestamp
字串函式:
1. 字元ascii碼函式:ascii
2. base64字串
3. 字串連線函式:concat
4. 帶分隔符字串連線函式:concat_ws
5. 陣列轉換成字串的函式:concat_ws
6. 小數位格式化成字串函式:format_number
7. 字串擷取函式:substr,substring

2.自定義函式
步驟:
1)自定義類繼承UDF,定義evaluate()方法
2)將此類打成jar包匯入到linux
3)在hive中 add jar ‘檔案路徑’
4)建立一個臨時函式 create temporary function zk as “類的全限定名稱”;
5)使用函式

3.hive的高階操作
1.視窗函式:
場景1:求到這個月為止,累計的銷售額或當月最大銷售額等

 SELECT cookieid,createtime,pv,SUM(pv) OVER(PARTITION BY cookieid ORDER BY createtime) AS pv1 from cookie;

結果: –其他AVG,MIN,MAX,和SUM用法一樣。
sum(pv)
cookie1 2015-04-10 1 1
cookie1 2015-04-11 5 6
cookie1 2015-04-12 7 13
cookie1 2015-04-13 3 16
cookie1 2015-04-14 2 18
cookie1 2015-04-15 4 22
cookie1 2015-04-16 4 26

場景2:統計一個cookie,pv數最多的前1/3天(ntile)
NTILE(n),用於將分組資料按照順序切分成n片,返回當前切片值
如果切片不均勻,預設增加第一個切片的分佈

SELECT cookieid, createtime, pv,
    NTILE(3) OVER(PARTITION BY cookieid ORDER BY pv DESC) AS rn 
FROM cookie2;

場景3:統計小於等於當前薪水的人數,所佔總人數的比例
–CUME_DIST 小於等於當前值的行數/分組內總行數
SELECT dept, userid, sal,
CUME_DIST() OVER(ORDER BY sal) AS rn1, //小於當前值所佔的比例
CUME_DIST() OVER(PARTITION BY dept ORDER BY sal) AS rn2 //分組後
FROM cookie3;
// 結果:
d1 user1 1000 0.2 0.3333333333333333
d1 user2 2000 0.4 0.6666666666666666
d1 user3 3000 0.6 1.0
d2 user4 4000 0.8 0.5
d2 user5 5000 1.0 1.0

2.hive中行轉列,列轉行的方法

列轉行;case (欄位) when (某個值) then (成立的時候的值) else (不成立的值) end [重新命名欄位])
例子:
張三 數學 89 1
張三 語文 80 1
張三 英語 70 1
李四 數學 90 2
李四 語文 70 2
李四 英語 80 2
1)select id,name,sum(shuxue),sum(yuwen),sum(english) from
(select id,name,
case course when “數學” then score else 0 end shuxue ,
case course when “語文” then score else 0 end yuwen,
case course when “英語” then score else 0 end english
from student) a group by id,name;
2)select id,name,concat_ws(’,’,collect_set(score))
> from student
> group by id,name;

行轉列:炸裂函式(explode)
select id,name,allscore from student a
lateral view explode(split(score,",")) b as allscoure;