0

I am working on riak-java client. while doing Mapreduce i got the following output. Now i want to get sum of ticketValue from the Result set that i got.I want to get it by passing userID.

update

From this method i get the resultSet as below

 def fetchTicketValue(userID:String): Boolean = {
    try{
      val result = riakClient.mapReduce("table-ticket","userID:Kim")
      .addMapPhase(new NamedJSFunction("Riak.mapValuesJson"),true)
      .execute
      val o:String = result.getResultRaw
    }catch{
      case e : Exception => 
         e.printStackTrace
     }
     true
  }

I forgot to mention large bracket [{....},{....},{....}] previously.

[{"ticketValue":3,"userID":"Kim","Date":"date","ticketID":"98394kjdf","ticketSource":"reg"},{"ticketValue":3,"userID":"Kim","Date":"date","ticketID":"98394kjdf","ticketSource":"reg"},{"ticketValue":3,"userID":"Kim","Date":"date","ticketID":"98394kjdf","ticketSource":"reg"}]

LIKE: userID:"kim"

RESULT: ticketValue:9

How can i get the data? Any idea will be highly appreciated. Thanks in advance.

(Riak Version : 1.3.2 and Riak Java Client : 1.1.4)

Uraniium
  • 109
  • 6
  • You want to compute the sum in Scala or in a reduce phase? – Joe Jun 06 '14 at 13:16
  • (I don't know Riak.) Is the result returned as a bare String, like you wrote it above? – Samy Dindane Jun 06 '14 at 13:17
  • Let me explained you with the example of SQL. The above string is the same result as the query does like this `Select * from table_name where userID="Kim"` – Uraniium Jun 09 '14 at 03:54
  • @Joe yes i want to compute the sum in scala. My requirement is that when i get a **resultset** after doing fetch i need to sum up the ticket values. As in above given line it is like **datatable of SQL** and i want to **sum up ticketvalue** as we do `sum(ticketvalue)` in SQL. Hope you understand my logic here. – Uraniium Jun 09 '14 at 04:17

2 Answers2

0
import scala.util.parsing.json.JSON

case class Row(ticketValue: Int, userId: String, date: String, ticketId: String, ticketSource: String)

// I'm sure there's a way to do this more cleanly; using it as an implicit maybe
def mapToRow(m: Any): Row = {
  val typedM = m.asInstanceOf[Map[String, Any]]
  Row(typedM("ticketValue").asInstanceOf[Double].toInt,
    typedM("userID").toString,
    typedM("Date").toString,
    typedM("ticketID").toString,
    typedM("ticketSource").toString)
}

def getSum(result: String, userId: String) = {
    val rows = JSON.parseFull("[" + result + "]").get.asInstanceOf[Seq[Map[String, Any]]].map(mapToRow)
    rows.filter(_.userId == userId).map(_.ticketValue).sum
}


val result = """{"ticketValue":3,"userID":"Samy","Date":"date","ticketID":"98394kjdf","ticketSource":"reg"},{"ticketValue":3,"userID":"Kim","Date":"date","ticketID":"98394kjdf","ticketSource":"reg"},{"ticketValue":3,"userID":"Kim","Date":"date","ticketID":"98394kjdf","ticketSource":"reg"},{"ticketValue":3,"userID":"Kim","Date":"date","ticketID":"98394kjdf","ticketSource":"reg"}"""
getSum(result, "Kim")
Samy Dindane
  • 17,900
  • 3
  • 40
  • 50
  • The output of your answer is same as what i need but the **String** that i provided is not a bare String it is a **datatable** as in sql. According to your answer in **mapToRow** when i print value of **m** in console i get **Map(...)** seperately in every iteration but in my case i get **List(Map(...),Map(...),(...))** all in one iteration. for better understanding i have updated my question. Sorry for inconvenience I am new on programming and in bloging also. Thanks. – Uraniium Jun 09 '14 at 09:26
  • @Uraniium You can make a Map from a List of Map like this: `m.reduce(_ ++ _)`. – Samy Dindane Jun 10 '14 at 09:01
  • could you please explain me little more about how to use `m.reduce(_ ++ _)`. – Uraniium Jun 10 '14 at 09:26
  • @Uraniium You need to use it where you have `List(Map(…), Map(…), …)` in order to get a `Map(…)`. It will basically merge the `List`'s `Map`s. — You'd also need to change `Map[String, Any]` to `List[Map[…]]`, depending on how the result list looks like. – Samy Dindane Jun 10 '14 at 09:34
0

Have you given a look at Jerkson Library (its abandoned as far as i know, but still provides good functionalities), its a scala wrapper for Jackson Library for java, using default Play JSON library is a good idea but since its getting deprecated from 2.11. onwards, and Jerkson is much easier as well.

Using Jerkson you could try this, the code is self explanatory, i had the same problem recently Link->

var sum:Int = 0
val parsedList = parse[List[Ticket]](result.getResultRaw)
for (e <-parsedList){
  sum = sum + e.ticketValue
}
println("Final sum: "+sum)
Community
  • 1
  • 1
mane
  • 1,149
  • 16
  • 41