如何使用横向视图爆炸将定界字符串拆分为 Hive 中的多行

How to split delimited String to multiple rows in Hive using lateral view explode

我在 Hive 中有一个 table 如下 -

create table somedf 
(sellers string , 
 orders int 
)

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

table 有一个名为 sellers 的列,它由插入语句中描述的字符分隔。我想将卖家分成多行,如下所示 -

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

我正在尝试在 Hive 中使用 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

此结果未按需要从 table 中拆分行 1('1--**--2--**--3',50),最终只生成 2 行而不是 3 行。

这个任务还需要其他函数吗? lateral view explode() 是否只适用于数组?

传入 split 的模式不正确。 * 个字符需要转义。无需转义 -.

使用

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

这也行。它期望在中间出现两次 *。

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