Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the last element from Apache Spark SQL split() Function

I want to get the last element from the Array that return from Spark SQL split() function.

split(4:3-2:3-5:4-6:4-5:2,'-')

I know it can get by

split(4:3-2:3-5:4-6:4-5:2,'-')[4]

But i want another way when i don't know the length of the Array . please help me.

like image 936
Dil Avatar asked Jan 26 '17 06:01

Dil


2 Answers

You can also use SparkSql Reverse() function on a column after Split(). For example:

SELECT reverse(split(MY_COLUMN,'-'))[0] FROM MY_TABLE

Here [0] gives you the first element of the reversed array, which is the last element of the initial array.

like image 116
Mahdi Shahbaba Avatar answered Dec 05 '22 05:12

Mahdi Shahbaba


Please check substring_index it should work exactly as you want:

substring_index(lit("1-2-3-4"), "-", -1) // 4
like image 31
Valentin Avatar answered Dec 05 '22 04:12

Valentin