|
1 | | -// tag::cloud-connect[] |
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Couchbase, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
2 | 17 | // tag::imports[] |
3 | | -import com.couchbase.client.java.*; |
4 | | -import com.couchbase.client.java.kv.*; |
5 | | -import com.couchbase.client.java.json.*; |
6 | | -import com.couchbase.client.java.query.*; |
| 18 | +import com.couchbase.client.java.Cluster; |
| 19 | +import com.couchbase.client.java.ClusterOptions; |
| 20 | +import com.couchbase.client.java.env.ClusterEnvironment; |
| 21 | +import com.couchbase.client.java.env.SecurityConfig; |
| 22 | +import com.couchbase.client.java.json.JsonObject; |
| 23 | +import com.couchbase.client.java.kv.GetResult; |
| 24 | +import com.couchbase.client.java.kv.ReplaceOptions; |
7 | 25 |
|
8 | 26 | import java.time.Duration; |
| 27 | +import java.util.UUID; |
9 | 28 | // end::imports[] |
10 | 29 |
|
11 | | -public class StartUsingCapella { |
12 | | - // tag::connect-info[] |
13 | | - // Update these variables to point to your Couchbase Capella instance and credentials. |
14 | | - static String connectionString = "couchbases://cb.<your-endpoint-here>.cloud.couchbase.com"; |
15 | | - static String username = "username"; |
16 | | - static String password = "Password!123"; |
17 | | - static String bucketName = "travel-sample"; |
18 | | - // end::connect-info[] |
| 30 | +public class Cloud { |
| 31 | + public static void main(String[] args) { |
| 32 | + // tag::connect[] |
| 33 | + // Update this to your cluster |
| 34 | + String endpoint = "cb.<your-endpoint>.cloud.couchbase.com"; |
| 35 | + String username = "username"; |
| 36 | + String password = "Password!123"; |
| 37 | + String bucketName = "travel-sample"; |
| 38 | + |
| 39 | + ClusterEnvironment env = ClusterEnvironment.builder() |
| 40 | + .securityConfig(SecurityConfig.enableTls(true)) |
| 41 | + // Sets a pre-configured profile called "wan-development" to help avoid latency issues |
| 42 | + // when accessing Capella from a different Wide Area Network |
| 43 | + // or Availability Zone (e.g. your laptop). |
| 44 | + .applyProfile(ClusterEnvironment.WanDevelopmentProfile.INSTANCE) |
| 45 | + .build(); |
| 46 | + |
| 47 | + Cluster cluster = Cluster.connect( |
| 48 | + "couchbases://" + endpoint, |
| 49 | + ClusterOptions.clusterOptions(username, password).environment(env) |
| 50 | + ); |
| 51 | + // end::connect[] |
| 52 | + |
| 53 | + // tag::bucket[] |
| 54 | + var bucket = cluster.bucket(bucketName); |
| 55 | + bucket.waitUntilReady(Duration.ofSeconds(30)); |
| 56 | + // end::bucket[] |
19 | 57 |
|
20 | | - public static void main(String... args) { |
21 | | - // tag::connect-env[] |
22 | | - Cluster cluster = Cluster.connect( |
23 | | - connectionString, |
24 | | - ClusterOptions.clusterOptions(username, password).environment(env -> { |
25 | | - // Sets a pre-configured profile called "wan-development" to help avoid |
26 | | - // latency issues when accessing Capella from a different Wide Area Network |
27 | | - // or Availability Zone (e.g. your laptop). |
28 | | - env.applyProfile("wan-development"); |
29 | | - }) |
30 | | - ); |
31 | | - // end::connect-env[] |
| 58 | + // tag::collection[] |
| 59 | + var collection = bucket.scope("inventory").collection("airport"); |
| 60 | + // end::collection[] |
32 | 61 |
|
33 | | - // tag::bucket[] |
34 | | - // Get a bucket reference |
35 | | - Bucket bucket = cluster.bucket(bucketName); |
36 | | - bucket.waitUntilReady(Duration.ofSeconds(10)); |
37 | | - // end::bucket[] |
| 62 | + // tag::json[] |
| 63 | + JsonObject json = JsonObject.create().put("status", "awesome"); |
| 64 | + // end::json[] |
38 | 65 |
|
39 | | - // tag::collection[] |
40 | | - // Get a user-defined collection reference |
41 | | - Scope scope = bucket.scope("tenant_agent_00"); |
42 | | - Collection collection = scope.collection("users"); |
43 | | - // end::collection[] |
| 66 | + // tag::upsert[] |
| 67 | + String docId = UUID.randomUUID().toString(); |
| 68 | + try { |
| 69 | + collection.upsert(docId, json); |
| 70 | + } catch (Exception e) { |
| 71 | + System.err.println("Error: " + e.getMessage()); |
| 72 | + } |
| 73 | + // end::upsert[] |
44 | 74 |
|
45 | | - // tag::upsert-get[] |
46 | | - // Upsert Document |
47 | | - MutationResult upsertResult = collection.upsert( |
48 | | - "my-document", |
49 | | - JsonObject.create().put("name", "mike") |
50 | | - ); |
| 75 | + // tag::get[] |
| 76 | + // Get a document |
| 77 | + try { |
| 78 | + GetResult result = collection.get(docId); |
| 79 | + JsonObject content = result.contentAsObject(); |
| 80 | + String status = content.getString("status"); |
| 81 | + System.out.println("Couchbase is " + status); |
| 82 | + } catch (Exception e) { |
| 83 | + System.err.println("Error getting document: " + e.getMessage()); |
| 84 | + } |
| 85 | + // end::get[] |
51 | 86 |
|
52 | | - // Get Document |
53 | | - GetResult getResult = collection.get("my-document"); |
54 | | - String name = getResult.contentAsObject().getString("name"); |
55 | | - System.out.println(name); // name == "mike" |
56 | | - // end::upsert-get[] |
| 87 | + // tag::get-for[] |
| 88 | + try { |
| 89 | + String status = collection.get(docId) |
| 90 | + .contentAsObject() |
| 91 | + .getString("status"); |
| 92 | + System.out.println("Couchbase is " + status); |
| 93 | + } catch (Exception e) { |
| 94 | + System.err.println("Error: " + e.getMessage()); |
| 95 | + } |
| 96 | + // end::get-for[] |
57 | 97 |
|
58 | | - // tag::n1ql-query[] |
59 | | - // Call the query() method on the scope object and store the result. |
60 | | - Scope inventoryScope = bucket.scope("inventory"); |
61 | | - QueryResult result = inventoryScope.query("SELECT * FROM airline WHERE id = 10;"); |
| 98 | + // tag::replace-options[] |
| 99 | + try { |
| 100 | + collection.replace( |
| 101 | + docId, |
| 102 | + json, |
| 103 | + ReplaceOptions.replaceOptions() |
| 104 | + .expiry(Duration.ofSeconds(10)) |
| 105 | + .durability(com.couchbase.client.java.kv.Durability.MAJORITY) |
| 106 | + ); |
| 107 | + } catch (Exception e) { |
| 108 | + System.err.println("Error: " + e.getMessage()); |
| 109 | + } |
| 110 | + // end::replace-options[] |
62 | 111 |
|
63 | | - // Return the result rows with the rowsAsObject() method and print to the terminal. |
64 | | - System.out.println(result.rowsAsObject()); |
65 | | - // end::n1ql-query[] |
66 | | - } |
| 112 | + // tag::replace-named[] |
| 113 | + try { |
| 114 | + collection.replace( |
| 115 | + docId, |
| 116 | + json, |
| 117 | + ReplaceOptions.replaceOptions() |
| 118 | + .durability(com.couchbase.client.java.kv.Durability.MAJORITY) |
| 119 | + ); |
| 120 | + } catch (Exception e) { |
| 121 | + System.err.println("Error: " + e.getMessage()); |
| 122 | + } |
| 123 | + // end::replace-named[] |
| 124 | + } |
67 | 125 | } |
68 | | -// end::cloud-connect[] |
|
0 commit comments