hive和presto的求数组长度函数区别及注意事项

1、任务

获取邮箱字符串'@'后字符串 ,求长度

2、hive & spark-sql 求数组长度的函数 size

sql 复制代码
hive & spark-sql 求数组长度的函数 size


select size(split(email, '@')),split(email, '@'),split(email, '@')[0],split(email, '@')[1]
FROM 
(select "jack@126.com" as email union select "tom@126.com.cn" as email) tb_mid;

select size(split(email, '@')),split(email, '@'),split(email, '@')[0],split(email, '@')[1]
FROM 
(select 'jack@126.com' as email union select 'tom@126.com.cn' as email) tb_mid;


2	["tom","126.com.cn"]	tom	126.com.cn
2	["jack","126.com"]	jack	126.com
Time taken: 0.723 seconds, Fetched 2 row(s)

3、presto 求数组长度的函数 cardinality

sql 复制代码
presto  求数组长度的函数 cardinality

select cardinality(split(email, '@')),split(email, '@'),split(email, '@')[1],split(email, '@')[2]
FROM 
(select 'jack@126.com' as email union select 'tom@126.com.cn' as email) tb_mid;

_col0 |       _col1       | _col2 |   _col3    
-------+-------------------+-------+------------
     2 | [tom, 126.com.cn] | tom   | 126.com.cn 
     2 | [jack, 126.com]   | jack  | 126.com    
(2 rows)


select cardinality(split(email, '@')),split(email, '@'),split(email, '@')[1],split(email, '@')[2]
FROM 
(select "jack@126.com" as email union select "tom@126.com.cn" as email) tb_mid;


Query 20231019_070945_20009_n9u2s failed: line 3:9: Column 'jack@126.com' cannot be resolved
select cardinality(split(email, '@')),split(email, '@'),split(email, '@')[1],split(email, '@')[2]
FROM
(select "jack@126.com" as email union select "tom@126.com.cn" as email) tb_mid

4、注意事项

1)、在计算数组长度的时候,hive和presto的函数不同

其中hive的size函数默认数组的下标从0开始

presto的cardinality函数默认数组的下标从1开始

2)、presto 不支持双引号 ,而hive 既支持单引号,也支持双引号

sql 复制代码
presto> SELECT 
     -> email,
     -> (case when cardinality(split(email, '@')) = 2 then split(email, '@')[1] else '' end ) as email_suffix
     -> FROM 
     -> (select "jack@126.com" as email union select "tom@126.com.cn" as email) tb_mid;
Query 20231016_070153_17958_p9f2s failed: line 5:9: Column 'jack@126.com' cannot be resolved
SELECT
email,
(case when cardinality(split(email, '@')) = 2 then split(email, '@')[1] else '' end ) as email_suffix
FROM
(select "jack@126.com" as email union select "tom@126.com.cn" as email) tb_mid
相关推荐
houzhizhen1 天前
HiveMetastore 的架构简析
hive
数据要素X1 天前
【数据仓库】Hive 拉链表实践
大数据·数据库·数据仓库·人工智能·hive·hadoop·安全
LiamTuc2 天前
远程访问,通过JDBC连接到Beeline对Hive进行操作报错
数据仓库·hive·hadoop
songqq272 天前
sql中判断一个字段是否包含一个数据的方法有哪些?
数据库·hive·sql
zmd-zk2 天前
sql在hive和阿里云maxComputer的区别
数据库·hive·sql
油头少年_w2 天前
Hive操作库、操作表及数据仓库的简单介绍
数据仓库·hive
我的K84092 天前
Hive中自定义函数的使用
数据仓库·hive·hadoop
sunxunyong2 天前
hive 异常任务中间数据清理
数据仓库·hive·hadoop
FF_y2 天前
Hive学习笔记
hive·笔记·学习
dogplays4 天前
sqoop问题汇总记录
hive·hadoop·sqoop