How to access the last element in an array ?

18,741

Solution 1

reverse(split(reverse(session), '-')[0])

Although this might be a bit more expensive than the regex solution ;)

Solution 2

Because Non-constant expressions for array indexes not supported in hive.

There will be some other ways to solve your problem:

  1. use regexp_extract, such as :

    select regexp_extract(session, '(\-[^\-]+)', 1) as user from my_table;

  2. use custom hive function : example and document could be found in hive document

Solution 3

One more method without reverse, using array size()-1. This method is more efficient because it does not produce intermediate reversed string, it works only with array.

select array[size(array)-1] as userID
from
( select split(session,'-') array from your_table ) s;
Share:
18,741
qiuxiafei
Author by

qiuxiafei

Little coder learning Clojure

Updated on June 06, 2022

Comments

  • qiuxiafei
    qiuxiafei almost 2 years

    In my hive table, the session field is a string in format like:

    ip-sessionID-userID or area-sessionID-userID

    There's 3 or 4 fields separated by "-", but userID is always the last one.

    i wanna select userID, but how to access the last field? In python, there's something like: arr[-1]

    but in hive, how to achieve this? The following SQL seems not correct.

    select split(session,"\-")[-1] as user from my_table;

    Thanks!

  • mel
    mel over 8 years
    This regex solution did not work in case array only have one element.
  • pensz
    pensz over 8 years
    try regexp_extract(session, '([^\-]+)$', 1) ?
  • Mike Wise
    Mike Wise over 8 years
    Love this answer. Thanks.