-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathStartUsing.java
More file actions
66 lines (57 loc) · 2.15 KB
/
StartUsing.java
File metadata and controls
66 lines (57 loc) · 2.15 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
// tag::start-using[]
// tag::imports[]
import com.couchbase.client.java.*;
import com.couchbase.client.java.kv.*;
import com.couchbase.client.java.json.*;
import com.couchbase.client.java.query.*;
import java.time.Duration;
// end::imports[]
public class StartUsing {
// tag::connect-info[]
// Update these variables to point to your Couchbase Server instance and credentials.
static String connectionString = "couchbase://127.0.0.1";
static String username = "Administrator";
static String password = "password";
static String bucketName = "travel-sample";
// end::connect-info[]
public static void main(String... args) {
// tag::connect-env[]
// Use the following code to connect to your cluster.
Cluster cluster = Cluster.connect(
connectionString,
ClusterOptions.clusterOptions(username, password).environment(env -> {
// Customize client settings by calling methods on the "env" variable.
})
);
// end::connect-env[]
// tag::bucket[]
// get a bucket reference
Bucket bucket = cluster.bucket(bucketName);
bucket.waitUntilReady(Duration.ofSeconds(10));
// end::bucket[]
// tag::collection[]
// get a user-defined collection reference
Scope scope = bucket.scope("tenant_agent_00");
Collection collection = scope.collection("users");
// end::collection[]
// tag::upsert-get[]
// Upsert Document
MutationResult upsertResult = collection.upsert(
"my-document",
JsonObject.create().put("name", "mike")
);
// Get Document
GetResult getResult = collection.get("my-document");
String name = getResult.contentAsObject().getString("name");
System.out.println(name); // name == "mike"
// end::upsert-get[]
// tag::n1ql-query[]
// Call the query() method on the scope object and store the result.
Scope inventoryScope = bucket.scope("inventory");
QueryResult result = inventoryScope.query("SELECT * FROM airline WHERE id = 10;");
// Return the result rows with the rowsAsObject() method and print to the terminal.
System.out.println(result.rowsAsObject());
// end::n1ql-query[]
}
}
// end::start-using[]