第3章 Hive資料型別

2021-10-10 18:38:38 字數 3578 閱讀 8760

關聯式資料庫裡有表(table),分割槽,hive裡也有這些東西,這些東西在hive技術裡稱為hive的資料模型

hive支援兩種資料型別,一類叫原子資料型別,一類叫複雜資料型別。

原子資料型別包括數值型、布林型和字串型別,具體如下表所示:

複雜資料型別包括陣列(array)、對映(map)和結構體(struct),具體如下表所示:

1) 假設某錶有如下一行,我們用json格式來表示其資料結構。在hive下訪問的格式為

"address"

:}

2)基於上述資料結構,我們在hive裡建立對應的表,並匯入資料。

建立本地測試檔案test.txt

xiongxiong

,huahua_lili

,xiao

xiong:

18_xiaoxiao

xiong:19,

huilong

guan_beijing

yangyang

,caicai_susu

,xiao

yang:

18_xiaoxiao

yang:19,

chao

yang_beijing

注意:map,struct和array裡的元素間關係都可以用同乙個字元表示,這裡用「_」。

3)hive上建立測試表test

字段解釋:

row format delimited fields terminated by ','– 列分隔符

collection

items

terminated

by'_'

--map struct 和 array 的分隔符(資料分割符號)
map

keys

terminated

by':'

-- map中的key與value的分隔符
lines terminated by 『\n』; – 行分隔符

4)匯入文字資料到測試表

hive

(default

)>

load

data

local

inpath

'/opt/module/datas/test.txt'

into

table

test

;

5)訪問三種集合列裡的資料,以下分別是array,map,struct的訪問方式

鑲套轉換

select

(cast

(cast(a

asstring)as

double))

from

users;

;

2014-11-10 和 20141110相互轉化的辦法:

1.from_unixtime && unix_timestamp

– 20141110

select from_unixtime(unix_timestamp(『2014-11-10』,『yyyy-mm-dd』),『yyyymmdd』) from default.dual;

– 2014-11-10

select from_unixtime(unix_timestamp(『20141110』,『yyyymmdd』),『yyyy-mm-dd』) from default.dual;

2.substr + concat

– 20141110

select concat(substr(『2014-11-10』,1,4),substr(『2014-11-10』,6,2),substr(『2014-11-10』,9,2)) from default.dual;

– 2014-11-10

select concat(substr(『20141110』,1,4),』-』,substr(『20141110』,5,2),』-』,substr(『20141110』,7,2)) from default.dual;

附加:hive 獲取當前時間

select from_unixtime(unix_timestamp(),『yyyy-mm-dd hh:mm:ss』) from default.dual;

第3章 基本資料型別

0301.獲得使用者輸入的乙個整數,輸出該整數百位及以上的數字。0301 n input print n 2 0302.獲得使用者輸入的乙個字串,將字串按照空格分隔,然後逐行列印出來。0302 n input nlist n.split for n in nlist print n 0303.程式讀...

第 3 章 組合資料型別

3.1 序列型別 序列型別支援成員操作符 in 大小計算函式 len 分片 並且都是可迭代的。元組 tuple 有序的,包含0個或多個物件引用。元組是固定的不可修改。如果要修改元組可以用list 將其轉換為列表。命名元組 namedtuple 簡單記錄一下用法。import collections ...

第11章 資料型別

11.1 數值資料型別 11.2 日期和時間資料型別 11.3 字串資料型別 11.4 spatial data types 11.5 the json data type 11.6 資料型別預設值 11.7 資料型別儲存要求 11.8 為列選擇正確的型別 11.9 使用來自其他資料庫 商的資料型別...