1

この場合、KV ストア (Redis) からデータを読み取っています。返されるデータは次の形式です。

 { "key1":"value1", "key2":"value2", "key3":"value3" ...} 

キーはStringで、値はIntです。に変換したいMap[String,Int]

json4s JSON APIを調べたところ、現在のコードは次のようになっています。これを行うためのより良い/より簡単な/よりクリーンな方法はありますか?

  //send a async query to Redis to
  val queryFuture  = redis.zrangebyscore[String](tablename, keymin, keymax )


  queryFuture.onComplete {
    case Success(datarows) =>
      println(s"Got %d rows of type %s for the query successfully".format(datarows.length))
      val jsonrows = for { x <- datarows.toList }
        yield parse(x)
      println("Got json rows %d".format(jsonrows.size))
      val mapdata = jsonrows.map(x => x.extract[Map[String,String]]).map( x => x.mapValues(_.toInt))

      //need to do something with this data now
    case Failure(y) =>
      println(s" there was some failure in getting the data from Redis")
  }
4

3 に答える 3

1

json4sを知らず、残念ながらタイプを省略しましたが、それjsonrowsはおそらくList[(String, String)]あなたができることのようなものだと推測します List(("key1" -> "1"),("key2" -> "2")).map { case (k, v) => (k, v.toInt)}.toMap

ところで、あなたが言うなら、それneed to do something with this data nowonComplete副作用の操作にすぎない可能性があります。mapあなたの処理が完了するまで、将来にわたってより良い.

于 2014-03-29T08:19:54.783 に答える