TechTip: Implementing JSON in DB2 Made Easy

DB2
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

DB2 JSON is simple to implement and maintain.


Editor's note: This article is an excerpt from the book DB2 10.1/10.5 for Linux, UNIX, and Windows Database Administration (Exams 611 and 311): Certification Study Guide.

 

IBM DB2 10.5 FP1 introduced JSON support. DB2 JSON provides a complete storage/persistence solution for JSON documents. The solution combines application server-side components with JSON enhancements and functions in the DB2 engine.

 

This configuration provides a NoSQL API that uses DB2 to store, retrieve, modify, parse, and index JSON documents in DB2. The application server-side component is Java-based. So a Java API is included that allows native Java applications to exercise all the JSON capabilities directly.

 

For applications written in other languages, the solution also provides the Wire Listener Gateway (WLG) between MongoDB applications and a DB2 database server. This allows Mongo applications that are written in a supported language (such as Java, Node.js, or PyMongo) or that use the Mongo CLI to communicate with a DB2 database with no code changes. (Java applications can use the Wire Listener but may find advantage in using the Java API methods directly.)

 

DB2 JSON is simple to implement and maintain. The interface is contained within the nosqljson.jar file.

 

The following requirements are necessary to implement JSON support:

 

  • DB2 10.5.0.1 or higher Java client
  • The jar files listed below (which are included) as well as startup scripts and documentation
  • DFT_TABLE_ORG = ROW
  • DB2 database at 10.5 FP1 or higher
  • Java in shell PATH ( Java 1.5 64-bit or higher)
  • CLASSPATH jar files:

»» db2jcc.jar (Java client)

»» nosqljson.jar (DB2 noSQL JSON)

»» wplistener.jar (Wire Protocol Listener)

»» js.jar (Rhino JavaScript engine—not required if only using Java methods directly)

 

Required for command shell:

  • Proper authority
  • CREATE TABLE and CREATE FUNCTION in schema SYSTOOLS or DBADM on the target database

 

Configuring the Database

The preferred database is one with automatic storage, code set UTF-8, and page size 32 KB. As the DB2 instance owner, configure the database for UNIX and Windows systems as follows.

 

On UNIX

Execute <DB2INSTALL PATH>/json/bin/db2nosql.sh.

If running a remote command, execute the following:

 

db2nosql.sh -user <USER> -hostName <HOSTNAME> -port <PORT> -db <DBNAME> -password <PASSWORD>

 

If running a local command, execute this instead:

 

db2nosql.sh -db <DBNAME>

 

On Windows

Execute <DB2INSTALL PATH>/json/bin/db2nosql.bat.

 

If running a remote command, execute the following:

 

db2nosql.bat -user <USER> -hostName <HOSTNAME> -port <PORT> -db <DBNAME> -password <PASSWORD>

 

If running a local command, execute this instead:

 

db2nosql.bat -db <DBNAME>

 

When prompted enter

 

enable(true)

 

The following is an example of the output:

 

db2nosql.sh

JSON Command Shell Setup and Launcher.

This batch script assumes your JRE is 1.5 and higher. 1.6 will mask your

password.

Type db2nosql.sh -help to see options

Enter DB:db3a_row

IBM DB2 NoSQL JSON API 1.1.0.0 build 1.3.44

Licensed Materials - Property of IBM

(c) Copyright IBM Corp. 2013 All Rights Reserved.

nosql>Type your JSON query and hit <ENTER>

nosql>Type help() or help for usage information. All commands are case

sensitive.

Meta-data tables and functions for NoSQL JSON have not been created or have

been created incorrectly.

Please run 'enable(true)' to create them.

You must have the correct admin privileges.

Run 'enable(false)' to see the SQL that will be used.

nosql>nosql>enable(true)

nosql>Executing SQL...

Database artifacts created successfully.

 

Managing JSON

You can control several of JSON’s features by using the nosql.properties file. Although you can place this file anywhere on the application server, you must include its location in the shell’s CLASSPATH (see the IBM Knowledge Center for details). Table lists the NoSQL properties.

 

NoSQL Properties

Property

Definition

nosql.asyncMaxThreadCount

Max number of asynchronous threads

nosql.connectionPoolSize

JDBC connection pool size

nosql.tracefile

Trace file name

nosql.tracelevel

Level of trace output

nosql.statementCacheSize

Internal statement cache size

nosql.afg.includeID

Aggregation query adds _id field if no custom_id is specified

nosql.caseSensitiveNames

Schema/collection as case-sensitive

nosql.multiTypedID

Enabled support for multiple _id fields to be different types for documents in

the same collection

 

 

Learn more with the book DB2 10.1/10.5 for Linux, UNIX, and Windows Database Administration (Exams 611 and 311): Certification Study Guide.

 

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$