HiveQL函数3—集合、类型转换函数

集合函数

HiveQL支持的集合函数如下

函数名返回类型描述
size(Map<K.V>)int计算map的元素个数
size(Array)int计算数组的元素个数
map_keys(Map<K.V>)array返回map中的所有key
map_values(Map<K.V>)array返回map中的所有value
array_contains(Array, value)boolean如该数组Array包含value返回true,否则返回false
sort_array(Array)array按自然顺序对数组从低到高进行排序并返回

示例1

--size(Map<K,V>)  ,size(Array\<T\>)
> SELECT
SIZE(work_place) as array_size,
SIZE(skills_score) as map_size,
SIZE(depart_title) as complex_size,
SIZE(depart_title["Product"]) as nest_size
FROM employee_internal;
+-------------+-----------+---------------+------------+
| array_size  | map_size  | complex_size  | nest_size  |
+-------------+-----------+---------------+------------+
| 2           | 1         | 1             | 1          |
| 1           | 1         | 2             | 1          |
| 1           | 1         | 2             | -1         |
| 1           | 2         | 1             | -1         |
+-------------+-----------+---------------+------------+

> SELECT size(null), size(array(null)), size(array());
+------+------+------+
| _c0  | _c1  | _c2  |
+------+------+------+
| -1   | 1    | 0    |
+------+------+------+

示例2

--array_contains(Array<T>, value),sort_array(Array<T>) 
>SELECT
array_contains(work_place, 'Toronto') as is_Toronto,
sort_array(work_place) as sorted_array
FROM employee_internal;
+-------------+-------------------------+
| is_toronto  |      sorted_array       |
+-------------+-------------------------+
| true        | ["Montreal","Toronto"]  |
| false       | ["Montreal"]            |
| false       | ["New York"]            |
| false       | ["Vancouver"]           |
+-------------+-------------------------+

类型转换函数

1. binary(string|binary)

返回值:binary
功能:将输入的值转换成二进制
示例:

> select binary('hello') as f1;
+--------+
|   f1   |
+--------+
| hello  |
+--------+

2. cast(expr as <type>)

返回值:指定类型
功能:将expr转换成type类型 如:cast(“1” as BIGINT) 将字符串1转换成了BIGINT类型,如果转换失败将返回NULL。对于cast(expr as boolean),如果字符串不为空则会返回true。
示例:

> select cast('123' as BIGINT) as  f1;
+------+
|  f1  |
+------+
| 123  |
+------+

> select cast('123' as boolean) as  f1;
+-------+
|  f1   |
+-------+
| true  |
+-------+

参考

https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-CollectionFunctions
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-TypeConversionFunctions
书籍 Apache Hive Essentials Second Edition (by Dayong Du) Chapter 5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值