Advertisement
Guest User

Couchbase query.

a guest
Sep 22nd, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.69 KB | None | 0 0
  1. //The below code is executed per HTTP request in vertx verticle.
  2.  
  3. com.couchbase.client.java.document.json.JsonObject couchBody = com.couchbase.client.java.document.json.JsonObject.empty();
  4. final String fullName = "John Smith";
  5.  
  6. couchBody.put("fullName", fullName);
  7.  
  8. final Context context = vertx.getOrCreateContext();
  9.  
  10. JsonDocument doc = JsonDocument.create("123456789", couchBody);
  11.  
  12. final AtomicInteger count = new AtomicInteger();
  13.  
  14. bucket.async().upsert(doc, PersistTo.MASTER).subscribe(d -> {
  15.  
  16.     String sql = "SELECT * FROM " + BUCKET_NAME + " WHERE fullName =$1";
  17.  
  18.     N1qlParams params = N1qlParams.build().adhoc(false);
  19.     ParameterizedN1qlQuery query = N1qlQuery.parameterized(sql, JsonArray.create()
  20.             .add(fullName), params);
  21.    
  22.     bucket.async().query(query)
  23.     .subscribe(r -> {
  24.         r.errors().subscribe(e -> {
  25.             System.out.println(e);
  26.             context.runOnContext((v) -> {
  27.                 h.reply(new JsonObject().put("queryErrors", e.toString()));
  28.             });                                                
  29.         }, re -> {
  30.             re.printStackTrace();
  31.             context.runOnContext((v) -> {
  32.                 h.reply(new JsonObject().put("queryErrors", re.getCause().toString()));
  33.             });
  34.         });
  35.  
  36.         r.rows().map(rows -> rows.value()).subscribe(
  37.                 row -> {
  38.                      //System.out.println("My row: " + row);
  39.                     count.incrementAndGet();
  40.                 }, re -> {
  41.                     re.printStackTrace();
  42.                     context.runOnContext((v) -> {
  43.                         h.reply(new JsonObject().put("queryError", re.getCause().toString()));
  44.                     });                                    
  45.                 }, () -> {
  46.                     context.runOnContext((v) -> {
  47.                         h.reply(new JsonObject().put("result", count.get()));
  48.                     });                                
  49.                 });
  50.     });
  51. }, t-> {
  52.     context.runOnContext((v) -> {
  53.         h.reply(new JsonObject().put("insertError", t.toString()));
  54.     });                    
  55. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement