Enabling Db2 JSON

To enable JSON functionality, you must configure the Db2 database and the database server.

Before you begin

  • Update the PATH system variable for Java™.
  • Add the db2jcc.jar file to the CLASSPATH environment variable or the -cp command-line switch.
  • If you do not have a UTF-8 database with a 32 KB page size that you can use, create one. For example, to create a database jsondb with automatic storage and territory us, enter the following command:
    db2 create database jsondb automatic storage yes using codeset utf-8 territory us pagesize 32 K
    Make a note of the database server host name, IP address, port number, and database name. This information is required for client configuration.
  • Although not recommended, if you are using a database with less than 32 K pagesize, manually create a temporary table space with 32 K pagesize. For example, enter the following command:
    CREATE BUFFERPOOL NOSQLSYSTOOLSBP ALL DBPARTITIONNUMS SIZE AUTOMATIC PAGESIZE 32K
    CREATE TEMPORARY TABLESPACE NOSQLSYSTOOLSTEMP PAGESIZE 32K MANAGED BY AUTOMATIC STORAGE BUFFERPOOL NOSQLSYSTOOLSBP
  • Ensure that you have proper authority to create tables and functions that are required to enable NoSQL JSON functionality. You require the following privileges:
    • CREATETAB authority and CREATE FUNCTION for the SYSTOOLS schema
    • DBADM authority for the target database
  • Ensure that the dft_table_org database configuration parameter is set to ROW.
  • Ensure that you have the database server host name, IP address, port number, and database name handy from the server configuration.

Procedure

To configure a Db2 database for JSON functionality:

  1. To start the NoSQL command line interface, start by choosing from the scripts in the sqllib\json\bin folder:
    • On Windows, use db2nosql.bat
    • On Linux® and UNIX, use db2nosql.sh
    The script defaults to localhost:50000 for the database, unless you specify otherwise by using the -hostName and -port options.
  2. Depending on your Db2 Version 11.1 Mod Pack and Fix Pack enabling JSON capability will vary. Choose the option for your version below.
    • Option A: For Db2 version 11.1 Mod Pack 2 and Fix Pack 2 and higher:

      Start the NoSQL command line interface and create the tables and functions for the database to enable Db2 JSON capability by running the script appropriate for your machine with the -setup enable option.

      If you get following error:
      Error code: -551
      DB2 SQL Error: SQLCODE=-551, SQLSTATE=42501, SQLERRMC=WEIWANG;REPLACE FUNCTION;SYSTOOLS.JSON_TABLE, DRIVER=3.72.24
      CDJSN1156E Failed to execute function 'enable'
      You can fix the error by executing the script again to disable and then re-enable JSON capability. The example below uses the db2nosql.sh script for Linux and UNIX:
      ./db2nosql.sh -setup disable
      ./db2nosql.sh -setup enable
    • Option B: For Db2 versions lower than 11.1 MP2 FP2:

      Start the NoSQL command line interface by running the script appropriate for your machine. On the command line, create the tables and functions for the database to enable Db2 JSON capability by typing enable(true).

      If you get following error:
      enable(true)
      
      Error code: -551
      DB2 SQL Error: SQLCODE=-551, SQLSTATE=42501, SQLERRMC=WEIWANG;REPLACE FUNCTION;SYSTOOLS.JSON_TABLE, DRIVER=3.72.24
      CDJSN1156E Failed to execute function 'enable'
      You can fix the error by executing:
      disable(true);
      enable(true);
  3. To exit the command-line interface, type quit.

Example

  • The following sample script shows the commands to enable Db2 JSON for Db2 version 11.1 Mod Pack 2 and Fix Pack 2 and higher:
    ./db2nosql.sh -user myuser -hostName myhost -port 50000 -db jsondb -password mypasswd -setup enable
    
    Database artifacts created successfully
    
    nosql>quit
  • The following sample script shows the commands to enable Db2 JSON for Db2 versions lower than 11.1 MP2 FP2:
    ./db2nosql.sh -user myuser -hostName myhost -port 50000 -db jsondb -password mypasswd
    
    nosql>enable(true)
    
    Database artifacts created successfully
    
    nosql>quit