在 HIVE 中查找子字符串

Finding Substring in HIVE

我正在尝试从字符串 (composite_key) 中获取子字符串值: 我的 composite_key 如下所述:

string1|string2|string3|string4|string5|string6|string7 

我可以使用 impala

的子串方法找到 string1、string2、string3、string4 和 string5

有人可以帮我用子串法找到 String6 和 String7 吗?

任何帮助都会得到体现。

我能够使用以下查询来做到这一点:

对于 String7

select substring(composite_key,locate('|',composite_key,locate('|',composite_key,locate('|',composite_key,locate('|',composite_key,locate('|',composite_key, locate('|',composite_key) + 1)+1)+1)+1)+1)+1)as a

对于 String6

select
substring(composite_key,
locate('|',composite_key,
locate('|',composite_key,
locate('|',composite_key,
locate('|',composite_key, 
locate('|',composite_key) + 1)+1)+1)+1)+1, 
locate('|',composite_key,
locate('|',composite_key,
locate('|',composite_key,
locate('|',composite_key,
locate('|',composite_key, 
locate('|',composite_key) + 1)+1)+1)+1)+1)
- locate('|',composite_key,
locate('|',composite_key,
locate('|',composite_key,
locate('|',composite_key, 
locate('|',composite_key) + 1)+1)+1)+1)-1) 
as a

你已经使用Hive子查询+数组数据结构+split函数完成了。但是,这仅适用于 Hive。 Impala 目前还不支持嵌套数据结构,除了 Impala 2.3(对应 CDH 5.5)及更高版本中基于 parquet 的 table。

select 
key_array[0] part0,
key_array[1] part1, 
key_array[2] part2, 
key_array[3] part3, 
key_array[4] part4, 
key_array[5] part5, 
key_array[6] part6, 
from (
select split(composite_key,'|') as key_array 
from mytable
) as temp