Hive建表和函式的語法

2021-10-07 09:42:37 字數 3695 閱讀 3299

1.create table(建表) ; load data(傳資料) ; --先建表,在用load載入資料到表中,形成對映

2.create table ;

3.create external table + location hdfs_path(指定位置) ;–不常見,但是有

4.create table 表名 as select --建立表的字段型別順序資料完全取決於後續的select查詢語句返回的結果

5.unio all :

1.修復分割槽

-- 在把資料載入到分割槽的表中,在檢視之前需要進行分割槽修復

msck repair table 表名;

-- 獲取hive中函式列表

show functions ;

-- 顯示函式

資料表 row2col_1:

col1 col2 col3

a b 1

a b 2

a b 3

c d 4

c d 5

c d 6

將其轉化為:

col1 col2 col3

a b 1-2

-3c d 4-5

-6此時需要兩個內建的函式:

a)concat_ws(引數1,引數2

),用於進行字元的拼接

引數1—指定分隔符

引數2—拼接的內容

b)collect_set(col3),它的主要作用是將某字段的值進行去重彙總,產生array型別字段

如果不想去重可用collect_list(

)select col1, col2, concat_ws(

'-', collect_set(cast(col3 as string)))

as col3

from row2col_1

group

by col1, col2;

最終sql:

select col1, col2, concat_ws(

'-', collect_set(cast(col3 as string)))

as col3

from row2col_1

group

by col1, col2;

載入資料:

load data local inpath '/root/hivedata/col2row_2.txt' into table col2row_2;

a b 1,2,3

c d 4,5,6

--使用lateral view(側檢視) + explode函式

select col1, col2, lv.col3 as col3

from col2row_2

lateral view explode(split(col3, ','

)) lv as col3;

資料表 row2col

col1 col2 col3

a c 1

a d 2

a e 3

b c 4

b d 5

b e 6

現在要將其轉化為:

col1 c d e

a 1 2 3

b 4 5 6

此時需要使用到max(case … when … then … else 0 end),僅限於轉化的字段為數值型別且為正值的情況

--最終sql:

select col1,

max(case col2 when 'c' then col3 else 0 end) as c,

max(case col2 when 'd' then col3 else 0 end) as d,

max(case col2 when 'e' then col3 else 0 end) as e

from row2col

group by col1;

資料表 col2row:

col1 c d e

a 1 2 3

b 4 5 6

現要將其轉化為:

col1 col2 col3

a c 1

a d 2

a e 3

b c 4

b d 5

b e 6

這裡需要使用union進行拼接。

union 可以結合多個select語句 返回共同的結果集

保證每個select語句返回的資料型別個數是一致的。

--最終sql:

select col1, 'c' as col2, c as col3 from col2row

union

select col1, 'd' as col2, d as col3 from col2row

union

select col1, 'e' as col2, e as col3 from col2row

order by col1, col2;

selcet  get_json_object (t.json, '$.id'

) from table t ;

select reflect(

"工具類的全路徑名"

,"方法名"

,要判斷列名)

from 表

使用 : 聚合函式+視窗函式

​ 排序函式+視窗函式

–針對time_local欄位 如何抽取?

–方式1:substring擷取

–方式2:把時間變成timestamp型別 使用hive內建的日期函式處理

--substring(字段,數字,數字); 含頭不含尾

第乙個數字 : 擷取開始的索引位置(索引是從0開始的)

第二個數字 : 擷取的個數

select substring(time_local,6,

2)asmonth

,substring(time_local,12,

2)ashour

from ods_weblog_origin limit1;

--substring(字段,數字); 括號裡只有乙個數字代表開始擷取的索引,並一致擷取到最後

select substring(time_local,12)

from ods_weblog_origin limit1;

--擷取時間

-- 格式化時間的函式:

date_fotmat(

"原來的格式"

,"想解析成的格式"

)

Hive建表語法 內部表 外部表

寫語句時未被external修飾的表是內部表特點 當刪除該錶時,表的元資料也會跟著被刪除,會影響其他使用該資料的表 從以上的操作可以看出兩個表共同使用乙個資訊,如果刪除乙個表的元資料的話,hdfs上的儲存資料也會跟著刪除,導致另外的表的資料也會丟失。不加external會預設為內部表 管理表 對於一...

hive建表範例

建表範例 支援update和delete create table aaa id string visitor name string clustered by id into 2buckets stored as orc tblproperties transactional true 目前只有o...

hive建表範例

建表範例 支援update和delete create table aaa id string visitor name string clustered by id into 2buckets stored as orc tblproperties transactional true 目前只有o...