Re: Access Last Element of RDD

2014-04-24 Thread Sai Prasanna
Thanks Cheng !! On Thu, Apr 24, 2014 at 5:43 PM, Cheng Lian wrote: > You may try this: > > val lastOption = sc.textFile("input").mapPartitions { iterator => > if (iterator.isEmpty) { > iterator > } else { > Iterator > .continually((iterator.next(), iterator.hasNext())) >

Re: Access Last Element of RDD

2014-04-24 Thread Cheng Lian
You may try this: val lastOption = sc.textFile("input").mapPartitions { iterator => if (iterator.isEmpty) { iterator } else { Iterator .continually((iterator.next(), iterator.hasNext())) .collect { case (value, false) => value } .take(1) } }.collect().lastOption It

Re: Access Last Element of RDD

2014-04-24 Thread Sai Prasanna
Hi All, Finally i wrote the following code, which is felt does optimally if not the most optimum one. Using file pointers, seeking the byte after the last \n but backwards !! This is memory efficient and i hope even unix tail implementation should be something similar !! import java.io.RandomAcces

Re: Access Last Element of RDD

2014-04-23 Thread Sai Prasanna
Thanks Guys ! On Thu, Apr 24, 2014 at 11:29 AM, Sourav Chandra < sourav.chan...@livestream.com> wrote: > Also same thing can be done using rdd.top(1)(reverseOrdering) > > > > On Thu, Apr 24, 2014 at 11:28 AM, Sourav Chandra < > sourav.chan...@livestream.com> wrote: > >> You can use rdd.takeOrder

Re: Access Last Element of RDD

2014-04-23 Thread Sourav Chandra
Also same thing can be done using rdd.top(1)(reverseOrdering) On Thu, Apr 24, 2014 at 11:28 AM, Sourav Chandra < sourav.chan...@livestream.com> wrote: > You can use rdd.takeOrdered(1)(reverseOrdrering) > > reverseOrdering is you Ordering[T] instance where you define the ordering > logic. This y

Re: Access Last Element of RDD

2014-04-23 Thread Sourav Chandra
You can use rdd.takeOrdered(1)(reverseOrdrering) reverseOrdering is you Ordering[T] instance where you define the ordering logic. This you have to pass in the method On Thu, Apr 24, 2014 at 11:21 AM, Frank Austin Nothaft < fnoth...@berkeley.edu> wrote: > If you do this, you could simplify to:

Re: Access Last Element of RDD

2014-04-23 Thread Sai Prasanna
What i observe is, this way of computing is very inefficient. It returns all the elements of the RDD to a List which takes considerable amount of time. Then it calculates the last element. I have a file of size 3 GB in which i ran a lot of aggregate operations which dint took the time that this ta

Re: Access Last Element of RDD

2014-04-23 Thread Frank Austin Nothaft
If you do this, you could simplify to: RDD.collect().last However, this has the problem of collecting all data to the driver. Is your data sorted? If so, you could reverse the sort and take the first. Alternatively, a hackey implementation might involve a mapPartitionsWithIndex that returns an

Re: Access Last Element of RDD

2014-04-23 Thread Adnan Yaqoob
This function will return scala List, you can use List's last function to get the last element. For example: RDD.take(RDD.count()).last On Thu, Apr 24, 2014 at 10:28 AM, Sai Prasanna wrote: > Adnan, but RDD.take(RDD.count()) returns all the elements of the RDD. > > I want only to access the la

Re: Access Last Element of RDD

2014-04-23 Thread Sai Prasanna
Adnan, but RDD.take(RDD.count()) returns all the elements of the RDD. I want only to access the last element. On Thu, Apr 24, 2014 at 10:33 AM, Sai Prasanna wrote: > Oh ya, Thanks Adnan. > > > On Thu, Apr 24, 2014 at 10:30 AM, Adnan Yaqoob wrote: > >> You can use following code: >> >> RDD.take

Re: Access Last Element of RDD

2014-04-23 Thread Sai Prasanna
Oh ya, Thanks Adnan. On Thu, Apr 24, 2014 at 10:30 AM, Adnan Yaqoob wrote: > You can use following code: > > RDD.take(RDD.count()) > > > On Thu, Apr 24, 2014 at 9:51 AM, Sai Prasanna wrote: > >> Hi All, Some help ! >> RDD.first or RDD.take(1) gives the first item, is there a straight >> forward

Re: Access Last Element of RDD

2014-04-23 Thread Adnan Yaqoob
You can use following code: RDD.take(RDD.count()) On Thu, Apr 24, 2014 at 9:51 AM, Sai Prasanna wrote: > Hi All, Some help ! > RDD.first or RDD.take(1) gives the first item, is there a straight forward > way to access the last element in a similar way ? > > I coudnt fine a tail/last method for