hive常用函数五
復(fù)合類型構(gòu)建操作
1. Map類型構(gòu)建: map
語法: map (key1, value1, key2, value2, …)
說明:根據(jù)輸入的key和value對構(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
語法: struct(val1, val2, val3, …)
說明:根據(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?????? struct<col1:string,col2:string,col3:string>
hive> select t from lxw_test;
{"col1":"tom","col2":"mary","col3":"tim"}
?
3. array類型構(gòu)建: array
語法: array(val1, val2, …)
說明:根據(jù)輸入的參數(shù)構(gòu)建數(shù)組array類型
舉例:
hive> create table lxw_test as select array("tom","mary","tim") as t from lxw_dual;
hive> describe lxw_test;
t?????? array<string>
hive> select t from lxw_test;
["tom","mary","tim"]
?
十、復(fù)雜類型訪問操作
1. array類型訪問: A[n]
語法: A[n]
操作類型: A為array類型,n為int類型
說明:返回數(shù)組A中的第n個變量值。數(shù)組的起始下標(biāo)為0。比如,A是個值為['foo', 'bar']的數(shù)組類型,那么A[0]將返回'foo',而A[1]將返回'bar'
舉例:
hive> create table lxw_test as select array("tom","mary","tim") as t from lxw_dual;
hive> select t[0],t[1],t[2] from lxw_test;
tom???? mary??? tim
?
2. map類型訪問: M[key]
語法: M[key]
操作類型: M為map類型,key為map中的key值
說明:返回map類型M中,key值為指定值的value值。比如,M是值為{'f' -> 'foo', 'b' -> 'bar', 'all' -> 'foobar'}的map類型,那么M['all']將會返回'foobar'
舉例:
hive> Create table lxw_test as select map('100','tom','200','mary') as t from lxw_dual;
hive> select t['200'],t['100'] from lxw_test;
mary??? tom
?
3. struct類型訪問: S.x
語法: S.x
操作類型: S為struct類型
說明:返回結(jié)構(gòu)體S中的x字段。比如,對于結(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?????? struct<col1:string,col2:string,col3:string>
hive> select t.col1,t.col3 from lxw_test;
tom???? tim
?
十一、復(fù)雜類型長度統(tǒng)計函數(shù)
1.??? Map類型長度函數(shù): size(Map<K.V>)
語法: size(Map<K.V>)
返回值: int
說明: 返回map類型的長度
舉例:
hive> select size(map('100','tom','101','mary')) from lxw_dual;
2
?
2.??? array類型長度函數(shù): size(Array<T>)
語法: size(Array<T>)
返回值: int
說明: 返回array類型的長度
舉例:
hive> select size(array('100','101','102','103')) from lxw_dual;
4
?
3.??? 類型轉(zhuǎn)換函數(shù)
類型轉(zhuǎn)換函數(shù): cast
語法: cast(expr as <type>)
返回值: Expected "=" to follow "type"
說明: 返回array類型的長度
舉例:
hive> select cast(1 as bigint) from lxw_dual;
1
4. 其他
cast 函數(shù):
類型轉(zhuǎn)換函數(shù),cast(kbcount as int);
case when:
條件判斷,case when kbcount is not null and cast(kbcount as int) >= cast(patch_count as int) then '1' else '0' end as isinstalled ;
語法:方法1
(
case sex
when '1' then '男'
when '2' then '女'
else '未知'
end
) as 性別
方法2
case
when sex='1' then '男'
when sex='2' then '女'
else '未知'
end as 性別
from_unixtime:將unix時間戳轉(zhuǎn)化為制定格式的時間
from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss') as xdsp_last_update_time
lcase() :將字段的值轉(zhuǎn)換為小寫
lcase(t.info_iot_name)
hive collect_set: 可以得到分組后,其他合并元素的制定位置的值作為去重后的值。
表明:user
id , name, url
1 shao www
1 shao1 www1
2 zhi www
2 zhi1 www.ee
2 zhi2 www.2323
3 qi www.eere
3 qi2 www.urr
想要得到:
id , name, url
1 shao www
2 zhi www
3 qi www.eere
sql實(shí)現(xiàn):方法1
select id ,collect_set(name)[0],collect_set(url)[0]
from user
group by id;
方法2:
select id ,max(name),max(url)
from user
group by id;
字符串max:字符串按照字母A-Z,越往后值越大
漢字按照全拼字母排,第一個字母相同則看第二個
concat() : 將兩個或者多個字符串連接起來,如果有任何一個參數(shù)為null,則返回值為null
a.os_version like concat('%','2012r2','%')
concat_ws(): 以第一個參數(shù)為分隔符,將其他參數(shù)連起來
concat_ws('.',os_version_main,os_version_sp,os_version_bit,os_version_sub)
?
轉(zhuǎn)載于:https://www.cnblogs.com/shaozhiqi/p/11534478.html
總結(jié)
- 上一篇: DIY智能温湿度计之数据获取
- 下一篇: 继电器学习笔记(一)——继电器的分类电磁