Last active
August 27, 2018 14:46
-
-
Save daschl/dd69b19517cd534edac1 to your computer and use it in GitHub Desktop.
subdoc examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.couchbase.client.java.Bucket; | |
import com.couchbase.client.java.Cluster; | |
import com.couchbase.client.java.CouchbaseCluster; | |
import com.couchbase.client.java.PersistTo; | |
import com.couchbase.client.java.ReplicateTo; | |
import com.couchbase.client.java.document.json.JsonArray; | |
import com.couchbase.client.java.document.json.JsonObject; | |
import com.couchbase.client.java.document.subdoc.DocumentFragment; | |
import com.couchbase.client.java.document.subdoc.ExtendDirection; | |
import com.couchbase.client.java.error.subdoc.PathExistsException; | |
public class ArraysAndDicts { | |
// Creates a "fleet" array and pushes aircraft into it | |
public static void main(String... args) { | |
Cluster cluster = CouchbaseCluster.create("127.0.0.1"); | |
Bucket bucket = cluster.openBucket("travel-sample"); | |
// insert a fleet array if it does not exist | |
DocumentFragment<JsonArray> fragment = DocumentFragment.create("airline_13633", "fleet", JsonArray.empty()); | |
try { | |
bucket.insertIn(fragment, true, PersistTo.NONE, ReplicateTo.NONE); | |
} catch (PathExistsException ex) { | |
System.out.println("The array already exists, ignoring."); | |
} | |
// create fleet information | |
DocumentFragment<JsonObject> aircraft1 = DocumentFragment.create("airline_13633", "fleet", | |
JsonObject.create().put("name", "747-200B").put("heavy", true).put("engines", 4) | |
); | |
DocumentFragment<JsonObject> aircraft2 = DocumentFragment.create("airline_13633", "fleet", | |
JsonObject.create().put("name", "737-200").put("engines", 2) | |
); | |
// append the aircraft information to the fleet array | |
bucket.extendIn(aircraft1, ExtendDirection.BACK, false, PersistTo.NONE, ReplicateTo.NONE); | |
bucket.extendIn(aircraft2, ExtendDirection.BACK, false, PersistTo.NONE, ReplicateTo.NONE); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.couchbase.client.java.Bucket; | |
import com.couchbase.client.java.Cluster; | |
import com.couchbase.client.java.CouchbaseCluster; | |
import com.couchbase.client.java.PersistTo; | |
import com.couchbase.client.java.ReplicateTo; | |
import com.couchbase.client.java.document.subdoc.DocumentFragment; | |
import java.util.Random; | |
public class Counter { | |
// increments the passengers_served counter on the airline | |
// uses dot-path notation to update counters in {"passengers": {"complained": 0, "served": 0}} | |
public static void main(String... args) { | |
Cluster cluster = CouchbaseCluster.create("127.0.0.1"); | |
Bucket bucket = cluster.openBucket("travel-sample"); | |
DocumentFragment<Long> counter = DocumentFragment.create("airline_13633", "passengers.served", 1L); | |
bucket.counterIn(counter, true, PersistTo.NONE, ReplicateTo.NONE); | |
// simulate some randomness that a passenger complained while being served | |
if (new Random().nextInt() % 2 == 0) { | |
counter = DocumentFragment.create("airline_13633", "passengers.complained", 1L); | |
bucket.counterIn(counter, true, PersistTo.NONE, ReplicateTo.NONE); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.couchbase.client.java.Bucket; | |
import com.couchbase.client.java.Cluster; | |
import com.couchbase.client.java.CouchbaseCluster; | |
import com.couchbase.client.java.document.subdoc.DocumentFragment; | |
public class HelloWorld { | |
// Fetch and print the name from an airline | |
public static void main(String... args) { | |
Cluster cluster = CouchbaseCluster.create("127.0.0.1"); | |
Bucket bucket = cluster.openBucket("travel-sample"); | |
DocumentFragment<String> fragment = bucket.getIn("airline_13633", "name", String.class); | |
System.out.println("The name is: " + fragment.fragment()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.couchbase.client.java.Bucket; | |
import com.couchbase.client.java.Cluster; | |
import com.couchbase.client.java.CouchbaseCluster; | |
import com.couchbase.client.java.PersistTo; | |
import com.couchbase.client.java.ReplicateTo; | |
import com.couchbase.client.java.document.subdoc.DocumentFragment; | |
public class MutateDict { | |
// update the callsign from the pan am airline | |
public static void main(String... args) { | |
Cluster cluster = CouchbaseCluster.create("127.0.0.1"); | |
Bucket bucket = cluster.openBucket("travel-sample"); | |
// update the callsign field to CLIPPER | |
DocumentFragment<String> fragment = DocumentFragment.create("airline_13633", "callsign", "CLIPPER"); | |
bucket.replaceIn(fragment, PersistTo.NONE, ReplicateTo.NONE); | |
// Load the full document and print its content to very | |
System.out.println(bucket.get("airline_13633").content()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.couchbase.client.java.Bucket; | |
import com.couchbase.client.java.Cluster; | |
import com.couchbase.client.java.CouchbaseCluster; | |
import com.couchbase.client.java.PersistTo; | |
import com.couchbase.client.java.ReplicateTo; | |
import com.couchbase.client.java.document.subdoc.DocumentFragment; | |
import com.couchbase.client.java.error.subdoc.PathExistsException; | |
public class Unique { | |
public static void main(String... args) { | |
Cluster cluster = CouchbaseCluster.create("127.0.0.1"); | |
Bucket bucket = cluster.openBucket("travel-sample"); | |
// add the 747-200 model to the models array | |
DocumentFragment<String> fragment = DocumentFragment.create("airline_13633", "models", "747-200B"); | |
bucket.addUniqueIn(fragment, true, PersistTo.NONE, ReplicateTo.NONE); | |
// add the 747-120 model to the models array | |
fragment = DocumentFragment.create("airline_13633", "models", "747-120"); | |
bucket.addUniqueIn(fragment, true, PersistTo.NONE, ReplicateTo.NONE); | |
// THIS WILL FAIL! the array already contains the 747-120 | |
try { | |
bucket.addUniqueIn(fragment, true, PersistTo.NONE, ReplicateTo.NONE); | |
} catch (PathExistsException ex) { | |
System.err.println("woops - model is already part of the models array."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it would be nice to include what the actual document looks like after these changes in order to figure out what you're doing here