Skip to main content
This page guides you through a simple demonstration of how CockroachDB can store and query unstructured data from a third-party API, as well as how a can optimize your queries. Run this in your browser →

Step 1. Install prerequisites

Step 2. Start a single-node cluster

For the purpose of this tutorial, you need only one CockroachDB node running in insecure mode, so use the command in the foreground:

Step 3. Create a user

In a new terminal window, open the and create a new SQL user, maxroach:

Step 4. Create a database and grant privileges

Next, create a database called jsonb_test:
Set the database as the default:
Then to the maxroach user:

Step 5. Create a table

Still in the SQL shell, create a table called programming:

Step 6. Run the code

Now that you have a database, a SQL user, and a table, let’s run code to insert rows into the table. Download the json-sample.py file, or create the file yourself and copy the code into it:
In a new terminal window, navigate to your sample code file and run it:
The code queries the Reddit API for posts in /r/programming. The Reddit API only returns 25 results per page; however, each page returns an "after" string that tells you how to get the next page. Therefore, the program does the following in a loop:
  1. Makes a request to the API.
  2. Grabs the "after" string.
  3. Inserts the results into the table.
  4. Uses the new "after" string as the basis for the next request.
Download the json-sample.go file, or create the file yourself and copy the code into it:
In a new terminal window, navigate to your sample code file and run it:
The code queries the Reddit API for posts in /r/programming. The Reddit API only returns 25 results per page; however, each page returns an "after" string that tells you how to get the next page. Therefore, the program does the following in a loop:
  1. Makes a request to the API.
  2. Inserts the results into the table and grabs the "after" string.
  3. Uses the new "after" string as the basis for the next request.
The program will loop through that 40 times, but you can start querying the data right away.

Step 7. Query the data

Back in the terminal where the SQL shell is running, verify that rows of data are being inserted into your table:
You should see the count increasing. Keep checking until you see 1000 rows. Now, retrieve all the current entries where the link is pointing to somewhere on GitHub:
Since you are querying live data, your results for this and the following steps may vary from the results documented in this tutorial.

Step 8. Create a GIN index to optimize performance

The query in the previous step took 103.748ms. To optimize the performance of queries that filter on the JSONB column, let’s create a on the column:

Step 9. Run the query again

Now that there is a GIN index, the same query will run much faster:
Instead of 103.748ms, the query now takes 6.862ms.

Step 10. Clean up

If the program is still running, press ctrl-c to terminate it. Get the process ID of the node:
Then gracefully shut down the node, specifying its process ID:
If you do not plan to restart the cluster, remove the node’s data store:

What’s next?

Explore other CockroachDB benefits and features:
You may also want to learn more about the data type and .