Query data on the command line with SQL-like SELECTs powered by Python expressions

dcmoura dcmoura Last update: Mar 27, 2024

SPyQL

SQL with Python in the middle

https://pypi.python.org/pypi/spyql

https://spyql.readthedocs.io/en/latest/?version=latest

codecov

downloads

code style: black

license: MIT

About

SPyQL is a query language that combines:

  • the simplicity and structure of SQL;
  • with the power and readability of Python.

SQL provides the structure of the query, while Python is used to define expressions, bringing along a vast ecosystem of packages.

SPyQL is fast and memory efficient. Take a look at the benchmarks with GB-size JSON data.

SPyQL CLI

SPyQL offers a command-line interface that allows running SPyQL queries on top of text data (e.g. CSV, JSON). Data can come from files but also from data streams, such as as Kafka, or from databases such as PostgreSQL. Basically, data can come from any command that outputs text :-). More, data can be generated by a Python expression! And since SPyQL also writes to different formats, it allows to easily convert between data formats.

Take a look at the Command line examples to see how to query parquet, process API calls, transverse directories of zipped JSONs, convert CSV to JSON, and import JSON/CSV data into SQL databases, among many other things.

See also:

SPyQL Module

SPyQL is also available as a Python module. In addition to the CLI features, you can also:

  • query variables (e.g. lists of dicts);
  • get results into in-memory data structures.

Principles

We aim for SPyQL to be:

  • Simple: simple to use with a straightforward implementation;
  • Familiar: you should feel at home if you are acquainted with SQL and Python;
  • Light: small memory footprint that allows you to process large data that fit into your machine;
  • Useful: it should make your life easier, filling a gap in the eco-system.

Distinctive features of SPyQL

  • Row order guarantee
  • Natural window for aggregations
  • No distinction between aggregate and window functions
  • IMPORT clause
  • Natural support for lists, sets, dictionaries, objects, etc
  • 1-liner by design
  • Multiple data formats supported

Testimonials

"I'm very impressed - this is some very neat pragmatic software design."

Simon Willison, Creator of Datasette, co-creator of Django

"I love this tool! I use it every day"...

Alin Panaitiu, Creator of Lunar

"Brilliant tool, thanks a lot for creating it and for the example here!"

Greg Sadetsky, Co-founder and CTO at Decibel Ads

Documentation

The official documentation of SPyQL can be found at: https://spyql.readthedocs.io/.

Installation

The easiest way to install SPyQL is from pip:

Hello world

To test your installation run in the terminal:

Output:

Message
-----------
Hello world

You can try replacing the output format by JSON or CSV, and adding more columns. e.g. run in the terminal:

Output:

Example queries

You can run the following example queries in the terminal: spyql "the_query" < a_data_file

Example data files are not provided on most cases.

Query a CSV (and print a pretty table)

Convert CSV to a flat JSON

Convert from CSV to a hierarchical JSON

or

JSON to CSV, filtering out NULLs

Explode JSON to CSV

Sample input:

Output:

id, name, price
1028, tomatoes, 1.5
1028, bananas, 2.0
1029, peaches, 3.12

Python iterator/list/comprehension to JSON

or

Importing python modules

Here we import hashlib to calculate a md5 hash for each input line. Before running this example you need to install the hashlib package (pip install hashlib).

Getting the top 5 records

Aggregations

Totals by player, alphabetically ordered.

Partial aggregations

Calculating the cumulative sum of a variable using the PARTIALS modifier. Also demoing the lag aggregator.

Sample input:

Output:

If PARTIALS was omitted the result would be equivalent to the last output row.

Distinct rows

Command line examples

To run the following examples, type Ctrl-x Ctrl-e on you terminal. This will open your default editor (emacs/vim). Paste the code of one of the examples, save and exit.

Queries on Parquet with directories

Here, find transverses a directory and executes parquet-tools for each parquet file, dumping each file to json format. jq -c makes sure that the output has 1 json per line before handing over to spyql. This is far from being an efficient way to query parquet files, but it might be a handy option if you need to do a quick inspection.

Querying multiple json.gz files

Querying YAML / XML / TOML files

yq converts yaml, xml and toml files to json, allowing to easily query any of these with spyql.

Kafka to PostegreSQL pipeline

Read data from a kafka topic and write to postgres table name customer.

Monitoring statistics in Kafka

Read data from a kafka topic, continuously calculating statistics.

Sub-queries (piping)

A special file format (spy) is used to efficiently pipe data between queries.

(Equi) Joins

It is possible to make simple (LEFT) JOIN operations based on dictionary lookups.

Given `numbers.json`:

Query:

Output:

If you want a INNER JOIN instead of a LEFT JOIN, you can add a criteria to the where clause, e.g.:

Output:

Queries over APIs

Plotting to the terminal

Plotting with matplotcli

matplotcli demo


This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Subscribe to our newsletter