Managing JSON documents-using transactions and not using transactions
Overview
Brief introduction
DB2 JSON is a Technology Preview that can be used for DB2 Linux, Unix and Windows 10.5, providing the following features:
A command-line shell for managing and querying JSON data
A Java API for application development
A wired listener is used to accept and respond to requests sent over the network.
Figure 1. DB2 JSON Component
This article describes how to use the Java interface to manage and query JSON documents in DB2 JSON document storage. Some aspects of the available ACID support will also be discussed, along with some limitations on combining transaction support options with throughput enhancement options.
Prerequisite
To run the sample application, you must have IBM DB2 10.5 for Linux, UNIX and Windows (or later) installed on your system. If you already have a JSON-enabled DB2 database that you can use to run the sample, remember the host name or IP address and port number, and continue to run the sample example of a simple Java application. Otherwise, complete the following steps.
Check environment variables:
Make sure that the PATH contains a Java Runtime environment (minimum JRE 1.5),
CLASSPATH contains Db2nosql.jar and JDBC drivers (Db2jcc.jar or Db2jcc4.jar).
Create a database.
Enable the database.
The following examples show how to create and enable a database.
Listing 1. Creating and Enabling databases
CREATE DATABASE JSONDB Automatic storage yes using CodeSet utf-8
Territory US collate using System pagesize 3/>db2nosql-user bob-hostname bobshost-port 50000-db jsondb-password mypwd-setup Enable
Terms
JSON storage: A DB2 database used as a JSON document store. Before you can start working with the JSON document using the IBM JSON solution, you need to obtain the necessary connection and authorization information to access the DB2 database server that is used as the JSON store.
JSON namespaces: DB2 SQL schemas are used to fully qualify collections, conceptually similar to MongoDB databases.
JSON Collection: A collection contains a set of documents that are represented in a DB2 table. The JSON collection supports flexible document patterns that do not enforce a certain structure.
A simple example of a Java application
Download the Sample.java from the download section of this article. To run the sample program in the Db2cmd window, complete the following steps:
Set CLASSPATH: Include Nosqljson.jar and Db2jcc.jar or Db2jcc4.jar into the classpath.
On Windows:
Suppose Db2jcc.jar is in the C:\Program Files\ibm\sqllib\java directory, Nosqljson.jar is in the C:\Program files\ibm\sqllib\json\lib directory, and then the The CLASSPATH settings are as follows:
Set classpath=.; C:\Program Files\ibm\sqllib\java\db2jcc.jar;
C:\Program Files\ibm\sqllib\json\lib\nosqljson.jar
On UNIX:
Suppose Db2jcc.jar is in the/sqllib/java directory, Nosqljson.jar is in the/sqllib/json/lib directory, and then the CLASSPATH is set as follows:
Export classpath=.:/ Sqllib/java/db2jcc.jar:/sqllib/json/lib/nosqljson.jar
Create a test directory and copy the sample program to this test directory.
Modify the sample program to fit the database name and connection information.
Compile and run the Java sample program in this test directory.
To compile the sample program:
Javac Sample.java
To run the Java sample program:
Java Sample
Java Sample program with annotations
The sample program inserts a document and submits a query to find it again. See Sample.java for a complete sample code.