Choix
Choix

Reputation: 575

How to extract a tuple in list in scala

I have a RDD as below:

scala> topWithKeysSorted.take(10).foreach(println)
(1,[Lscala.Tuple4;@3e0c2650)
(2,[Lscala.Tuple4;@fef700c)
(3,[Lscala.Tuple4;@1ecb9068)
(4,[Lscala.Tuple4;@35b53a35)
(5,[Lscala.Tuple4;@6ad4a475)
(6,[Lscala.Tuple4;@1e4fd633)
(7,[Lscala.Tuple4;@5c455d42)
(8,[Lscala.Tuple4;@40bdb06d)
(9,[Lscala.Tuple4;@f854303)
(10,[Lscala.Tuple4;@6f9be28e)

scala> topWithKeysSorted.first
res38: (Int, Array[(Int, Int, String, Float)]) = (1,Array((1,2,Quest Q64 10 FT. x 10 FT. Slant Leg Instant U,59.98)))

How can I transform it into (Int, Int, Int, String, Float)? I am thinking there should be some easy and simple way to do it that I am not aware of.

Thank you very much.

Upvotes: 2

Views: 813

Answers (1)

Xavier Guihot
Xavier Guihot

Reputation: 61774

You can use pattern matching to easily work with individual parts of the input tuple:

val input = (1, Array((1, 2, "Quest Q64 10 FT. x 10 FT. Slant Leg Instant U", 59.98)))
input match { case (k, Array((v1, v2, v3, v4))) => (k, v1, v2, v3, v4) }

which returns:

(1,1,2,Quest Q64 10 FT. x 10 FT. Slant Leg Instant U,59.98)

And if you want to apply this transformation on each element of your RDD:

topWithKeysSorted.map{
  case (k, Array((v1, v2, v3, v4))) => (k, v1, v2, v3, v4) 
}

Upvotes: 1

Related Questions