Hive split函数获取元素数

时间:2017-06-15 12:49:01

标签: hive hiveql

我想知道Hive是否可以选择从字符串中获取元素数量,例如david.Udert。我试过这个split(type,'\\.')[2][3]
split(type,'\\.')[2:3]而且它不起作用, 有没有选择做这样的事情来把两个单词放在一起?

1 个答案:

答案 0 :(得分:1)

  • 数组索引以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"] |
    +--------------------------+--------------------------+