David Lerech
David Lerech

Reputation: 55

Hive split function get number of elements

I would like to know if Hive has the option to get number of elements from a String, for example david.Udert. I tried this split(type,'\\.')[2][3]
and this split(type,'\\.')[2:3] and it doesn't work, Is there any option to do something like this to get the two words together?

Upvotes: 1

Views: 3266

Answers (1)

David דודו Markovitz
David דודו Markovitz

Reputation: 44941

  • Array indexes starts with 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           |
    +----------------------------------------------------------+-----------------+-----------------+-----------------+
    
  • Array slicing is not supported, at least for now. If you want to slice, do it before the split

    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"] |
    +--------------------------+--------------------------+
    

Upvotes: 1

Related Questions