在hive中将一行单词分成单词组

col17t5w  于 2021-06-24  发布在  Hive
关注(0)|答案(1)|浏览(422)

我有一些文字,我想分解成两个,三个,甚至四个字的时间。我试着用有意义的词组。
我用过 split 以及 explode 检索我需要的内容,但我希望一次将行拆分为两个或三个单词。到目前为止,我只把这一行一次分成一个单词。

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
?
d6kp6zgx

d6kp6zgx1#

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子句将上述逻辑添加到实际表中,并让我知道它是如何运行的。

相关问题