Setup
Let’s first create a virtual environment:ipython to run the commands in the rest of the guide, which you can launch by running:
Querying a JSON file in S3
Let’s now have a look at how to query a JSON file that’s stored in an S3 bucket. The YouTube dislikes dataset contains more than 4 billion rows of dislikes on YouTube videos up to 2021. We’re going to work with one of the JSON files from that dataset. Import chdb:Configuring the output format
The default output format isCSV, but we can change that via the output_format parameter.
chDB supports the ClickHouse data formats, as well as some of its own, including DataFrame, which returns a Pandas DataFrame:
Creating a table from JSON file
Next, let’s have a look at how to create a table in chDB. We need to use a different API to do that, so let’s first import that:chDB runs a single embedded engine per process, so the database directory (its path) follows a few rules:
- One process per path: a chDB data directory can be opened by only one operating system process at a time. A second process that opens the same path fails until the first one closes.
- One path per process: while a session or connection is open, opening another one for a different path fails; first close the open one(s) to avoid the error. Multiple sessions or connections on the same path can coexist.
- Concurrency: sessions or connections on the same path can run queries concurrently.
- Path-less sessions are shared: with no path, sessions use the process-wide
:memory:database (so blank sessions see each other’s tables), and its temporary directory is removed only when the last one closes.
dislikes table based on the schema from the JSON file, using the CREATE...EMPTY AS technique.
We’ll use the schema_inference_make_columns_nullable setting so that column types aren’t all made Nullable.
DESCRIBE clause to inspect the schema:
CREATE...AS technique.
Let’s create a different table using that technique: