首页 > 解决方案 > 在 Hive 中将一行单词分成单词组

问题描述

我有一些文本,我想一次分解成两个、三个甚至四个单词。我正在尝试提取有意义的短语。

我已经使用splitexplode检索了我需要的内容,但我希望一次将行分成两个或三个单词。这是我到目前为止所拥有的,一次只将行分成一个单词。

select explode(a.text) text
from (select split(text," ") text
      from table abc
      where id = 123
      and date = 2019-08-16
     ) a

我得到的输出:

text
----
thank 
you 
for 
calling
your
tv
is
not
working
?

我想要这样的输出:

text
----
Thank you 
for calling 
your tv
is not
working?

或类似的东西:

text
----
thank you for calling
your
tv is not working
?

标签: hivehiveql

解决方案


CREATE TABLE IF NOT EXISTS db.test_string
(
text string
)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY ','
STORED AS orc
;

INSERT INTO TABLE db.test_string VALUES
('thank you for calling your tv is not working ?');

以下是查询:

select k,s from db.test_string
lateral view posexplode(split(text,' ')) pe as i,s
lateral view posexplode(split(text,' ')) ne as j,k
where ne.j=pe.i-1
and ne.j%2==0
;

thank   you
for     calling
your    tv
is      not
working ?
Time taken: 0.248 seconds, Fetched: 5 row(s)

使用 where 子句将上述逻辑添加到您的实际表中,并让我知道它是如何进行的。


推荐阅读