假设我们有一张表:
Owner | Pets
------------------------------
Jack | "dog, cat, crocodile"
Mary | "bear, pig"
我想得到的结果是:
Owner | Pets
------------------------------
Jack | "dog"
Jack | "cat"
Jack | "crocodile"
Mary | "bear"
Mary | "pig"
我通过谷歌搜索找到了一些类似问题的解决方案,但Impala SQL没有提供任何这些功能来应用建议的解决方案。
任何帮助都将不胜感激!
发布于 2017-04-05 17:55:48
以下内容适用于Impala:
split_part(string source, string delimiter, bigint n)
您可以在以下位置找到文档:
https://www.cloudera.com/documentation/enterprise/5-9-x/topics/impala_string_functions.html
发布于 2020-09-02 22:08:00
您必须使用Hive SQL来执行此操作。
因此,让我们创建一张impala表
-- imapla sql
CREATE TABLE IF NOT EXISTS tmp.my_example (
`Owner` VARCHAR,
Pets VARCHAR
);
INSERT INTO tmp.my_example (`Owner`, `Pets`) VALUES
('Jack', 'dog, cat, crocodile'),
('Mary', 'bear, pig');
然后使用配置单元sql将列拆分为行:
-- hive sql
select f.owner as owner, t1.pos as pos, t1.val AS pet
from tmp.my_example f
lateral view posexplode(split(pets,', ')) t1
如果将结果另存为新表,请不要忘记在impala中运行refresh new_table
,以便可以使用新表
P.S. Hive对于这个小表来说非常慢,但是对于真实数据来说性能是合理的
https://stackoverflow.com/questions/37399187
复制相似问题