Hive 簡單應用

一 Hive 使用方式

1.1.Hive shell

直接在hive的安裝目錄下,然後輸入hql語句就可以了

bin/hive

1.2.通過Hive thrift服務啓動,啓動後可以遠程連接

啓動爲前臺:
bin/hiveserver2
啓動爲後臺:(1代表標準輸出,2代表錯誤輸出,標準輸出和錯誤輸出分別重定向到不同的日誌文件)
nohup bin/hiveserver2 1>/var/log/hiveserver.log 2>/var/log/hiveserver.err &

啓動後,連接
# 方式1
hive/bin/beeline  回車,進入beeline的命令界面
輸入命令連接hiveserver2
beeline> !connect jdbc:hive2//master:10000
(master是hiveserver2所啓動的那臺主機名,端口默認是10000)
方式2
或者啓動就連接:
bin/beeline -u jdbc:master://mini1:10000 -n hadoop

2.hive基本操作

2.1 DDL操作

2.1.1 創建表
基本的語法
項目實例

  • 創建內表
  • 創建外表
  • 創建分區表
  • 創建分桶表

建表語法

CREATE [EXTERNAL] TABLE [IF NOT EXISTS] table_name

[(col_name data_type [COMMENT col_comment], …)]

[COMMENT table_comment]

[PARTITIONED BY (col_name data_type [COMMENT col_comment], …)]

[CLUSTERED BY (col_name, col_name, …)

[SORTED BY (col_name [ASC|DESC], …)] INTO num_buckets BUCKETS]

[ROW FORMAT row_format]

[STORED AS file_format]

[LOCATION hdfs_path]

說明:

1、 CREATE TABLE 創建一個指定名字的表。如果相同名字的表已經存在,則拋出異常;用戶可以用 IF NOT EXISTS 選項來忽略這個異常。

2、 EXTERNAL關鍵字可以讓用戶創建一個外部表,在建表的同時指定一個指向實際數據的路徑(LOCATION),Hive 創建內部表時,會將數據移動到數據倉庫指向的路徑;若創建外部表,僅記錄數據所在的路徑,不對數據的位置做任何改變。在刪除表的時候,內部表的元數據和數據會被一起刪除,而外部表只刪除元數據,不刪除數據。

3、 LIKE 允許用戶複製現有的表結構,但是不復制數據。

4、 ROW FORMAT

DELIMITED [FIELDS TERMINATED BY char] [COLLECTION ITEMS TERMINATED BY char]

    [MAP KEYS TERMINATED BY char] [LINES TERMINATED BY char]

| SERDE serde_name [WITH SERDEPROPERTIES (property_name=property_value, property_name=property_value, …)]

用戶在建表的時候可以自定義 SerDe 或者使用自帶的 SerDe。如果沒有指定 ROW FORMAT 或者 ROW FORMAT DELIMITED,將會使用自帶的 SerDe。在建表的時候,用戶還需要爲表指定列,用戶在指定表的列的同時也會指定自定義的 SerDe,Hive通過 SerDe 確定表的具體的列的數據。

5、 STORED AS

SEQUENCEFILE|TEXTFILE|RCFILE

如果文件數據是純文本,可以使用 STORED AS TEXTFILE。如果數據需要壓縮,使用 STORED AS SEQUENCEFILE。

6、CLUSTERED BY

對於每一個表(table)或者分區, Hive可以進一步組織成桶,也就是說桶是更爲細粒度的數據範圍劃分。Hive也是 針對某一列進行桶的組織。Hive採用對列值哈希,然後除以桶的個數求餘的方式決定該條記錄存放在哪個桶當中。

把表(或者分區)組織成桶(Bucket)有兩個理由:

(1)獲得更高的查詢處理效率。桶爲表加上了額外的結構,Hive 在處理有些查詢時能利用這個結構。具體而言,連接兩個在(包含連接列的)相同列上劃分了桶的表,可以使用 Map 端連接 (Map-side join)高效的實現。比如JOIN操作。對於JOIN操作兩個表有一個相同的列,如果對這兩個表都進行了桶操作。那麼將保存相同列值的桶進行JOIN操作就可以,可以大大較少JOIN的數據量。

(2)使取樣(sampling)更高效。在處理大規模數據集時,在開發和修改查詢的階段,如果能在數據集的一小部分數據上試運行查詢,會帶來很多方便。

2.1.1.1創建內部表

use ods_freemud;
create table ods_freemud.ods_ext_csv_unc_dmp_freemud_active
(
active_product_no           string    comment "18位活動產品編號",
active_product_name         string    comment "產品名稱" ,
active_type                string    comment "活動類型"

)
row format delimited fields terminated by ',' lines terminated by '\n'
stored as textfile

2.1.1.2 創建外部表

create external table ods_freemud.ods_ext_csv_unc_dmp_freemud_active
(
active_product_no           string    comment "18位活動產品編號",
active_product_name         string    comment "產品名稱" ,
active_type                string    comment "活動類型"

)
row format delimited fields terminated by ',' lines terminated by '\n'
stored as textfile
LOCATION '/data/data_coe/data_asset/prod/db/ods_freemud/ods_ext_csv_unc_dmp_freemud_active/';

2.1.1.3 創建分區表

use ods_freemud;
create external table ods_freemud.ods_ext_csv_unc_dmp_freemud_active
(
active_product_no           string    comment "18位活動產品編號",
active_product_name         string    comment "產品名稱" ,
active_type                string    comment "活動類型"

)
partitioned by (dt STRING)
row format delimited fields terminated by ',' lines terminated by '\n'
stored as textfile
LOCATION '/data/data_coe/data_asset/prod/db/ods_freemud/ods_ext_csv_unc_dmp_freemud_active/';

2.1.1.4 創建分桶表

use ods_freemud;
create table ods_freemud.ods_ext_csv_unc_dmp_freemud_active
(
active_product_no           string    comment "18位活動產品編號",
active_product_name         string    comment "產品名稱" ,
active_type                string    comment "活動類型"

)
partitioned by (dt STRING)
clustered by(active_product_no) sorted by(active_product_name) into 2 buckets
row format delimited fields terminated by ',' lines terminated by '\n'
stored as textfile

hive 實例

  • 求每個月每個顧客的消費總額
  • 求每個月每個顧客的累積銷售額
建表語句
use tmp;
create table tmp.ods_ext_csv_unc_tmp_customer_sale(
    customer_name string,
    sale_date string,
    total_sale int
)
row format delimited fields terminated by ',' lines terminated by '\n'
stored as textfile;

數據樣例
"A","2015-01-02",5
"A","2015-01-03",15
"B","2015-01-01",5
"A","2015-01-04",8
"B","2015-01-05",25
"A","2015-01-06",5
"A","2015-02-02",4
"A","2015-02-06",6
"B","2015-02-06",10
"B","2015-02-07",5

插入數據
insert into tmp.ods_ext_csv_unc_tmp_customer_sale(customer_name, sale_date, total_sale)
values ("A","2015-01-02",5),("A","2015-01-03",15),("B","2015-01-01",5),("A","2015-01-04",8),("B","2015-01-05",25),("A","2015-01-06",5),("A","2015-02-02",4),("A","2015-02-06",6),("B","2015-02-06",10),("B","2015-02-07",5);

每個顧客,每個月消費額
select customer_name,substring(sale_date,1, 7), sum(total_sale)  from tmp.ods_ext_csv_unc_tmp_customer_sale group by customer_name, substring(sale_date,1, 7);

查詢結果
A	2015-01	33
A	2015-02	10
B	2015-01	30
B	2015-02	15

每個顧客,每個月消費額和累計消費額
select left_table.customer_name, left_table.sale_date, left_table.month_count, sum(left_table.month_count) from 
, sum(right_table.month_count) ,, left_table.month_count
select left_table.customer_name , left_table.sale_date ,max(left_table.month_count), sum(right_table.month_count) from
(select customer_name,substring(sale_date,1, 7) as sale_date, sum(total_sale) as month_count from tmp.ods_ext_csv_unc_tmp_customer_sale group by customer_name, substring(sale_date,1, 7)) as left_table 
left join 
(select customer_name,substring(sale_date,1, 7) as sale_date, sum(total_sale)  as month_count from tmp.ods_ext_csv_unc_tmp_customer_sale group by customer_name, substring(sale_date,1, 7)) as right_table 
on left_table.customer_name = right_table.customer_name
where left_table.sale_date >= right_table.sale_date 
group by left_table.customer_name, left_table.sale_date;

查詢結果
A	2015-01	33	33
A	2015-02	10	43
B	2015-01	30	30
B	2015-02	15	45
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章