Querying data in a Jupyter Notebook

David G. Simmons

David G. Simmons

QuestDB Team
A software developer typing code on a laptop
Photo by Glenn Carstens-Peters via Unsplash

This tutorial shows how to use QuestDB with a Jupyter notebook. We will learn how to create a database, query data and read table contents into a Pandas dataframe, and use matplotlib to create charts of our table data.

Getting started#

To get started, you'll need a few things installed and set up. This should be quick.

QuestDB: To download QuestDB, you can find complete instructions for getting started with Docker, Homebrew on macOS, or the binaries.

Jupyter Notebooks: These are interactive Python environments that will help you run a complete version of this tutorial. To run it, you should:

  • make sure you are running Python 3.x and not Python 2.7. If you're in doubt, python --version will tell you.
  • install Jupyter Notebooks with pip3 install --upgrade ipython jupyter make sure that the libraries we use in this tutorial are also installed with pip3 install requests urlib matplotlib pandas
  • clone this repository (git clone https://github.com/davidgs/QuestNotebook) in the repository directory run Jupyter Notebook

That will get you right back to a page that looks eerily similar to this that is interactive, allowing you to run the code and interact with the database yourself.

If you get errors like ModuleNotFoundError: No module named 'requests' for any of the libraries you installed above, double-check to make sure that you are actually using Python 3.x jupytper --path will let you know if Jupyter is using 2.7 or 3.x

Create a database#

We will need someplace to store our data, so let's create a test database where we can put some random data. We will create a simple table with 5 columns, one of which is a timestamp.

The create operation in QuestDB appends records to the bottom of a table. If the table has a designated new record, timestamps must be greater than, or equal to the latest timestamp. Attempts to add a timestamp in middle of a table will result in a timestamp out of order error.

  • cust_id is the customer identifier. It uniquely identifies a customer.

  • balance_ccy balance currency. We use char in this example, but we could use SYMBOL to avoid storing text against each record to save space and increase database performance.

  • balance is the current balance for customer and currency tuple.

  • inactive is used to flag deleted records.

  • timestamp timestamp in microseconds of the record. Note that if you receive the timestamp data as a string, it could also be inserted using to_timestamp.

This should return a 200 status the first time you run it. If you run it more than once, subsequent runs will return 400 because the database already exists.

import requests import urllib.parse as par
q = 'create table balances'\
'(cust_id int,'\
' balance_ccy char,'\
'balance double,'\
'inactive boolean,'\
'timestamp timestamp)'\
r = requests.get("http://localhost:9000/exec?query=" + q)

Generate some data#

Since we have a new setup, we should add some data to QuestDB so that we can have something to query. We will add some random data, for now. You can re-run this section as many times as you want to add 100 entries at a time, or simply change the range(100) to add as many datapoints as you wish.

import requests
import random
from datetime import datetime
success = 0
fail = 0
currency = ["$", "€", "£", "¥"]
for x in range(1000):
cust = random.randint(20, 42)
cur = random.choice(currency)
bal = round(random.uniform(10.45, 235.15), 2)
act = bool(random.getrandbits(1))
query = "insert into balances values("\
+ str(cust) + ",'"\
+ cur + "'," \
+ str(bal) + "," \
+ str(act) + ",systimestamp())"
r = requests.get("http://localhost:9000/exec?query=" + query)
if r.status_code == 200:
success += 1
fail += 1
print("Rows inserted: " + str(success))
if fail > 0:
print("Rows Failed: " + str(fail))

Query data from QuestDB#

Now that we have data available, let's try querying some of it to see what we get back!

import requests
import io
r = requests.get("http://localhost:9000/exp?query=select * from balances")
rawData = r.text

Read the content into pandas dataframe#

So you'll notice that the returned data is just a massive CSV string. If you'd rather have JSON data, then you would change the endpoint to http://localhost:9000/exec, but since we're going to use Pandas to frame our data, we'll stick with CSV.

We are also telling pandas to parse the timestamp field as a date. This is important since we're dealing with time series data.

import pandas as pd
pData = pd.read_csv(io.StringIO(rawData), parse_dates=['timestamp'])

Narrow the search#

That's just getting us all the data, but let's narrow the search using some SQL clauses. Let's look for a specific cust_id and only balances of that customer that are in USD.

We are also only interested in times the customer was active. Since this is SQL, you can make this query as simple, or as complex, as you'd like. All of the data was generated randomly, so this exact query may return no results, you may have to adjust the cust_id below until you get results back.

Note: The query string must be URL-encoded before it is sent.

import urllib.parse
q = "select cust_id,"\
" balance,"\
" balance_ccy,"\
" inactive,"\
" timestamp"\
" from balances"\
" where cust_id = 26"\
" and balance_ccy = '$'"\
" and not inactive"
query = urllib.parse.quote(q)
r = requests.get("http://localhost:9000/exp?query=" + query)
queryData = r.text
rawData = pd.read_csv(io.StringIO(queryData), parse_dates=['timestamp'])

Plot the data#

We will use matplotlib to plot the data

from matplotlib import pyplot as plt
rawData.plot("timestamp", ["balance"], subplots=True)

From that query we should get a nice little plot of our data, like this:

Graph of the balance from the query

Clean up#

Now we will clean everything up for the next time.

r = requests.get("http://localhost:9000/exec?query=drop table balances")
if r.status_code == 200:
print("Database Table dropped")
print("Database Table not Dropped: " + str(r.status_code))

You can now stop your QuestDB instance, if you'd like, or leave it running and find some great uses for it! If you enjoyed this tutorial, please follow us on Twitter, and make sure you give our GitHub repository a star!

An icon showing wave propagation

Join our developer community

QuestDB is open source. Follow us on Twitter, star our GitHub repo, and join our developer community on Slack!

An icon showing a paper plane

Subscribe to our newsletter

Stay up to date with all things QuestDB