水善利萬(wàn)物而不爭(zhēng),處眾人之所惡,故幾于道??
一、簡(jiǎn)單數(shù)據(jù)類型(復(fù)習(xí))
官方數(shù)據(jù)類型詳情頁(yè)
數(shù)據(jù)類型 | 描述 | 范圍 |
---|---|---|
tinyint | 1byte有符號(hào)整數(shù) | from -128 to 127 |
smallint | 2byte有符號(hào)整數(shù) | from -32,768 to 32,767 |
int | 4byte有符號(hào)整數(shù) | from -2,147,483,648 to 2,147,483,647 |
bigint | 8byte有符號(hào)整數(shù) | from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 4byte單精度浮點(diǎn)數(shù) | \ |
double | 8byte雙精度浮點(diǎn)數(shù) | \ |
decimal | 高精度數(shù)值類型 | decimal(10,0)默認(rèn)值,表示最多有10位數(shù),其中0位小數(shù) |
boolean | 布爾類型 | true或者false |
string | 字符串 | 用的最多 |
date | 日期類型 | YYYY--MM--DD |
timestamp | 時(shí)間戳類型 | \ |
二、復(fù)雜數(shù)據(jù)類型
array(數(shù)組)
定義數(shù)組:array<int>
構(gòu)建數(shù)組:array(1,3,5,6)
訪問(wèn)數(shù)組:字段名[0]
map(集合)
定義集合:map<string,int>
構(gòu)建集合:map(“age”,11,“weight”,99)
訪問(wèn)集合:字段名[“weight”]
struct(結(jié)構(gòu)體)
定義結(jié)構(gòu)體:struct<name:string,age:int>
構(gòu)建結(jié)構(gòu)體:named_struct(“name”,“Asrid”,“age”,99)
訪問(wèn)結(jié)構(gòu)體:字段名.name
看完上面可能一臉懵,有點(diǎn)懂,又有點(diǎn)迷,下面用一個(gè)例子解釋下。
數(shù)據(jù)類型無(wú)非就是創(chuàng)建表的時(shí)候指定數(shù)據(jù)類型,然后向表中插入數(shù)據(jù)的時(shí)候要按照相應(yīng)的數(shù)據(jù)類型構(gòu)建出相應(yīng)格式類型的數(shù)據(jù)才能插入,最終在查詢的時(shí)候如何訪問(wèn)到數(shù)據(jù)。
上面說(shuō)的分別就對(duì)應(yīng)數(shù)據(jù)類型的定義、構(gòu)建、訪問(wèn)。
比如,想創(chuàng)建一張data_type表,字段的數(shù)據(jù)類型分別用array、map、struct,那如何寫呢?
create table data_type(
name string,
age int,
address map<string,string>,
friend struct<name:string,age:int>,
phone array<bigint>
)
表中最后三個(gè)字段的類型就是復(fù)雜數(shù)據(jù)類型的定義,也就是如何聲明一個(gè)復(fù)雜的數(shù)據(jù)類型。
表創(chuàng)建好了以后,如何向表中插入數(shù)據(jù)呢?這就涉及到如何構(gòu)建一個(gè)map、array、struct 了
insert into table data_type
(name,age,address,friend,phone) values
("Bersd",
32,
map("山西","太原","陜西","西安"),
named_struct("name","aaa","age",33),
array(12345678888,18234567777,19243219998))
構(gòu)建map的時(shí)候,直接map()括號(hào)里面寫上key value 中間用逗號(hào)分割開(kāi)就行
構(gòu)建struct的時(shí)候,使用named_struct()括號(hào)里面寫屬性名,屬性值,之間用逗號(hào)隔開(kāi)
構(gòu)建array的時(shí)候,直接array()括號(hào)里面寫上數(shù)組內(nèi)元素就行了,各個(gè)元素之間用逗號(hào)隔開(kāi)
向表中插入數(shù)據(jù)之后,那怎樣訪問(wèn)到struct、map、array 里面的數(shù)據(jù)呢?
select name,
address["山西"] shanxi,
friend.name friend_name,
friend.age friend_age,
phone[0] phone_first
from data_type;
可以看到,訪問(wèn)array和map的時(shí)候都是方括號(hào),然后里面寫數(shù)組下標(biāo)或者map的key,而訪問(wèn)struct的數(shù)據(jù)時(shí)要.屬性名
這樣子訪問(wèn)
擴(kuò)展:以上復(fù)雜數(shù)據(jù)類型之間支持嵌套,也就是說(shuō)struct里面的數(shù)據(jù)類型可以寫map、array,array里面也可以放map、struct…
構(gòu)建struct還可以直接struct(“name”,“張三”,“age”,89)這樣他就會(huì)給四個(gè)默認(rèn)的列名,這個(gè)一般不用文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-838909.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-838909.html
到了這里,關(guān)于Hive中的復(fù)雜數(shù)據(jù)類型 - array、map、struct的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!