Skip to content

Commit c8b106f

Browse files
author
Mike Dotson
committed
Issue 2340
- add new java client version - add update process for RiakObjects to TastOfRiak.java - add update process for POJOs to TasteOfRiak.java
1 parent 96bfdd1 commit c8b106f

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

Diff for: content/riak/kv/2.2.0/developing/getting-started/java.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ project's dependencies. Here is a Maven example:
3131
<dependency>
3232
<groupId>com.basho.riak</groupId>
3333
<artifactId>riak-client</artifactId>
34-
<version>2.0.0</version>
34+
<version>2.1.1</version>
3535
</dependency
3636
</dependencies>
3737
```

Diff for: content/riak/kv/2.2.0/developing/getting-started/java/crud-operations.md

+64-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ StoreValue storeOp = new StoreValue.Builder(quoteObject)
3030
.build();
3131
```
3232

33-
In line 76 we use our `client` object to execute the storage operation:
33+
We then use our `client` object to execute the storage operation:
3434

3535
```java
3636
StoreValue.Response response = client.execute(storeOp);
@@ -53,6 +53,24 @@ If the values are equal, as they should be, the Java client will say
5353
`Success! The object we created and the object we fetched have the same
5454
value`. If not, then the client will throw an exception.
5555

56+
## Updating Objects
57+
58+
Once we've read the object back in from Riak, we can update the object
59+
and store it back as we did before with the `StoreValue` object:
60+
61+
```java
62+
fetchedObject.setValue(BinaryValue.create("You can be my wingman any time.\n"));
63+
StoreValue updateOp = new StoreValue.Builder(fetchedObject)
64+
.withLocation(quoteObjectLocation)
65+
.build();
66+
StoreValue.Response updateOpResp = client.execute(updateOp);
67+
```
68+
69+
For more in depth information on updating objects and sibling resolution in
70+
Riak, see [Updating Objects](/riak/kv/2.2.0/developing/usage/updating-objects/)
71+
and [Conflict Resolution](/riak/kv/2.2.0/developing/usage/conflict-resolution/)
72+
documentation.
73+
5674
## Deleting Objects
5775

5876
Now that we've stored and then fetched the object, we can delete it by
@@ -117,3 +135,48 @@ If we fetch the object (using the same method we showed up above and in
117135
"copiesOwned": 3
118136
}
119137
```
138+
139+
Since we really like Moby Dick, let's buy a couple more copies
140+
and update the POJO.
141+
142+
To update the POJO, we would use `UpdateValue` by
143+
extending a new `BookUpdate` class as follows:
144+
145+
```java
146+
public static class BookUpdate extends UpdateValue.Update<Book> {
147+
private final Book update;
148+
public BookUpdate(Book update){
149+
this.update = update;
150+
}
151+
152+
@Override
153+
public Book apply(Book t) {
154+
if(t == null) {
155+
t = new Book();
156+
}
157+
158+
t.author = update.author;
159+
t.body = update.body;
160+
t.copiesOwned = update.copiesOwned;
161+
t.isbn = update.isbn;
162+
t.title = update.title;
163+
164+
return t;
165+
}
166+
}
167+
```
168+
169+
Then using the `BookUpdate` class with our `mobyDick` object:
170+
171+
```java
172+
mobyDick.copiesOwned = 5;
173+
UpdateBook updatedBook = new BookUpdate(mobyDick);
174+
175+
UpdateValue updateValue = new UpdateValue.Builder(mobyDickLocation).withUpdate(updatedBook).build();
176+
UpdateValue.Response response = client.execute(updateValue);
177+
```
178+
179+
For more in depth information on updating objects and sibling resolution in
180+
Riak, see [Updating Objects](/riak/kv/2.2.0/developing/usage/updating-objects/)
181+
and [Conflict Resolution](/riak/kv/2.2.0/developing/usage/conflict-resolution/)
182+
documention.

Diff for: extras/code-examples/TasteOfRiak.java

+41
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import com.basho.riak.client.api.commands.kv.DeleteValue;
33
import com.basho.riak.client.api.commands.kv.FetchValue;
44
import com.basho.riak.client.api.commands.kv.StoreValue;
5+
import com.basho.riak.client.api.commands.kv.UpdateValue;
56
import com.basho.riak.client.core.RiakCluster;
67
import com.basho.riak.client.core.RiakNode;
78
import com.basho.riak.client.core.query.Location;
@@ -21,6 +22,30 @@ public static class Book {
2122
public Integer copiesOwned;
2223
}
2324

25+
// This will allow us to update the book object handling the
26+
// entire fetch/modify/update cycle.
27+
public static class BookUpdate extends UpdateValue.Update<Book> {
28+
private final Book update;
29+
public BookUpdate(Book update){
30+
this.update = update;
31+
}
32+
33+
@Override
34+
public Book apply(Book t) {
35+
if(t == null) {
36+
t = new Book();
37+
}
38+
39+
t.author = update.author;
40+
t.body = update.body;
41+
t.copiesOwned = update.copiesOwned;
42+
t.isbn = update.isbn;
43+
t.title = update.title;
44+
45+
return t;
46+
}
47+
}
48+
2449
// This will create a client object that we can use to interact with Riak
2550
private static RiakCluster setUpCluster() throws UnknownHostException {
2651
// This example will use only one node listening on localhost:10017
@@ -84,6 +109,14 @@ public static void main( String[] args ) {
84109
assert(fetchedObject.getValue().equals(quoteObject.getValue()));
85110
System.out.println("Success! The object we created and the object we fetched have the same value");
86111

112+
// Now update the fetched object
113+
fetchedObject.setValue(BinaryValue.create("You can be my wingman any time."));
114+
StoreValue updateOp = new StoreValue.Builder(fetchedObject)
115+
.withLocation(quoteObjectLocation)
116+
.build();
117+
StoreValue.Response updateOpResp = client.execute(updateOp);
118+
updateOpResp = client.execute(updateOp);
119+
87120
// And we'll delete the object
88121
DeleteValue deleteOp = new DeleteValue.Builder(quoteObjectLocation)
89122
.build();
@@ -119,6 +152,14 @@ public static void main( String[] args ) {
119152
assert(mobyDick.title.equals(fetchedBook.title));
120153
assert(mobyDick.author.equals(fetchedBook.author));
121154
// And so on...
155+
156+
// Now to update the book with additional copies
157+
mobyDick.copiesOwned = 5;
158+
BookUpdate updatedBook = new BookUpdate(mobyDick);
159+
UpdateValue updateValue = new UpdateValue.Builder(mobyDickLocation)
160+
.withUpdate(updatedBook).build();
161+
UpdateValue.Response response = client.execute(updateValue);
162+
122163
System.out.println("Success! All of our tests check out");
123164

124165
// Now that we're all finished, we should shut our cluster object down

0 commit comments

Comments
 (0)