Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions modules/howtos/examples/ProvisioningResourcesBuckets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2022 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

import com.couchbase.client.core.error.BucketExistsException;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.manager.bucket.BucketManager;
import com.couchbase.client.java.manager.bucket.BucketSettings;
import com.couchbase.client.java.manager.bucket.BucketType;
import com.couchbase.client.java.manager.bucket.ConflictResolutionType;

public class ProvisioningResourcesBuckets {
public static void main(String[] args) {
// tag::creatingbucketmgr[]
Cluster cluster = Cluster.connect("localhost", "Administrator", "password");
BucketManager bucketMgr = cluster.buckets();
// end::creatingbucketmgr[]

{
System.out.println("[createBucket]");

// tag::createBucket[]
try {
BucketSettings bucketSettings = BucketSettings.create("hello")
.flushEnabled(false)
.replicaIndexes(true)
.ramQuotaMB(150)
.numReplicas(1)
.bucketType(BucketType.COUCHBASE)
.conflictResolutionType(ConflictResolutionType.SEQUENCE_NUMBER);

bucketMgr.createBucket(bucketSettings);
} catch (BucketExistsException e) {
System.out.println("Bucket already exists");
}
// end::createBucket[]
}

{
System.out.println("[updateBucket]");

// tag::updateBucket[]
BucketSettings settings = bucketMgr.getBucket("hello");
settings.flushEnabled(true);

bucketMgr.updateBucket(settings);
// end::updateBucket[]
}
{
System.out.println("[flushBucket]");

// tag::flushBucket[]
bucketMgr.flushBucket("hello");
// end::flushBucket[]
}
{
System.out.println("[removeBucket]");

// tag::removeBucket[]
bucketMgr.dropBucket("hello");
// end::removeBucket[]
}
}
}
71 changes: 71 additions & 0 deletions modules/howtos/examples/ProvisioningResourcesViews.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2022 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

import java.util.HashMap;
import java.util.Map;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.manager.view.DesignDocument;
import com.couchbase.client.java.manager.view.View;
import com.couchbase.client.java.manager.view.ViewIndexManager;
import com.couchbase.client.java.view.DesignDocumentNamespace;

public class ProvisioningResourcesViews {
public static void main(String[] args) {
// tag::viewmgr[]
Cluster cluster = Cluster.connect("localhost", "Administrator", "password");
Bucket bucket = cluster.bucket("travel-sample");
ViewIndexManager viewMgr = bucket.viewIndexes();
// end::viewmgr[]

{
// tag::createView[]
Map<String, View> views = new HashMap<>();
views.put(
"by_country",
new View("function (doc, meta) { if (doc.type == 'landmark') { emit([doc.country, doc.city], null); } }")
);
views.put(
"by_activity",
new View(
"function (doc, meta) { if (doc.type == 'landmark') { emit([doc.country, doc.city], null); } }",
"_count")
);

DesignDocument designDocument = new DesignDocument("landmarks", views);
viewMgr.upsertDesignDocument(designDocument, DesignDocumentNamespace.DEVELOPMENT);
// end::createView[]
}

{
// tag::getView[]
DesignDocument designDocument = viewMgr.getDesignDocument("landmarks", DesignDocumentNamespace.DEVELOPMENT);
System.out.print(designDocument);
// end::getView[]
}


{
// tag::publishView[]
viewMgr.publishDesignDocument("landmarks");
// end::publishView[]
}

{
// tag::removeView[]
viewMgr.dropDesignDocument("landmarks", DesignDocumentNamespace.PRODUCTION);
// end::removeView[]
}
}
}
121 changes: 121 additions & 0 deletions modules/howtos/examples/QueryIndexManagerExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Copyright (c) 2022 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/

import java.time.Duration;
import java.util.Arrays;
import com.couchbase.client.core.error.IndexExistsException;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.manager.query.BuildQueryIndexOptions;
import com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions;
import com.couchbase.client.java.manager.query.CreateQueryIndexOptions;
import com.couchbase.client.java.manager.query.DropPrimaryQueryIndexOptions;
import com.couchbase.client.java.manager.query.DropQueryIndexOptions;
import com.couchbase.client.java.manager.query.QueryIndexManager;
import com.couchbase.client.java.manager.query.WatchQueryIndexesOptions;

public class QueryIndexManagerExample {

public static void main(String... args) {
// tag::creating-index-mgr[]
Cluster cluster = Cluster.connect("localhost", "Administrator", "password");
QueryIndexManager queryIndexMgr = cluster.queryIndexes();
// end::creating-index-mgr[]

{
System.out.println("[primary]");

// tag::primary[]
CreatePrimaryQueryIndexOptions opts = CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions()
.scopeName("tenant_agent_01")
.collectionName("users")
// Set this if you wish to use a custom name
// .indexName("custom_name")
.ignoreIfExists(true);

queryIndexMgr.createPrimaryIndex("travel-sample", opts);
// end::primary[]
}

{
System.out.println("[secondary]");

// tag::secondary[]
try {
CreateQueryIndexOptions opts = CreateQueryIndexOptions.createQueryIndexOptions()
.scopeName("tenant_agent_01")
.collectionName("users");

queryIndexMgr.createIndex("travel-sample", "tenant_agent_01_users_email",
Arrays.asList("preferred_email"), opts);
} catch (IndexExistsException e) {
System.out.println("Index already exists");
}
// end::secondary[]
}

{
System.out.println("[defer-indexes]");

// tag::defer-indexes[]
try {
// Create a deferred index
CreateQueryIndexOptions createOpts = CreateQueryIndexOptions.createQueryIndexOptions()
.scopeName("tenant_agent_01")
.collectionName("users")
.deferred(true);

queryIndexMgr.createIndex("travel-sample", "tenant_agent_01_users_phone",
Arrays.asList("preferred_phone"), createOpts);

// Build any deferred indexes within `travel-sample`.tenant_agent_01.users
BuildQueryIndexOptions deferredOpts = BuildQueryIndexOptions.buildDeferredQueryIndexesOptions()
.scopeName("tenant_agent_01")
.collectionName("users");

queryIndexMgr.buildDeferredIndexes("travel-sample", deferredOpts);

// Wait for indexes to come online
WatchQueryIndexesOptions watchOpts = WatchQueryIndexesOptions.watchQueryIndexesOptions()
.scopeName("tenant_agent_01")
.collectionName("users");

queryIndexMgr.watchIndexes("travel-sample", Arrays.asList("tenant_agent_01_users_phone"),
Duration.ofSeconds(60), watchOpts);

} catch (IndexExistsException e) {
System.out.println("Index already exists");
}
// end::defer-indexes[]
}

{
System.out.println("[drop-primary-or-secondary-index]");

// tag::drop-primary-or-secondary-index[]
DropPrimaryQueryIndexOptions primaryIndexOpts = DropPrimaryQueryIndexOptions.dropPrimaryQueryIndexOptions()
.scopeName("tenant_agent_01")
.collectionName("users");

queryIndexMgr.dropPrimaryIndex("travel-sample", primaryIndexOpts);

// Drop a secondary index
DropQueryIndexOptions indexOpts = DropQueryIndexOptions.dropQueryIndexOptions()
.scopeName("tenant_agent_01")
.collectionName("users");

queryIndexMgr.dropIndex("travel-sample", "tenant_agent_01_users_email", indexOpts);
// end::drop-primary-or-secondary-index[]
}
}
}
Loading