You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here we cover CRUD operations, document expiration, and optimistic locking with CAS.
12
+
Here we cover CRUD operations, document expiration, and optimistic locking with CAS --
13
+
as well as KV Range scan, for querying without an index.
14
+
15
+
14
16
15
-
The complete code sample used on this page can be downloaded from https://github.com/couchbase/docs-sdk-java/blob/release/3.2/modules/howtos/examples/KvOperations.java[here].
17
+
The complete code sample used on this page can be downloaded from https://github.com/couchbase/docs-sdk-java/blob/release/3.8/modules/devguide/examples/java/KvOperations.java[here].
16
18
17
19
At its heart Couchbase Server is a high-performance key-value store, and the key-value interface outlined below is the fastest and best method to perform operations involving single documents.
{sqlpp_url}[{sqlpp} (formerly N1QL)] can also be used to perform many single-document operations but we very strongly recommend using the key-value API for this instead, as it can be much more efficient. The request can go directly to the correct node, there's no query parsing overhead, and it's over the highly optimized memcached binary protocol.
37
39
====
38
40
41
+
39
42
== JSON
43
+
40
44
The Couchbase Server is a key-value store that's agnostic to what's stored, but it's very common to store JSON so most of the examples below will focus on that use-case.
41
45
42
46
The Java SDK provides you with several options for working with JSON.
@@ -45,7 +49,8 @@ If you pass any object (like the provided `JsonObject` and `JsonArray`), includi
45
49
the SDK will use its internal JSON codec (utilizing https://github.com/FasterXML/jackson[Jackson]) to encode/decode those objects transparently.
46
50
The SDK also supports custom transcoders and serializers which are covered separately.
47
51
48
-
== Upsert
52
+
53
+
== CRUD
49
54
50
55
[TIP]
51
56
.Sub-Document Operations
@@ -55,6 +60,7 @@ Where the number of operations or other circumstances make bandwidth a significa
55
60
the SDK can work on just a specific _path_ of the document with xref:subdocument-operations.adoc[Sub-Document Operations].
56
61
====
57
62
63
+
=== Upsert
58
64
Here is a simple upsert operation, which will insert the document if it does not exist, or replace it if it does.
59
65
60
66
We'll use the built-in JSON types for simplicity, but you can use different types if you want.
We've tried upserting and inserting documents into Couchbase Server, let's get them back:
91
98
@@ -105,7 +112,8 @@ Once we have a `GetResult`, we can use `contentAsObject()` to turn the content b
105
112
or use the more generic `contentAs(T.class)` equivalent to turn it back into other entity structures.
106
113
In fact, the `contentAsObject()` method is just a convenience method for `contentAs(JsonObject.class)`.
107
114
108
-
== Replace
115
+
=== Replace
116
+
109
117
A very common sequence of operations is to `get` a document, modify its contents, and `replace` it.
110
118
111
119
[source,java]
@@ -129,7 +137,7 @@ See xref:concurrent-document-mutations.adoc[this detailed description of CAS] fo
129
137
130
138
In general, you'll want to provide a CAS value whenever you `replace` a document, to prevent overwriting another agent's mutations.
131
139
132
-
== Retrying on CAS failures
140
+
=== Retrying on CAS failures
133
141
134
142
But if we get a CAS mismatch, we usually just want to retry the operation.
135
143
Let's see a more advanced `replace` example that shows one way to handle this:
@@ -147,7 +155,7 @@ In later chapters we cover more sophisticated approaches to this, including asyn
147
155
All these should be in place for robust, production ready code.
148
156
149
157
150
-
== Removing
158
+
=== Removing
151
159
152
160
Removing a document is straightforward:
153
161
@@ -181,14 +189,16 @@ If 6.5 or above is being used, you can take advantage of the xref:concept-docs:d
181
189
in which Couchbase Server will only return success to the SDK after the requested replication level has been achieved.
182
190
The three replication levels are:
183
191
184
-
* `Majority` - The server will ensure that the change is available in memory on the majority of configured replicas.
185
-
* `MajorityAndPersistToActive` - Majority level, plus persisted to disk on the active node.
186
-
* `PersistToMajority` - Majority level, plus persisted to disk on the majority of configured replicas.
192
+
* `Majority` -- The server will ensure that the change is available in memory on the majority of configured replicas.
193
+
* `MajorityAndPersistToActive` -- Majority level, plus persisted to disk on the active node.
194
+
* `PersistToMajority` -- Majority level, plus persisted to disk on the majority of configured replicas.
187
195
188
196
The options are in increasing levels of safety.
189
-
Note that nothing comes for free - for a given node, waiting for writes to storage is considerably slower than waiting for it to be available in-memory.
197
+
Note that nothing comes for free -- for a given node, waiting for writes to storage is considerably slower than waiting for it to be available in-memory.
190
198
These tradeoffs, as well as which settings may be tuned, are discussed in the xref:concept-docs:durability-replication-failure-considerations.adoc#durable-writes[durability page].
191
199
200
+
// remove 6.5 stuff once 8.0 is released?
201
+
192
202
If a version of Couchbase Server lower than 6.5 is being used then the application can fall-back to xref:concept-docs:durability-replication-failure-considerations.adoc#older-server-versions['client verified' durability].
193
203
Here the SDK will do a simple poll of the replicas and only return once the requested durability level is achieved.
NOTE: Increment & Decrement are considered part of the ‘binary’ API and as such may still be subject to change
287
297
298
+
See the https://docs.couchbase.com/sdk-api/couchbase-java-client/com/couchbase/client/java/kv/Increment.html[API Reference] for full details.
299
+
288
300
TIP: Setting the document expiry time only works when a document is created, and it is not possible to update the expiry time of an existing counter document with the Increment method -- to do this during an increment, use with the `Touch()` method.
289
301
290
302
@@ -387,4 +399,4 @@ Another way of increasing network performance is to _pipeline_ operations with x
387
399
388
400
As well as various xref:concept-docs:data-model.adoc[Formats] of JSON, Couchbase can work directly with xref:concept-docs:nonjson.adoc[arbitrary bytes, or binary format].
389
401
390
-
Our xref:n1ql-queries-with-sdk.adoc[Query Engine] enables retrieval of information using the SQL-like syntax of {sqlpp}.
402
+
Our xref:sqlpp-queries-with-sdk.adoc[Query Engine] enables retrieval of information using the SQL-like syntax of {sqlpp}.
0 commit comments