简体   繁体   中英

Fastest way to encode and decode data in scala

I have the following csv file:

Name    Age City    Start   Stop    Point
Mike    29  Fuji    10      30      5
Mike    29  Fuji    0       10      7
Susan   26  Fuji    77      1000    9

I'm trying to access the Point, given name, age, city and range

Example given:

Mike, 29, Fuji, 15 will yield 5
Mike, 29, Fuji, 5 will yield 7
Susan, 26, Fuji, 990 will yield 9
Susan, 26, Fuji, 1500 will yield 0 since there's no match

I read the csv and tried to construct a Scala Map[String, Map[Int, Map[String, Map[Int, Int]]]] but that is not very scalable given I have few thousand records. The start and stop ranges are disjoint and have to relation with other rows.

How can I encode and decode this data efficiently without using a SQL database or a KeyValue store? Any help will be greatly appreciated.

Here's a naive version using case classes for the key ( Person ) and the ranges ( RangeAndPoint ) - there are some missing details in the post (eg are ranges inclusive/exclusive? Are they disjoint? What should be the result of no matching range is found? If more than one is found?) - but these can be fixed in the answer:

case class Person(name: String, age: Int, city: String)

case class RangeAndPoint(start: Int, end: Int, point: Int) {
  def inRange(value: Int): Boolean = value < end && value >= start
}

// Let's assume CSV was read into such a format - I'll hard-code it here for the example:
val persons: Map[Person, List[RangeAndPoint]] = Map(
  Person("Mike", 29, "Fuji") -> List(RangeAndPoint(10, 30, 5), RangeAndPoint(0, 10, 7)),
  Person("Susan", 26, "Fuji") -> List(RangeAndPoint(77, 1000, 9))
)

// returns the expected result or None if no match found:
def getPoint(name: String, age: Int, city: String, value: Int): Option[Int] = {
  persons
    .get(Person(name, age, city))
    .flatMap(_.find(_.inRange(value)))
    .map(_.point)
}

println(getPoint("Mike", 29, "Fuji", 15))   // Some(5)
println(getPoint("Mike", 29, "Fuji", 5))    // Some(7)
println(getPoint("Susan", 26, "Fuji", 990)) // Some(9)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM