1. 程式人生 > >Hive DML ,分割槽表

Hive DML ,分割槽表

1.Hive構建在Hadoop之上的資料倉庫
sql ==> Hive ==> MapReduce
但是有些簡單基本的hive不呼叫mapreduce,就是不帶分組

2.分組函式:出現在select中的欄位,要麼出現在group by子句中,要麼出現在聚合函式中。

3.count(1) and count(欄位)
兩者的主要區別是
(1) count(1) 會統計表中的所有的記錄數,包含欄位為null 的記錄。
(2) count(欄位) 會統計該欄位在表中出現的次數,忽略欄位為null 的情況。即不統計欄位為null 的記錄。

4.(case when then else end ) 類似if-else,返回一列then
的結果.
union all 堆疊

5.看hive裡有哪些函式

hive (default)> show functions;

desc function extended xxx 檢視函式功能

轉換某個欄位的型別,如果轉換失敗,返回值就是null

cast(value as TYPE)

擷取一段字串,開始位置,擷取長度

substr(str,pos,len)

返回以.分割的連線

concat_ws('.','www','asd') 返回www.asd

返回長度,字串數字都可以

length()

把陣列分隔為多行

explode()

拆分,以a,d兩種分割符

split('asd.sdf','[a,d]')

用.分割的話要

hive (default)> select split('asd.asd','\\.');
OK
["asd","asd" ]

6.用hive函式完成一個wordcount

資料
asd,dsa,asd
asd,das

create table ruoze_wc(
sentence string
);

select word, count(1) as c
from
(
select explode(split(sentence,",")) as word from ruoze_wc
) t group by word
order by c desc;

split之後成了
[‘asd’,‘dsa’,‘asd’]
[‘asd’,‘das’]

explode後變成5行1列的形式

7.建立和陣列相關的表

1,doudou,化學:物理:數學:語文
2,dasheng,化學:數學:生物:生理:衛生
3,rachel,化學:語文:英語:體育:生物

create table ruoze_student(
id int,
name string,
subjects array<string>  數組裡裝string
)row format delimited fields terminated by ','
COLLECTION ITEMS TERMINATED BY ':';   陣列集合用:分割



load data local inpath '/home/hadoop/data/student.txt' into table ruoze_student;

hive (default)> select * from ruoze_student;
OK
1	doudou	["化學","物理","數學","語文"]
2	dasheng	["化學","數學","生物","生理","衛生"]
3	rachel	["化學","語文","英語","體育","生物"]

8.分割槽表

分割槽表:一個表按照某些欄位進行分割槽
解決問題:全盤掃描慢,分割槽定位掃描快

create table order_partition(
orderNumber string,
event_time string
)PARTITIONED BY(event_month string)   按照event_month分割槽
row format delimited fields terminated by '\t';

指定分割槽載入,資料表會多個分割槽列

load data local inpath '/home/hadoop/data/order.txt' into table order_partition PARTITION (event_month='2014-05');

如果報錯,key太長,需要修改字符集,在mysql裡改

use ruoze_d5;
alter table PARTITIONS convert to character set latin1;
alter table PARTITION_KEYS convert to character set latin1;

手動hdfs dfs 建立partitions分割槽,會找不到元資料,需要
MSCK REPAIR 分割槽表 ,這要刷所有分割槽,效能低,不用。

增加分割槽的辦法:

alter table order_partition add partition(event_month='2014-07');

檢視一個表的分割槽:

show partitions order_partition;

檢視如何建立的表

show create table xxx;

9.多級分割槽表

create table order_mulit_partition(
orderNumber string,
event_time string
)PARTITIONED BY(event_month string, step string)
row format delimited fields terminated by '\t';

load data local inpath '/home/hadoop/data/order.txt' into table order_mulit_partition PARTITION (event_month='2014-05',step='1');

10.動態分割槽

需求,按照deptno欄位寫進分割槽表裡

CREATE TABLE `ruoze_emp_partition`(
  `empno` int, 
  `ename` string, 
  `job` string, 
  `mgr` int, 
  `hiredate` string, 
  `sal` double, 
  `comm` double)
partitioned by(`deptno` int)
ROW FORMAT DELIMITED 
FIELDS TERMINATED BY '\t';

靜態匯入

insert into table ruoze_emp_partition PARTITION(deptno=10)
select empno,ename,job,mgr,hiredate,sal,comm from ruoze_emp where deptno=10;

假如有1000個deptno,豈不是要寫1000個匯入

動態匯入

分割槽欄位deptno要寫在最後,1句解決。

insert overwrite table ruoze_emp_partition PARTITION(deptno)
select empno,ename,job,mgr,hiredate,sal,comm,deptno from ruoze_emp;

啟動動態分割槽功能

hive> set hive.exec.dynamic.partition=true;