hive常用函数之其他函数
九、復(fù)合類型構(gòu)建操作
1. Map類型構(gòu)建: map
語(yǔ)法: map (key1, value1, key2, value2,…)
說(shuō)明:根據(jù)輸入的key和value對(duì)構(gòu)建map類型
舉例:
hive> Create table lxw_test as select map(‘100’,‘tom’,‘200’,‘mary’)as t from lxw_dual;
hive> describe lxw_test;
t map<string,string>
hive> select t from lxw_test;
{“100”:“tom”,“200”:“mary”}
2. Struct類型構(gòu)建: struct
語(yǔ)法: struct(val1, val2, val3,…)
說(shuō)明:根據(jù)輸入的參數(shù)構(gòu)建結(jié)構(gòu)體struct類型
舉例:
hive> create table lxw_test as select struct(‘tom’,‘mary’,‘tim’)as t from lxw_dual;
hive> describe lxw_test;
t structcol1:string,col2:string,col3:string
hive> select t from lxw_test;
{“col1”:“tom”,“col2”:“mary”,“col3”:“tim”}
3. array類型構(gòu)建: array
語(yǔ)法: array(val1, val2,…)
說(shuō)明:根據(jù)輸入的參數(shù)構(gòu)建數(shù)組array類型
舉例:
hive> create table lxw_test as selectarray(“tom”,“mary”,“tim”) as t from lxw_dual;
hive> describe lxw_test;
t array
hive> select t from lxw_test;
[“tom”,“mary”,“tim”]
十、復(fù)雜類型訪問(wèn)操作
1. array類型訪問(wèn): A[n]
語(yǔ)法: A[n]
操作類型: A為array類型,n為int類型
說(shuō)明:返回?cái)?shù)組A中的第n個(gè)變量值。數(shù)組的起始下標(biāo)為0。比如,A是個(gè)值為[‘foo’, ‘bar’]的數(shù)組類型,那么A[0]將返回’foo’,而A[1]將返回’bar’
舉例:
hive> create table lxw_test as selectarray(“tom”,“mary”,“tim”) as t from lxw_dual;
hive> select t[0],t[1],t[2] from lxw_test;
tom mary tim
2. map類型訪問(wèn): M[key]
語(yǔ)法: M[key]
操作類型: M為map類型,key為map中的key值
說(shuō)明:返回map類型M中,key值為指定值的value值。比如,M是值為{‘f’ -> ‘foo’, ‘b’-> ‘bar’, ‘a(chǎn)ll’ -> ‘foobar’}的map類型,那么M[‘a(chǎn)ll’]將會(huì)返回’foobar’
舉例:
hive> Create table lxw_test as selectmap(‘100’,‘tom’,‘200’,‘mary’) as t from lxw_dual;
hive> select t[‘200’],t[‘100’] from lxw_test;
mary tom
3. struct類型訪問(wèn): S.x
語(yǔ)法: S.x
操作類型: S為struct類型
說(shuō)明:返回結(jié)構(gòu)體S中的x字段。比如,對(duì)于結(jié)構(gòu)體struct foobar {int foo, int bar},foobar.foo返回結(jié)構(gòu)體中的foo字段
舉例:
hive> create table lxw_test as select struct(‘tom’,‘mary’,‘tim’)as t from lxw_dual;
hive> describe lxw_test;
t structcol1:string,col2:string,col3:string
hive> select t.col1,t.col3 from lxw_test;
tom tim
十一、復(fù)雜類型長(zhǎng)度統(tǒng)計(jì)函數(shù)
1. Map類型長(zhǎng)度函數(shù): size(Map<K.V>)
語(yǔ)法: size(Map<K.V>)
返回值: int
說(shuō)明:返回map類型的長(zhǎng)度
舉例:
hive> select size(map(‘100’,‘tom’,‘101’,‘mary’)) from lxw_dual;
2
2. array類型長(zhǎng)度函數(shù): size(Array)
語(yǔ)法: size(Array)
返回值: int
說(shuō)明:返回array類型的長(zhǎng)度
舉例:
hive> select size(array(‘100’,‘101’,‘102’,‘103’)) from lxw_dual;
4
3. 類型轉(zhuǎn)換函數(shù)
類型轉(zhuǎn)換函數(shù): cast
語(yǔ)法: cast(expr as )
返回值: Expected “=” to follow “type”
說(shuō)明:返回array類型的長(zhǎng)度
舉例:
hive> select cast(1 as bigint) from lxw_dual;
1
總結(jié)
以上是生活随笔為你收集整理的hive常用函数之其他函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 继电器——开关作用
- 下一篇: 基于单片机的血压计c语言,基于AT89C