How to find minimum and maximum value using single foldByKey


#1

Use Case: I want to find customer wise minimum and maximum item value using single foldByKey() function with spark RDD.

val salesData = sc.textFile(args(0)).mapPartitions(_.drop(1)) // sales data
//key: custid and value:itemvalue
val salesDataMap1 = salesData.map(rec => ( rec.split(",")(0).toInt,(rec.split(",")(3).toInt, rec.split(",")(3).toInt )))

val max_min_ByCust = salesDataMap1.
foldByKey((0,0))((acc,ele) => (if(acc._1 > ele._1) acc._1 else ele._1, if(acc._2 < ele._2) acc._2 else ele._2))

why second initialiser value is not updated for minimum
Here i can get only maximun but minimu returns 0 value. I need (cust_id, max_value,minValue) this output.
Please guide.