user3316676
user3316676

Reputation: 109

Converting Iterable[(Double, Double)] to Seq(Seq(Double))

I want to convert Pair RDD "myRDD" values from Iterable[(Double,Double)] to Seq(Seq(Double)), however I am not sure how to do it. I tried the following but it does not work.

val groupedrdd: RDD[BB,Iterable[(Double,Double)]] = RDDofPoints.groupByKey()
val RDDofSeq = groupedrdd.mapValues{case (x,y) => Seq(x,y)}

The myRDD is formed using a groupByKey operation on a RddofPoints with their respective bounding boxes as keys. The BB is a case class and it is the key for a set of points with type (Double,Double). I want the RDDofSeq to have the type RDD[BB,Seq(Seq(Double))], however after groupByKey, myRDD has the type RDD[BB,Iterable[(Double,Double)]].

Here, it gives an error as:

Error:(107, 58) constructor cannot be instantiated to expected type;
 found   : (T1, T2)
 required: Iterable[(Double, Double)]

I am new to Scala, any help in this regard is appreciated. Thanks.

ANSWER : The following is used to accomplish the above goal:

val RDDofSeq = groupedrdd.mapValues{iterable => iterable.toSeq.map{case (x,y) => Seq(x,y)}}

Upvotes: 1

Views: 1049

Answers (2)

pme
pme

Reputation: 14803

I tried this on Scalafiddle

val myRDD: Iterable[(Double,Double)] = Seq((1.1, 1.2), (2.1, 2.2))

val RDDofSeq = myRDD.map{case (x,y) => Seq(x,y)}

println(RDDofSeq) // returns List(List(1.1, 1.2), List(2.1, 2.2))

The only difference is that I used myRDD.map(.. instead of myRDD.mapValues(..

Make sure that myRDD is really of the type Iterable[(Double,Double)]!

Update after comment:

If I understand you correctly you want a Seq[Double] and not a Seq[Seq[Double]]

That would be this:

val RDDofSeq = myRDD.map{case (k,v) => v} // returns List(1.2, 2.2)

Update after the Type is now clear:

The values are of type Iterable[(Double,Double)] so you cannot match on a pair.

Try this:

 val RDDofSeq = groupedrdd.mapValues{iterable => 
 Seq(iterable.head._1, iterable.head._2)}

Upvotes: 3

uh_big_mike_boi
uh_big_mike_boi

Reputation: 3470

You just need map, not mapValues.

val RDDofSeq = myRDD.map{case (x,y) => Seq(x,y)}

Upvotes: 2

Related Questions