Setup
Let’s first create a virtual environment:If you’re using Jupyter Lab, you’ll need to create a notebook before following the rest of the guide.
Downloading a dataset
We’re going to use the New York City taxi dataset, which contains around 3 million taxi rides along with the fare, tip, and pickup neighborhood of each one. The trips are split across several TSV files, so let’s start by downloading those:Configuring chDB and JupySQL
Next, let’s import thedbapi module for chDB:
taxi.chdb directory:
sql magic and create a connection to chDB:
Querying data in TSV files
We’ve downloaded a bunch of files with thetrips_ prefix.
Let’s use the DESCRIBE clause to understand the schema:
SELECT query directly against these files to see what the data looks like:
trip_distance, fare_amount, and tip_amount — were inferred as String rather than a numeric type.
We’ll clean those up when we import the data into a table.
Importing TSV files into chDB
Now we’re going to store the data from these TSV files in a table. The default database doesn’t persist data on disk, so we need to create another database first:trips whose schema will be derived from the structure of the data in the TSV files.
We’ll use the REPLACE clause to cast the money-related columns to Float64, and the transform function to turn the numeric pickup_borocode column into a human-readable borough name:
zones based on the content of the CSV file:
Querying chDB
Data ingestion is done, now it’s time for the fun part - querying the data! Each borough is divided into a different number of taxi zones. We’re going to write a query that joins the two tables to find out how many trips were picked up in each borough, and how many trips that works out to per taxi zone:Saving queries
We can save queries using the--save parameter on the same line as the %%sql magic.
The --no-execute parameter means that query execution will be skipped.
Querying with parameters
We can also use parameters in our queries. Parameters are just normal variables:{{variable}} syntax in our query.
The following query finds the neighborhoods with the highest average tip among those with more than 10,000 trips: