-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk-connection-java.java
More file actions
109 lines (92 loc) · 3.58 KB
/
sdk-connection-java.java
File metadata and controls
109 lines (92 loc) · 3.58 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
/**
* Couchbase SDK connection singleton — Java
* Copy and adapt for your application.
*
* Dependencies (Maven):
* <dependency>
* <groupId>com.couchbase.client</groupId>
* <artifactId>java-client</artifactId>
* <version>3.7.4</version>
* </dependency>
*/
package com.example;
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.core.env.TimeoutConfig;
import java.time.Duration;
public class CouchbaseConnection {
// --- Configuration ---
private static final String CB_HOST = "localhost"; // or "cb.xxxxx.cloud.couchbase.com" for Capella
private static final String CB_USER = "app-service-user";
private static final String CB_PASSWORD = "AppSecret123!";
private static final String CB_BUCKET = "myapp";
private static final String CB_SCOPE = "_default";
private static final String CB_COLLECTION = "_default";
private static volatile Cluster cluster;
private static volatile Collection collection;
private CouchbaseConnection() {}
/** Return the singleton Collection, initializing the cluster on first call. */
public static Collection getCollection() {
if (collection == null) {
synchronized (CouchbaseConnection.class) {
if (collection == null) {
init();
}
}
}
return collection;
}
/** Return the singleton Cluster (needed for queries and transactions). */
public static Cluster getCluster() {
getCollection(); // ensures cluster is initialized
return cluster;
}
private static void init() {
ClusterEnvironment env = ClusterEnvironment.builder()
.timeoutConfig(TimeoutConfig.builder()
.connectTimeout(Duration.ofSeconds(10))
.kvTimeout(Duration.ofMillis(2500))
.queryTimeout(Duration.ofSeconds(75))
.searchTimeout(Duration.ofSeconds(75))
.build())
.build();
// Use "couchbases://" for TLS (required for Capella, recommended for production)
cluster = Cluster.connect(
CB_HOST,
ClusterOptions.clusterOptions(CB_USER, CB_PASSWORD).environment(env)
);
cluster.waitUntilReady(Duration.ofSeconds(10));
collection = cluster
.bucket(CB_BUCKET)
.scope(CB_SCOPE)
.collection(CB_COLLECTION);
}
/** Call on application shutdown to release resources. */
public static void close() {
if (cluster != null) {
cluster.disconnect();
}
}
// --- Usage example ---
public static void main(String[] args) {
Collection col = getCollection();
Cluster cluster = getCluster();
// KV upsert
col.upsert("doc_1", com.couchbase.client.java.json.JsonObject.create()
.put("type", "example")
.put("value", 42));
// KV get
var result = col.get("doc_1");
System.out.println(result.contentAsObject());
// SQL++ query
var rows = cluster.query(
"SELECT * FROM `myapp`._default._default WHERE type = $type LIMIT 5",
com.couchbase.client.java.query.QueryOptions.queryOptions()
.parameters(com.couchbase.client.java.json.JsonObject.create().put("type", "example"))
);
rows.rowsAsObject().forEach(System.out::println);
close();
}
}