A JSON By Example
This appendix contains a complete Java example of how to use JSON data in a Oracle NoSQL Database store.
The example loads a series of table rows, using JSON objects to represent each row. The example then updates all table rows that contain a home address in Boston so that the zip code for that address is updated from 02102
to 02102-1000
.
Our sample data deliberately contains some table rows with null and missing fields so as to illustrate some (but by no means all) of the error handling that is required when working with JSON data. It is possible to be endlessly creative when providing broken JSON to the store. Any production code would have to be a great deal more robust than what is shown here.
The update operation is shown three different ways in the following example. While the actual update is always the same (see the UpdateJSON.updateZipCode() method), there are three different ways to seek out rows with a home address in Boston:
-
No query.
This simply iterates over the entire table, examining each row in turn. See UpdateJSON.updateTableWithoutQuery().
-
With an index.
This uses a JSON index to retrieve all table rows where the home address is in Boston. See UpdateJSON.updateTableWithIndex().
-
With a SQL Query.
This uses a SQL statement with a
executeSync()
method to retrieve all relevant table rows. See UpdateJSON.updateTableUsingSQLQuery().
The next section shows some of the sample data used by this example. The description of the example itself begins with UpdateJSON.
If you want to follow along with the example, and see all of the sample data, you can find this example in the Examples
download from here. The example and its sample data can be found in the Table
folder.
When compiling the example, make sure that kvclient.jar
is in your classpath. For example:
javac -d . -cp <KVHOME>/lib/kvclient.jar UpdateJSON.java
You can run this program against a store or a kvlite instance that does not have security enabled.
java -cp .:<KVHOME>/lib/kvclient.jar table.UpdateJSON
By default, this example uses localhost:5000
, but you can set the helper host and port at the command line using the -hostport
parameter.