You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@spark.apache.org by Yifan LI <ia...@gmail.com> on 2014/12/04 10:26:26 UTC

map function

Hi,

I have a RDD like below:
(1, (10, 20))
(2, (30, 40, 10))
(3, (30))
…

Is there any way to map it to this:
(10,1)
(20,1)
(30,2)
(40,2)
(10,2)
(30,3)
…

generally, for each element, it might be mapped to multiple.

Thanks in advance! 


Best,
Yifan LI






Re: map function

Posted by Yifan LI <ia...@gmail.com>.
Thanks, Paolo and Mark. :)


> On 04 Dec 2014, at 11:58, Paolo Platter <pa...@agilelab.it> wrote:
> 
> Hi,
> 
> rdd.flatMap( e => e._2.map( i => ( i, e._1)))
> 
> Should work, but I didn't test it so maybe I'm missing something.
> 
> Paolo
> 
> Inviata dal mio Windows Phone
> Da: Yifan LI <ma...@gmail.com>
> Inviato: ‎04/‎12/‎2014 09:27
> A: user@spark.apache.org <ma...@spark.apache.org>
> Oggetto: map function 
> 
> Hi,
> 
> I have a RDD like below:
> (1, (10, 20))
> (2, (30, 40, 10))
> (3, (30))
> …
> 
> Is there any way to map it to this:
> (10,1)
> (20,1)
> (30,2)
> (40,2)
> (10,2)
> (30,3)
> …
> 
> generally, for each element, it might be mapped to multiple.
> 
> Thanks in advance! 
> 
> 
> Best,
> Yifan LI
> 
> 
> 
> 
> 


R: map function

Posted by Paolo Platter <pa...@agilelab.it>.
Hi,

rdd.flatMap( e => e._2.map( i => ( i, e._1)))

Should work, but I didn't test it so maybe I'm missing something.

Paolo

Inviata dal mio Windows Phone
________________________________
Da: Yifan LI<ma...@gmail.com>
Inviato: ‎04/‎12/‎2014 09:27
A: user@spark.apache.org<ma...@spark.apache.org>
Oggetto: map function

Hi,

I have a RDD like below:
(1, (10, 20))
(2, (30, 40, 10))
(3, (30))
…

Is there any way to map it to this:
(10,1)
(20,1)
(30,2)
(40,2)
(10,2)
(30,3)
…

generally, for each element, it might be mapped to multiple.

Thanks in advance!


Best,
Yifan LI






Re: map function

Posted by Mark Hamstra <ma...@clearstorydata.com>.
rdd.flatMap { case (k, coll) => coll.map { elem => (elem, k) } }

On Thu, Dec 4, 2014 at 1:26 AM, Yifan LI <ia...@gmail.com> wrote:

> Hi,
>
> I have a RDD like below:
> (1, (10, 20))
> (2, (30, 40, 10))
> (3, (30))
> …
>
> Is there any way to map it to this:
> (10,1)
> (20,1)
> (30,2)
> (40,2)
> (10,2)
> (30,3)
> …
>
> generally, for each element, it might be mapped to multiple.
>
> Thanks in advance!
>
>
> Best,
> Yifan LI
>
>
>
>
>
>