其他分享
首页 > 其他分享> > HiveQL函数3—集合、类型转换函数

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

看得出的就是 发布了47 篇原创文章 · 获赞 3 · 访问量 1万+ 私信 关注

标签:类型转换,map,函数,HiveQL,示例,cast,array,Array,size
来源: https://blog.csdn.net/CPP_MAYIBO/article/details/104065151