How to access the last element in an array ?

qiuxiafei picture qiuxiafei · Dec 12, 2012 · Viewed 14.5k times · Source

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!

Answer

arno_v picture arno_v · Nov 15, 2013
reverse(split(reverse(session), '-')[0])

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