Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the last element in an array ?

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!

like image 667
qiuxiafei Avatar asked Dec 12 '12 03:12

qiuxiafei


People also ask

What is the last element in an array?

The Last element is nothing but the element at the index position that is the length of the array minus-1. If the length is 4 then the last element is arr[3].

How do you call the last element of an array in Java?

Get the ArrayList with elements. Get the first element of ArrayList with use of get(index) method by passing index = 0. Get the last element of ArrayList with use of get(index) method by passing index = size – 1.

What is the last in array in C?

The last element of an array is at position size - 1.


2 Answers

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

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

like image 82
arno_v Avatar answered Sep 23 '22 00:09

arno_v


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

like image 29
pensz Avatar answered Sep 23 '22 00:09

pensz