hive - 如何在蜂巢中的管道分隔符后获取第 N 个字符串
问题描述
我在 Hive 中有一个表,我想从其中一个看起来像这样的列中提取字符串的第 5 个分量 -
样本数据
john:12|doe|google|usa|google.com|newspaper - title - 1 - volume - 1234|360671191
john:34|doe|fb|usa|google.com|newspaper - title - X - volume - 1233|360671192
john:45|doe|twitter|usa|google.com|newspaper - title - Y - volume - 1232|360671193
jane:45:1323
我想在第一个管道字符(|)之后解析出第 5 个字符串。输出列的值为 -
newspaper - title - 1 - volume - 1234
newspaper - title - X - volume - 1233
newspaper - title - Y - volume - 1232
jane:45:1323
如果标题不存在(如在记录 4 中),则我们按原样返回原始字符串。
解决方案
使用拆分功能,如下所示:
with your_data as (
select stack(4,
'john:12|doe|google|usa|google.com|newspaper - title - 1 - volume - 1234|360671191',
'john:34|doe|fb|usa|google.com|newspaper - title - X - volume - 1233|360671192',
'john:45|doe|twitter|usa|google.com|newspaper - title - Y - volume - 1232|360671193',
'jane:45:1323'
) as str
)
select nvl(splitted_str[5], original_str) result
from
(
select split(str,'\\|') splitted_str, str original_str
from your_data
)s;
回报:
newspaper - title - 1 - volume - 1234
newspaper - title - X - volume - 1233
newspaper - title - Y - volume - 1232
jane:45:1323
推荐阅读
- html - React Native:如何使用 FlexBox 水平对齐所有项目,并在它们之间留一些空间?
- qemu - 在没有 -cpu 主机的情况下运行 qemu 时是否可以启用 KVM(-enable-kvm)
- node.js - GCloud 部署功能 - 代码 3 但我在入口点没有问题
- android - 如何从 SQLite 数据库创建 Android 图表
- javascript - 如何将 javascript 文件转换为包装函数并在 html 页面中使用它?
- docker - 来自守护进程的错误响应:registry.gitlab.com 存储库的拉取访问被拒绝或可能需要“docker login”
- flutter - 水平对齐复选框和文本在颤动的中心
- javascript - 使用 Javascript 对时间跨度进行单元测试
- android - 如何在android中模拟用于单元测试的视图
- python - Discord.py:如何提取布尔值以判断某人是否静音