简体   繁体   中英

using couchbase java api on scala: async select query and mapping results

I'm new to couchbase. I use its java sdk on scala. Basically I want to execute a query asynchronously and map the future results to an object (ex. Transaction)

I have this code snippet:

   val query= N1qlQuery.simple(s"SELECT * FROM `transaction` $whereClause $orderByClause LIMIT $itemsPerPage OFFSET $offset")


    val promise= Promise[Seq[Transaction]]()

    println("****asyncSearch query=" + query)

    bucket.async().query(query).flatMap(new Func1[AsyncN1qlQueryResult, Observable[AsyncN1qlQueryRow]](){
      override def call(result: AsyncN1qlQueryResult):Observable[AsyncN1qlQueryRow]= {
        println("****asyncSearch flatMap=" + result)
        result.rows()
      }
    }).map[Transaction]( new Func1[AsyncN1qlQueryRow, Transaction](){
      override def call(result: AsyncN1qlQueryRow):Transaction= {
        println("****asyncSearch map=" + result)
        result.value().toString : Transaction
      }
    }).scan(List[Transaction](), new Func2[ List[Transaction], Transaction, List[Transaction] ](){
      override def call(accumulated: List[Transaction], current: Transaction): List[Transaction]= {
        println("****asyncSearch scan=" + current)
        accumulated ::: List(current)
      }
    }).subscribe(new Action1[List[Transaction]](){
      override def call(result: List[Transaction]):Unit= {
        println("****asyncSearch subscribe=" + result)
        promise.success(result)
      }
    })

    promise.future

However, I am only able to see the prints for "****asyncSearch query=..." and "****asyncSearch subscribe=List()". As you can see, my final result is empty but I executed the same query in Couchbase web console and I am getting results. So there must be something wrong in the code because I am not seeing the prints for "****asyncSearch flatMap=..." "****asyncSearch scan=..." etc.

Can somebody help me with my code? Thanks in advance.

im not sure you need all those new Func1 's - thats more Java style because Java doesnt/didnt support lambda expressions. Try something like this:

...(query).flatMap( result => { println("res"+result); result.rows() )

once thats coming back ok perhaps start chaining other blocks in a similar style.

See also http://reactivecouchbase.org/

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