Support of Couchbase for Java
CAST supports Couchbase via its com.castsoftware.nosqljava extension. Details about how this support is provided for Java source code is discussed below.
Supported Libraries
Library | Version | Supported |
---|---|---|
Couchbase | 2.0.0 to 3.4.7 |
Supported Operations (2.x)
Support is provided for CRUD operations only when jars are configured inside the classpath. In all other situations, CRUD operations for Couchbase 2.x are NOT supported.
Operation |
Methods Supported |
---|---|
Insert | com.couchbase.client.java.Bucket.insert com.couchbase.client.java.AsyncBucket.insert com.couchbase.client.java.Bucket.upsert com.couchbase.client.java.Bucket.append com.couchbase.client.java.Bucket.prepend |
Select | com.couchbase.client.java.Bucket.get com.couchbase.client.java.AsyncBucket.get com.couchbase.client.java.Bucket.getAndLock com.couchbase.client.java.Bucket.getAndTouch com.couchbase.client.java.AsyncBucket.getAndLock com.couchbase.client.java.query.N1qlQuery.parameterized com.couchbase.client.java.query.N1qlQuery.simple com.couchbase.client.java.AsyncBucket.exists |
Delete | com.couchbase.client.java.Bucket.remove com.couchbase.client.java.AsyncBucket.remove |
Update | com.couchbase.client.java.Bucket.replace com.couchbase.client.java.AsyncBucket.replace |
Supported Operations (3.x)
Support is provided for CRUD operations in Couchbase 3.x in all situations.
Operation | Methods Supported |
---|---|
Insert | com.couchbase.client.java.Collection.insert com.couchbase.client.java.Collection.upsert com.couchbase.client.java.AsyncCollection.insert com.couchbase.client.java.AsyncCollection.upsert com.couchbase.client.java.ReactiveCollection.insert com.couchbase.client.java.ReactiveCollection.upsert |
Select | com.couchbase.client.java.Collection.get com.couchbase.client.java.Collection.getAndTouch com.couchbase.client.java.Collection.getAndLock com.couchbase.client.java.Collection.exists com.couchbase.client.java.Cluster.query com.couchbase.client.java.Scope.query com.couchbase.client.java.AsyncCollection.get com.couchbase.client.java.AsyncCollection.getAndTouch com.couchbase.client.java.AsyncCollection.getAndLock com.couchbase.client.java.AsyncCollection.exists com.couchbase.client.java.AsyncCollection.query com.couchbase.client.java.AsyncCollection.query com.couchbase.client.java.ReactiveCollection.get com.couchbase.client.java.ReactiveCollection.getAndTouch com.couchbase.client.java.ReactiveCollection.getAndLock com.couchbase.client.java.ReactiveCollection.exists com.couchbase.client.java.ReactiveCollection.query com.couchbase.client.java.ReactiveCollection.query |
Delete | com.couchbase.client.java.Collection.remove com.couchbase.client.java.AsyncCollection.remove com.couchbase.client.java.ReactiveCollection.remove |
Update | com.couchbase.client.java.Collection.replace com.couchbase.client.java.AsyncCollection.replace com.couchbase.client.java.ReactiveCollection.replace |
What results can you expect?
Objects
Icon |
Description |
---|---|
Java Couchbase Cluster | |
Java Couchbase Bucket | |
Java Couchbase Collection (3.x only) | |
Java Couchbase Unknown Cluster | |
|
Java Couchbase Unknown Bucket |
Java Couchbase Unknown Collection (3.x only) |
Links (Couchbase 2.x)
Link type | Source and destination of link | Methods supported |
---|---|---|
belongsTo | From Java Couchbase Collection object to Java Couchbase Bucket object and Java Couchbase Bucket object to Java Couchbase Cluster object |
- |
useLink | Between the caller Couchbase Java Method objects and Java Couchbase Buckets/Collection objects | Upsert N1QL.parameterized N1QL.simple |
useSelectLink |
Between the caller Couchbase Java Method objects and Java Couchbase Buckets/Collection objects | get() async().get() getAndTouch() async().getAndTouch() getAndLock() async().getAndLock() exists() async().exists() |
useUpdateLink | Between the caller Couchbase Java Method objects and Java Couchbase Buckets/Collection objects | replace() async().replace() append() async().append() prepend() async.prepend() |
useDeleteLink | Between the caller Couchbase Java Method objects and Java Couchbase Buckets/Collection objects | remove() async().remove() |
useInsertLink | Between the caller Couchbase Java Method objects and Java Couchbase Buckets/Collection objects | insert async().insert() |
Links (Couchbase 3.x)
Link type | Source and destination of link | Methods supported |
---|---|---|
parentLink | Between Couchbase Connection object and Bucket object Between Couchbase bucket object and Collection object |
- |
useLink | Between the caller Couchbase Java Method objects and Couchbase Buckets objects | Upsert |
useSelectLink |
Between the caller Couchbase Java Method objects and Couchbase Buckets objects | get() async().get() getAndTouch() async().getAndTouch() getAndLock() async().getAndLock() exists() async().exists() Cluster().query() Scope().query() |
useUpdateLink | Between the caller Couchbase Java Method objects and Couchbase Buckets objects | replace() async().replace() |
useDeleteLink | Between the caller Couchbase Java Method objects and Couchbase Buckets objects | remove() async().remove() |
useInsertLink | Between the caller Couchbase Java Method objects and Couchbase Buckets objects | insert async().insert() |
Code examples
Cluster and Bucket Object - 2.x
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
}
}
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
Bucket myBucket1 = cluster.openBucket("example1");
}
}
Connection, Bucket and Cluster object - 3.x
public static void main(String... args) {
Cluster cluster = Cluster.connect("localhost", "Administrator", "password");
Bucket bucket = cluster.bucket("travel-sample");
Scope scope = bucket.scope("_default");
Collection collection = scope.collection("_default");
Insert operations - 2.x
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
Bucket myBucket = cluster.openBucket("example");
JsonObject content = JsonObject.empty()
.put("name", "John Doe")
.put("type", "Person")
.put("email", "john.doe@mydomain.com")
.put("homeTown", "Chicago");
String id = UUID.randomUUID().toString();
JsonDocument document = JsonDocument.create(id, content);
JsonDocument inserted = myBucket.insert(document);
}
}
![]](insertlink_insert.PNG)
Insert operations - 3.x
public void insert()
{
System.out.println("\nExample: [insert]");
// tag::insert[]
try {
JsonObject content = JsonObject.create().put("title", "My Blog Post 2");
MutationResult insertResult = collection.insert("document-key2", content);
} catch (DocumentExistsException ex) {
System.err.println("The document already exists!");
} catch (CouchbaseException ex) {
System.err.println("Something else happened: " + ex);
}
// end::insert[]
}
Select operations - 2.x
get()
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
Bucket myBucket = cluster.openBucket("example");
JsonObject content = JsonObject.empty()
.put("name", "John Doe")
.put("type", "Person")
.put("email", "john.doe@mydomain.com")
.put("homeTown", "Chicago");
String id = UUID.randomUUID().toString();
JsonDocument document = JsonDocument.create(id, content);
JsonDocument inserted = myBucket.insert(document);
JsonDocument document1 = JsonDocument.create(id, content1);
JsonDocument gets = myBucket.get(document);
System.out.println(gets);
}
}
getAndTouch()
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "password");
Bucket myBucket = cluster.openBucket("example");
JsonObject content = JsonObject.empty()
.put("name", "John Doe")
.put("type", "Person")
.put("email", "john.doe@mydomain.com")
.put("homeTown", "Chicago");
String id = UUID.randomUUID().toString();
JsonDocument document = JsonDocument.create(id, content);
JsonDocument inserted = myBucket.insert(document);
JsonDocument document1 = JsonDocument.create(id, content1);
JsonDocument getandtouch = myBucket.getAndTouch(document);
System.out.println(getandtouch);
}
}
getAndLock()
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
Bucket myBucket = cluster.openBucket("example");
JsonObject content = JsonObject.empty()
.put("name", "John Doe")
.put("type", "Person")
.put("email", "john.doe@mydomain.com")
.put("homeTown", "Chicago");
String id = UUID.randomUUID().toString();
JsonDocument document = JsonDocument.create(id, content);
JsonDocument inserted = myBucket.insert(document);
JsonDocument document1 = JsonDocument.create(id, content1);
JsonDocument getandlock = myBucket.getAndLock(document, 10);
System.out.println(getandlock);
}
}
exists()
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
Bucket myBucket = cluster.openBucket("example");
JsonObject content = JsonObject.empty()
.put("name", "John Doe")
.put("type", "Person")
.put("email", "john.doe@mydomain.com")
.put("homeTown", "Chicago");
String id = UUID.randomUUID().toString();
JsonDocument document = JsonDocument.create(id, content);
JsonDocument inserted = myBucket.insert(document);
JsonDocument document1 = JsonDocument.create(id, content1);
Boolean exists = myBucket.exists(document);
System.out.println(exists);
}
}
Select operations - 3.x
public void get()
{
System.out.println("\nExample: [get-simple]");
// tag::get-simple[]
try {
GetResult getResult = collection.get("document-key");
String title = getResult.contentAsObject().getString("title");
System.out.println(title); // title == "My Blog Post"
} catch (DocumentNotFoundException ex) {
System.out.println("Document not found!");
}
// end::get-simple[]
}
public void getAndTouch()
{
System.out.println("\nExample: [expiry-touch]");
// tag::expiry-touch[]
GetResult result = collection.getAndTouch("my-document3", Duration.ofDays(1));
// end::expiry-touch[]
System.out.println("cas value: " + result.cas());
}
Update Operations - 2.x
replace()
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
Bucket myBucket = cluster.openBucket("example");
JsonObject content = JsonObject.empty()
.put("name", "John Doe")
.put("type", "Person")
.put("email", "john.doe@mydomain.com")
.put("homeTown", "Chicago");
JsonObject content3 = JsonObject.empty()
.put("Emp_Trigram", "ABC")
.put("Emp_id", "001");
JsonDocument document1 = JsonDocument.create(id, content1);
String id = UUID.randomUUID().toString();
JsonDocument document = JsonDocument.create(id, content);
JsonDocument inserted = myBucket.insert(document);
JsonDocument document1 = JsonDocument.create(id, content1);
JsonDocument removed = myBucket.replace(document1);
System.out.println(myBucket);
}
}
prepend()
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
Bucket myBucket = cluster.openBucket("example");
JsonObject content = JsonObject.empty()
.put("name", "John Doe")
.put("type", "Person")
.put("email", "john.doe@mydomain.com")
.put("homeTown", "Chicago");
String id = UUID.randomUUID().toString();
JsonDocument document = JsonDocument.create(id, content);
JsonDocument inserted = myBucket.insert(document);
JsonObject content3 = JsonObject.empty()
.put("Emp_Trigram", "ABC")
.put("Emp_id", "001");
JsonDocument document1 = JsonDocument.create(id, content1);
JsonDocument prepended = myBucket.prepend(document1);
}
}
append()
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
Bucket myBucket = cluster.openBucket("example");
JsonObject content = JsonObject.empty()
.put("name", "John Doe")
.put("type", "Person")
.put("email", "john.doe@mydomain.com")
.put("homeTown", "Chicago");
String id = UUID.randomUUID().toString();
JsonDocument document = JsonDocument.create(id, content);
JsonDocument inserted = myBucket.insert(document);
JsonObject content3 = JsonObject.empty()
.put("Emp_Trigram", "ABC")
.put("Emp_id", "001");
JsonDocument document1 = JsonDocument.create(id, content1);
JsonDocument upserted = myBucket1.upsert(document);
JsonDocument upserted1 = myBucket1.upsert(document, PersistTo.NONE);
JsonDocument upserted2 = myBucket1.upsert(document, ReplicateTo.NONE);
JsonDocument upserted3 = myBucket1.upsert(document, PersistTo.NONE, ReplicateTo.NONE);
JsonDocument upserted4 = myBucket1.upsert(document, 100, TimeUnit.MILLISECONDS);
JsonDocument upserted5 = myBucket1.upsert(document, PersistTo.NONE, 100, TimeUnit.MILLISECONDS);
JsonDocument upserted6 = myBucket1.upsert(document, ReplicateTo.NONE, 100, TimeUnit.MILLISECONDS);
JsonDocument upserted7 = myBucket1.upsert(document, PersistTo.NONE, ReplicateTo.NONE, 100, TimeUnit.MILLISECONDS);
}
}
Update Operations - 3.x
public void get()
{
System.out.println("\nExample: [expiry-replace]");
// tag::expiry-replace[]
GetResult found = collection.get("my-document3", getOptions().withExpiry(true));
MutationResult result = collection.replace("my-document3", json,
replaceOptions().expiry(found.expiryTime().get()));
// end::expiry-replace[]
System.out.println("cas value: " + result.cas());
}
Delete Operations - 2.x
remove()
public class App
{
public static void main( String[] args )
{
System.out.println("Create connection");
final Cluster cluster = CouchbaseCluster.create("127.0.0.1");
cluster.authenticate("Administrator", "Password");
Bucket myBucket = cluster.openBucket("example");
JsonObject content = JsonObject.empty()
.put("name", "John Doe")
.put("type", "Person")
.put("email", "john.doe@mydomain.com")
.put("homeTown", "Chicago");
String id = UUID.randomUUID().toString();
JsonDocument document = JsonDocument.create(id, content);
JsonDocument inserted = myBucket.insert(document);
JsonDocument document1 = JsonDocument.create(id, content1);
JsonDocument removed = myBucket.remove(document);
System.out.println(myBucket);
}
}
Delete Operations - 3.x
public void remove()
{
System.out.println("\nExample: [remove]");
// tag::remove[]
try {
collection.remove("my-document");
} catch (DocumentNotFoundException ex) {
System.out.println("Document did not exist when trying to remove");
}
// end::remove[]
}
Batch Operations- 2.x
int docsToCreate = 100;
List<JsonDocument> documents = new ArrayList<JsonDocument>();
for (int i = 0; i < docsToCreate; i++) {
JsonObject content = JsonObject.create()
.put("counter", i)
.put("name", "Foo Bar");
documents.add(JsonDocument.create("doc-"+i, content));
}
// Insert them in one batch, waiting until the last one is done.
Observable
.from(documents)
.flatMap(new Func1<JsonDocument, Observable<JsonDocument>>() {
@Override
public Observable<JsonDocument> call(final JsonDocument docToInsert) {
return bucket.async().insert(docToInsert);
}
})
.last()
.toBlocking()
.single();
Queries - N1QL - 2.x
protected static N1qlQuery buildQuery(Statement statement, JsonValue queryPlaceholderValues, ScanConsistency scanConsistency) {
Cluster cluster = CouchbaseCluster.create("localhost");
N1qlParams n1qlParams = N1qlParams.build().consistency(scanConsistency);
N1qlQuery query;
if (queryPlaceholderValues instanceof JsonObject && !((JsonObject) queryPlaceholderValues).isEmpty()) {
query = N1qlQuery.parameterized(statement, (JsonObject) queryPlaceholderValues, n1qlParams);
} else if (queryPlaceholderValues instanceof JsonArray && !((JsonArray) queryPlaceholderValues).isEmpty()) {
query = N1qlQuery.parameterized(statement, (JsonArray) queryPlaceholderValues, n1qlParams);
} else {
query = N1qlQuery.simple(statement, n1qlParams);
}
return query;
}
Queries - Cluster - 3.x
private void initHotel(String bucketName) {
hotelcluster = Cluster.connect("HotelServer", "Administrator", "password");
hotelBucket = hotelcluster.bucket(bucketName);
hotelScope = hotelBucket.scope("inventory");
}
public void clusterQuery() throws Exception {
QueryResult result2 = hotelcluster.query("SELECT h.name, h.phone FROM default:`travel-sample`.inventory.hotel h\r\n"
+ "WHERE h.city=\"Manchester\" AND h.directions IS NOT MISSING ORDER BY h.name LIMIT 10",
queryOptions().metrics(true));
}
public static void main(String[] args) throws Exception {
App7 obj = new App7();
obj.initHotel("hotelBucket");
obj.scopeQuery();
obj.clusterQuery();
}
Queries - Scope - 3.x
private void init(String bucketName) {
cluster = Cluster.connect("App5127.0.0.1", "Administrator", "password");
bucket = cluster.bucket(bucketName);
scope = bucket.scope("inventory");
}
public void scopeQuery() throws Exception {
QueryResult queryResult2 = scope.query("select * FROM `passenger`", queryOptions().timeout(Duration.ofSeconds(3)));
}
public static void main(String[] args) throws Exception {
App7 obj = new App7();
obj.init("airlineBucket");
obj.scopeQuery();
obj.clusterQuery();
}
Known Limitations
- If the bucket or cluster name is not resolved, then “Java Couchbase Unknown Objects” will be created.