-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathStartUsingCapella.java
More file actions
130 lines (116 loc) · 4.39 KB
/
Copy pathStartUsingCapella.java
File metadata and controls
130 lines (116 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2025 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.
*/
// tag::imports[]
import com.couchbase.client.core.env.WanDevelopmentProfile;
import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.java.Bucket;
import com.couchbase.client.java.Cluster;
import com.couchbase.client.java.ClusterOptions;
import com.couchbase.client.java.Collection;
import com.couchbase.client.java.env.ClusterEnvironment;
import com.couchbase.client.java.json.JsonObject;
import com.couchbase.client.java.kv.GetResult;
import com.couchbase.client.java.kv.ReplaceOptions;
import java.time.Duration;
import java.util.UUID;
import static com.couchbase.client.core.msg.kv.DurabilityLevel.MAJORITY;
// end::imports[]
public class StartUsingCapella {
public static void main(String[] args) {
// tag::connect[]
// Update this to your cluster
String endpoint = "cb.<your-endpoint>.cloud.couchbase.com";
String username = "username";
String password = "Password!123";
String bucketName = "travel-sample";
ClusterEnvironment env = ClusterEnvironment.builder()
.securityConfig(sc -> sc.enableTls(true))
// Sets a pre-configured profile called "wan-development" to help avoid latency issues
// when accessing Capella from a different Wide Area Network
// or Availability Zone (e.g. your laptop).
.applyProfile(new WanDevelopmentProfile().name())
.build();
Cluster cluster = Cluster.connect(
"couchbases://" + endpoint,
ClusterOptions.clusterOptions(username, password).environment(env)
);
// end::connect[]
// tag::bucket[]
Bucket bucket = cluster.bucket(bucketName);
bucket.waitUntilReady(Duration.ofSeconds(30));
// end::bucket[]
// tag::collection[]
Collection collection = bucket.scope("inventory").collection("airport");
// end::collection[]
// tag::json[]
JsonObject json = JsonObject.create().put("status", "awesome");
// end::json[]
// tag::upsert[]
String docId = UUID.randomUUID().toString();
try {
collection.upsert(docId, json);
} catch (CouchbaseException e) {
System.err.println("Error: " + e.getMessage());
}
// end::upsert[]
// tag::get[]
// Get a document
try {
GetResult result = collection.get(docId);
JsonObject content = result.contentAsObject();
String status = content.getString("status");
System.out.println("Couchbase is " + status);
} catch (Exception e) {
System.err.println("Error getting document: " + e.getMessage());
}
// end::get[]
// tag::get-for[]
try {
String status = collection.get(docId)
.contentAsObject()
.getString("status");
System.out.println("Couchbase is " + status);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
// end::get-for[]
// tag::replace-options[]
try {
collection.replace(
docId,
json,
ReplaceOptions.replaceOptions()
.expiry(Duration.ofSeconds(10))
.durability(MAJORITY)
);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
// end::replace-options[]
// tag::replace-named[]
try {
collection.replace(
docId,
json,
ReplaceOptions.replaceOptions()
.durability(MAJORITY)
);
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
// end::replace-named[]
}
}