HQL数据类型

map数据类型

创建:

1
2
3
4
5
6
hive> CREATE TABLE t3 (foo STRING, bar MAP<STRING,INT>)
> ROW FORMAT DELIMITED
> FIELDS TERMINATED BY '/t'
> COLLECTION ITEMS TERMINATED BY ','(必须使用)
> MAP KEYS TERMINATED BY ':'
> STORED AS TEXTFILE;

使用:

1.map_values(map):取map字段全部value

1
map_values(map):取map字段全部value

2.使用下标访问map

1
select cookie,mid['2024'] from temp.map_20181101

3.size()查看map长度即有多少键值对

1
select cookie,size(mid) from temp.map_20181101

4.Lateral View语法将值展开为一个新的虚拟表

1
2
3
SELECT cookie,fixeddim_key,fixeddim_value
FROM temp.map_20181101
LATERAL VIEW explode(mid) myTable1 AS fixeddim_key,fixeddim_value

假设有一张表,表名为t,其中字段params的数据类型是map,其map的具体k-v对如下:

1
{'k0':'abc','k1':'01,02,03','k2':'456'}

map_keys()

map_keys(Map):

函数说明:可得map中所有的key; 返回值类型: array

1
2
select map_keys(map(t.params));
>> ["k0","k1","k2"]

array数据类型

array_contains()

array_contains(array arr, element)

函数说明:
Returns if the element is in the array, the element must hava same type as array

1
2
3
4
SELECT array_CONTAINS(ARRAY(0,1),0) 
>> true
SELECT array_CONTAINS(split(‘0=1’,’=’),’0’)
>> true

string数据类型

split()

语法: split(string str, string pat)

返回值: array

说明: 按照pat字符串分割str,会返回分割后的字符串数组

1
2
3
4
5
6
7
8
9
10
11
12
13
# 基本用法
hive> select split('abcdef', 'c') from test;
["ab", "def"]

# 截取字符串中的某个值
hive> select split('abcdef', 'c')[0] from test;
ab

# 特殊字符
hive> select split('ab_cd_ef', '\_')[0] from test;
ab
hive> select split('ab?cd_ef', '\\?')[0] from test;
ab