【问题标题】:Break a row of words into word groups in Hive在 Hive 中将一行单词分成单词组
【发布时间】:2019-12-23 02:40:56
【问题描述】:

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

我使用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
?

【问题讨论】:

标签: hive hiveql


【解决方案1】:
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 子句将上述逻辑添加到您的实际表中,并让我知道它是如何进行的。

【讨论】:

  • 您好维克兰特:感谢您的回复。不幸的是,我的 Hive 版本在横向视图poseexplode(split(text,'') pe as i,s 横向视图poseexplode(split(text,'') pe as j,k) 上的版本错误。它在i,s 和j,k。它不会接受带逗号的别名。同样,我的 Hive 版本不识别poseexplode,只能分解。我可以拆分、分解和使用横向视图,但我不能以词组结尾而不是单个单词。
  • 您是否运行了相同的查询?您使用的是哪个配置单元版本?
  • 不确定您遇到什么错误,但可以看到代码中缺少 ) .. 上面的评论部分。
  • 嗨维克兰特:我又试了一次。我之前一定做错了什么。这行得通!非常感谢。这回答了我的问题。
猜你喜欢
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 2020-12-29
  • 2016-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-24
相关资源
最近更新 更多