Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. import com.couchbase.client.java.Bucket;
  2. import com.couchbase.client.java.Cluster;
  3. import com.couchbase.client.java.CouchbaseCluster;
  4. import com.couchbase.client.java.PersistTo;
  5. import com.couchbase.client.java.ReplicateTo;
  6. import com.couchbase.client.java.document.json.JsonArray;
  7. import com.couchbase.client.java.document.json.JsonObject;
  8. import com.couchbase.client.java.document.subdoc.DocumentFragment;
  9. import com.couchbase.client.java.document.subdoc.ExtendDirection;
  10. import com.couchbase.client.java.error.subdoc.PathExistsException;
  11.  
  12. public class ArraysAndDicts {
  13.  
  14. // Creates a "fleet" array and pushes aircraft into it
  15. public static void main(String... args) {
  16. Cluster cluster = CouchbaseCluster.create("127.0.0.1");
  17. Bucket bucket = cluster.openBucket("travel-sample");
  18.  
  19. // insert a fleet array if it does not exist
  20. DocumentFragment<JsonArray> fragment = DocumentFragment.create("airline_13633", "fleet", JsonArray.empty());
  21. try {
  22. bucket.insertIn(fragment, true, PersistTo.NONE, ReplicateTo.NONE);
  23. } catch (PathExistsException ex) {
  24. System.out.println("The array already exists, ignoring.");
  25. }
  26.  
  27. // create fleet information
  28. DocumentFragment<JsonObject> aircraft1 = DocumentFragment.create("airline_13633", "fleet",
  29. JsonObject.create().put("name", "747-200B").put("heavy", true).put("engines", 4)
  30. );
  31.  
  32. DocumentFragment<JsonObject> aircraft2 = DocumentFragment.create("airline_13633", "fleet",
  33. JsonObject.create().put("name", "737-200").put("engines", 2)
  34. );
  35.  
  36. // append the aircraft information to the fleet array
  37. bucket.extendIn(aircraft1, ExtendDirection.BACK, false, PersistTo.NONE, ReplicateTo.NONE);
  38. bucket.extendIn(aircraft2, ExtendDirection.BACK, false, PersistTo.NONE, ReplicateTo.NONE);
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement