I am trying to produce the RDD that contains an array of tuples that has country names as the first element, and the minimum integer of the tuple as the second element.
I have this code here.
val test = sc.parallelize(Array(("US", (4,2)), ("France", (1,2)), ("Italy", (2,3))))
I want to store a variable to a value that looks like this:
Array( ("US", 2), ("France", 1), ("Italy", 2) )
I tried to use this code, but it produced a 'Value min is not a member of (Int, Int)' error.
val test1 = test.map(x => (x._1, x._2.min))
How to get minimum of Tuple2[Int, Int]
?
To compute the minimum of numeric elements in a Tuple (x, y)
, you could use x min y
:
val test = sc.parallelize(Array(("US", (4,2)), ("France", (1,2)), ("Italy", (2,3))))
test.map(t => (t._1, t._2._1 min t._2._2)).collect
// res1: Array[(String, Int)] = Array((US,2), (France,1), (Italy,2))
For readability, an alternative is to use case
partial function, as follows:
test.map{ case (country, (t1, t2)) => (country, t1 min t2) }