Lucien
Lucien

Reputation: 99

Scala RDD count by range

I need to "extract" some data contained in an Iterable[MyObject] (it was a RDD[MyObject] before a groupBy).

My initial RDD[MyObject] :

|-----------|---------|----------|
| startCity | endCity | Customer |
|-----------|---------|----------|
| Paris     | London  | ID | Age |
|           |         |----|-----|
|           |         |  1 | 1   |
|           |         |----|-----|
|           |         |  2 | 1   |
|           |         |----|-----|
|           |         |  3 | 50  |
|-----------|---------|----------|
| Paris     | London  | ID | Age |
|           |         |----|-----|
|           |         |  5 | 40  |
|           |         |----|-----|
|           |         |  6 | 41  |
|           |         |----|-----|
|           |         |  7 | 2   |
|-----------|---------|----|-----|
| New-York  | Paris   | ID | Age |
|           |         |----|-----|
|           |         |  9 | 15  |
|           |         |----|-----|
|           |         |  10| 16  |
|           |         |----|-----|
|           |         |  11| 46  |
|-----------|---------|----|-----|
| New-York  | Paris   | ID | Age |
|           |         |----|-----|
|           |         |  13| 7   |
|           |         |----|-----|
|           |         |  14| 9   |
|           |         |----|-----|
|           |         |  15| 60  |
|-----------|---------|----|-----|
| Barcelona | London  | ID | Age |
|           |         |----|-----|
|           |         |  17| 66  |
|           |         |----|-----|
|           |         |  18| 53  |
|           |         |----|-----|
|           |         |  19| 11  |
|-----------|---------|----|-----|

I need to count them by age range by and groupBy startCity - endCity

The final result should be :

|-----------|---------|-------------|
| startCity | endCity | Customer    |
|-----------|---------|-------------|
| Paris     | London  | Range| Count|
|           |         |------|------|
|           |         |0-2   | 3    |
|           |         |------|------|
|           |         |3-18  | 0    |
|           |         |------|------|
|           |         |19-99 | 3    |
|-----------|---------|-------------|
| New-York  | Paris   | Range| Count|
|           |         |------|------|
|           |         |0-2   | 0    |
|           |         |------|------|
|           |         |3-18  | 3    |
|           |         |------|------|
|           |         |19-99 | 2    |
|-----------|---------|-------------|
| Barcelona | London  | Range| Count|
|           |         |------|------|
|           |         |0-2   | 0    |
|           |         |------|------|
|           |         |3-18  | 1    |
|           |         |------|------|
|           |         |19-99 | 2    |
|-----------|---------|-------------|

At the moment I'm doing this by count 3 times the same data (first time with 0-2 range, then 10-20, then 21-99).

Like :

Iterable[MyObject] ite

ite.count(x => x.age match {
    case Some(age) => { age >= 0 && age < 2 }
}

It's working by giving me an Integer but not efficient at all I think since I have to count many times, what's the best way to do this please ?

Thanks

EDIT : The Customer object is a case class

Upvotes: 1

Views: 1029

Answers (2)

Ramesh Maharjan
Ramesh Maharjan

Reputation: 41987

Assuming that you have Customer[Object] as a case class as below

case class Customer(ID: Int, Age: Int)

And your RDD[MyObject] is a rdd of case class as below

case class MyObject(startCity: String, endCity: String, customer: List[Customer])

So using above case classes you should be having input (that you have in table format) as below

MyObject(Paris,London,List(Customer(1,1), Customer(2,1), Customer(3,50)))
MyObject(Paris,London,List(Customer(5,40), Customer(6,41), Customer(7,2)))
MyObject(New-York,Paris,List(Customer(9,15), Customer(10,16), Customer(11,46)))
MyObject(New-York,Paris,List(Customer(13,7), Customer(14,9), Customer(15,60)))
MyObject(Barcelona,London,List(Customer(17,66), Customer(18,53), Customer(19,11)))

And you've also mentioned that after grouping you have Iterable[MyObject] which is equivalent to below step

val groupedRDD = rdd.groupBy(myobject => (myobject.startCity, myobject.endCity))   //groupedRDD: org.apache.spark.rdd.RDD[((String, String), Iterable[MyObject])] = ShuffledRDD[2] at groupBy at worksheetTest.sc:23

So the next step for you to do is to use mapValues to iterate through the Iterable[MyObject], and then count the ages belonging to each ranges, and finally converting to the output you require as below

val finalResult = groupedRDD.mapValues(x => {
  val rangeAge = Map("0-2" -> 0, "3-18" -> 0, "19-99" -> 0)
  val list = x.flatMap(y => y.customer.map(z => z.Age)).toList
  updateCounts(list, rangeAge).map(x => CustomerOut(x._1, x._2)).toList
})

where updateCounts is a recursive function

def updateCounts(ageList: List[Int], map: Map[String, Int]) : Map[String, Int] = ageList match{
  case head :: tail => if(head >= 0 && head < 3) {
    updateCounts(tail, map ++ Map("0-2" -> (map("0-2")+1)))
  } else if(head >= 3 && head < 19) {
    updateCounts(tail, map ++ Map("3-18" -> (map("3-18")+1)))
  } else updateCounts(tail, map ++ Map("19-99" -> (map("19-99")+1)))
  case Nil => map
}

and CustomerOut is another case class

case class CustomerOut(Range: String, Count: Int)

so the finalResult is as below

((Barcelona,London),List(CustomerOut(0-2,0), CustomerOut(3-18,1), CustomerOut(19-99,2)))
((New-York,Paris),List(CustomerOut(0-2,0), CustomerOut(3-18,4), CustomerOut(19-99,2)))
((Paris,London),List(CustomerOut(0-2,3), CustomerOut(3-18,0), CustomerOut(19-99,3)))

Upvotes: 1

Oli
Oli

Reputation: 10406

def computeRange(age : Int) = 
    if(age<=2)
        "0-2"
    else if(age<=10)
        "2-10"
    // etc, you get the idea

Then, with an RDD of case class MyObject(id : String, age : Int)

rdd
   .map(x=> computeRange(x.age) -> 1)
   .reduceByKey(_+_)

Edit: If you need to group by some columns, you can do it this way, provided that you have a RDD[(SomeColumns, Iterable[MyObject])]. The following lines would give you a map that associates each "range" to its number of occurences.

def computeMapOfOccurances(list : Iterable[MyObject]) : Map[String, Int] =
    list
        .map(_.age)
        .map(computeRange)
        .groupBy(x=>x)
        .mapValues(_.size)

val result1 = rdd
    .mapValues( computeMapOfOccurances(_))

And if you need to flatten your data, you can write:

val result2 = result1
    .flatMapValues(_.toSeq)    

Upvotes: 2

Related Questions