hive split函数获取元素数

qnakjoqk  于 2021-06-26  发布在  Hive
关注(0)|答案(1)|浏览(956)

例如,我想知道hive是否有从字符串中获取元素数的选项 david.Udert . 我试过了 split(type,'\\.')[2][3] 还有这个 split(type,'\\.')[2:3] 但这是行不通的,有没有办法用这样的方法把这两个词拼在一起呢?

kmbjn2e3

kmbjn2e31#

数组索引以 0 ```
with t as (select 'Now.I.heard.you.know.that.secret.chord' as mycol)

select split(t.mycol,'\.') as arr
,split(t.mycol,'\.')[0] as arr_1st_element
,split(t.mycol,'\.')[1] as arr_2nd_element
,split(t.mycol,'\.')[2] as arr_3nd_element

from t
;

+----------------------------------------------------------+-----------------+-----------------+-----------------+
| arr | arr_1st_element | arr_2nd_element | arr_3nd_element |
+----------------------------------------------------------+-----------------+-----------------+-----------------+
| ["Now","I","heard","you","know","that","secret","chord"] | Now | I | heard |
+----------------------------------------------------------+-----------------+-----------------+-----------------+

至少目前不支持数组切片。如果你想切片,就在切片前做

with t as (select 'Now.I.heard.you.know.that.secret.chord' as mycol)

select split(substring_index(substring_index(t.mycol,'.',7),'.',-3),'\.') as slice_option_1
,split(regexp_extract(t.mycol,'(.?\.){4}((\.?[^.]){0,3})',2),'\.') as slice_option_2

from t
;

+--------------------------+--------------------------+
| slice_option_1 | slice_option_2 |
+--------------------------+--------------------------+
| ["know","that","secret"] | ["know","that","secret"] |
+--------------------------+--------------------------+

相关问题