hive 复合类型_hive 常用函数整理 9.复合类型操作
1. map類型構建: map
語法: map (key1, value1, key2, value2, …)
說明:根據輸入的key和value對構建map類型
舉例:
hive> Create table lxw_test as select map('100','tom','200','mary') as t from student;
hive> describe lxw_test;
t map
hive> select t from lxw_test;
{"100":"tom","200":"mary"}
--通過key 查詢value
hive> select t['100'] from lxw_test limit 1;
Tom
--查看鍵值對的個數
hive> select size(t) from lxw_test limit 1;
2
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 student;
hive> select t['200'],t['100'] from lxw_test;
mary tom
3. map 取出key集合
4. map類型長度函數: size(Map)
語法: size(Map)
返回值: int
說明: 返回map類型的長度
舉例:
hive> select size(map('100','tom','101','mary')) from student;
2
5. array類型構建: array
語法: array(val1, val2, …)
說明:根據輸入的參數構建數組array類型
舉例:
hive> create table lxw_test as select array("tom","mary","tim") as t from student;
hive> describe lxw_test;
t array
hive> select t from lxw_test;
["tom","mary","tim"]
hive> select t[1] from lxw_test3;
mary
6. array類型訪問: A[n]
語法: A[n]
操作類型: A為array類型,n為int類型
說明:返回數組A中的第n個變量值。數組的起始下標為0。比如,A是個值為[‘foo’, ‘bar’]的數組類型,那么A[0]將返回’foo’,而A[1]將返回’bar’
舉例:
hive> create table lxw_test as select array("tom","mary","tim") as t from student;
hive> select t[0],t[1],t[2] from lxw_test;
tom mary tim
7. array類型長度函數: size(Array)
語法: size(Array)
返回值: int
說明: 返回array類型的長度
舉例:
hive> select size(array('100','101','102','103')) from student;
4
8. struct類型構建: struct
語法: struct(val1, val2, val3, …)
說明:根據輸入的參數構建結構體struct類型
舉例:
hive> create table lxw_test2 as select struct('tom','mary','tim') as t from student;
hive> describe lxw_test2;
t struct
hive> select t from lxw_test2;
{"col1":"tom","col2":"mary","col3":"tim"}
hive> select t.col1 from lxw_test2;
tom
9. struct類型訪問: S.x
語法: S.x
操作類型: S為struct類型
說明:返回結構體S中的x字段。比如,對于結構體struct foobar {int foo, int bar},foobar.foo返回結構體中的foo字段
舉例:
hive> create table lxw_test as select struct('tom','mary','tim') as t from student;
hive> describe lxw_test;
t struct
hive> select t.col1,t.col3 from lxw_test;
tom tim
10. 類型轉換函數 cast(expr as )
類型轉換函數: cast
語法: cast(expr as )
返回值: Expected “=” to follow “type”
說明: 返回array類型的長度
舉例:
hive> select cast(1 as bigint) from student;
1
hive> Create table lxw_test5 as select 1 as t from student;
hive> desc lxw_test5;
t int
hive> Create table lxw_test6 as select cast(1 as bigint) as t from student;
hive> desc lxw_test6;
t bigint
總結
以上是生活随笔為你收集整理的hive 复合类型_hive 常用函数整理 9.复合类型操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Rabbitmq用户角色
- 下一篇: 一文看懂「生成对抗网络 - GAN」基本