如何使用横向视图分解将分隔字符串拆分为配置单元中的多行

gdx19jrr  于 2021-06-26  发布在  Hive
关注(0)|答案(2)|浏览(332)

我在 hive 里有一张table如下-

create table somedf 
(sellers string , 
 orders int 
)

insert into somedf values
('1--**--2--**--3',50),
('1--**--2', 10)

该表有一个名为sellers的列,由insert语句中描述的字符分隔。我想把卖家分成多行,如下所示-

exploded_sellers orders
1                 50
2                 50
3                 50
1                 10
2                 10

我想用 lateral view explode() 在配置单元中运行,但无法获得结果。我正在使用下面的查询-

select exploded_sellers, orders
from somedf
lateral view outer explode(split(sellers,'\\--*.*\\*.*--')) t1 as exploded_sellers

下面的结果作为输出-

exploded_sellers orders
1                 50
3                 50
1                 10
2                 10

此结果不会拆分第1行 ('1--**--2--**--3',50) 根据需要从表中删除,最后只生成2行而不是3行。
这个任务还需要其他功能吗?做 lateral view explode() 只在阵列上工作?

lnxxn5zx

lnxxn5zx1#

这也行。它预期中间出现两个*。

select exploded_sellers, orders
from somedf
lateral view outer explode(split(sellers,'--\\*{2}--')) t1 as exploded_sellers;
rqdpfwrv

rqdpfwrv2#

图案传入 split 不正确。 * 角色需要转义。不需要逃跑 - .
使用

select exploded_sellers, orders
from somedf
lateral view outer explode(split(sellers,'--\\*\\*--')) t1 as exploded_sellers

相关问题