sq/README.md

372 lines
11 KiB
Markdown
Raw Normal View History

2023-04-22 06:28:07 +03:00
[//]: # ([![Go Coverage](https://github.com/neilotoole/sq/wiki/coverage.svg)](https://raw.githack.com/wiki/neilotoole/sq/coverage.html))
2023-04-22 06:27:29 +03:00
[![Go Reference](https://pkg.go.dev/badge/github.com/neilotoole/sq.svg)](https://pkg.go.dev/github.com/neilotoole/sq)
2023-04-22 06:31:59 +03:00
![Main pipeline](https://github.com/neilotoole/sq/actions/workflows/main.yml/badge.svg)
2023-04-22 06:29:20 +03:00
2023-03-12 06:25:19 +03:00
# sq: data wrangler
2016-10-17 07:14:01 +03:00
2021-01-02 09:31:52 +03:00
`sq` is a command line tool that provides `jq`-style access to
structured data sources: SQL databases, or document formats like CSV or Excel.
2021-01-02 09:31:30 +03:00
2023-03-16 05:33:45 +03:00
![sq](./splash.png)
2021-01-04 07:44:09 +03:00
`sq` can perform cross-source joins,
execute database-native SQL, and output to a multitude of formats including JSON,
2021-01-04 07:47:13 +03:00
Excel, CSV, HTML, Markdown and XML, or insert directly to a SQL database.
2021-01-04 07:56:50 +03:00
`sq` can also inspect sources to view metadata about the source structure (tables,
2020-08-19 23:46:04 +03:00
columns, size) and has commands for common database operations such as copying
or dropping tables.
2023-03-12 06:25:19 +03:00
Find out more at [sq.io](https://sq.io).
2016-10-17 07:14:01 +03:00
2023-03-12 06:25:19 +03:00
## Install
2021-01-04 07:46:25 +03:00
2021-01-04 03:40:32 +03:00
### macOS
2016-10-17 07:14:01 +03:00
2022-11-20 09:43:18 +03:00
```shell
2021-03-07 21:12:10 +03:00
brew install neilotoole/sq/sq
2021-01-04 03:40:32 +03:00
```
### Linux
2022-11-20 09:43:18 +03:00
```shell
/bin/sh -c "$(curl -fsSL https://sq.io/install.sh)"
2020-08-06 20:58:47 +03:00
```
2016-10-17 07:14:01 +03:00
2023-03-12 06:25:19 +03:00
### Windows
2022-11-20 09:43:18 +03:00
2023-03-12 06:25:19 +03:00
```shell
scoop bucket add sq https://github.com/neilotoole/sq
scoop install sq
```
2021-01-04 03:40:32 +03:00
2023-03-12 06:25:19 +03:00
### Go
2023-03-12 06:25:19 +03:00
```shell
go install github.com/neilotoole/sq
```
2023-03-12 06:25:19 +03:00
See other [install options](https://sq.io/docs/install/).
2021-01-04 07:46:25 +03:00
2021-01-04 03:40:32 +03:00
## Quickstart
2023-03-12 06:25:19 +03:00
Use `sq help` to see command help. Docs are over at [sq.io](https://sq.io).
Read the [overview](https://sq.io/docs/overview/), and the
[tutorial](https://sq.io/docs/tutorial/). The [cookbook](https://sq.io/docs/cookbook/) has
recipes for common tasks.
2021-01-04 03:40:32 +03:00
The major concept is: `sq` operates on data sources, which are treated as SQL databases (even if the
2023-03-12 06:25:19 +03:00
source is really a CSV or XLSX file etc.).
2021-01-04 03:40:32 +03:00
In a nutshell, you `sq add` a source (giving it a `handle`), and then execute commands against the
source.
2021-01-04 03:40:32 +03:00
### Sources
Initially there are no sources.
```shell
2021-01-04 03:40:32 +03:00
$ sq ls
```
2021-01-04 07:44:09 +03:00
Let's add a source. First we'll add a SQLite database, but this could also be Postgres,
2021-01-04 09:36:15 +03:00
SQL Server, Excel, etc. Download the sample DB, and `sq add` the source. We
2021-01-04 09:16:45 +03:00
use `-h` to specify a _handle_ to use.
2021-01-04 03:40:32 +03:00
```shell
2021-01-04 03:40:32 +03:00
$ wget https://sq.io/testdata/sakila.db
2021-01-04 07:44:09 +03:00
$ sq add ./sakila.db -h @sakila_sl3
@sakila_sl3 sqlite3 sakila.db
2021-01-04 03:40:32 +03:00
$ sq ls -v
2021-01-04 07:56:50 +03:00
HANDLE DRIVER LOCATION OPTIONS
@sakila_sl3* sqlite3 sqlite3:/root/sakila.db
2021-01-04 03:40:32 +03:00
2021-01-04 07:44:09 +03:00
$ sq ping @sakila_sl3
@sakila_sl3 1ms pong
2021-01-04 03:40:32 +03:00
$ sq src
2021-01-04 07:44:09 +03:00
@sakila_sl3 sqlite3 sakila.db
2021-01-04 03:40:32 +03:00
```
2021-01-04 07:44:09 +03:00
The `sq ping` command simply pings the source to verify that it's available.
2021-01-04 03:40:32 +03:00
`sq src` lists the _active source_, which in our case is `@sakila_sl3`.
You can change the active source using `sq src @other_src`.
When there's an active source specified, you can usually omit the handle from `sq` commands.
Thus you could instead do:
2021-01-04 03:40:32 +03:00
```shell
2021-01-04 03:40:32 +03:00
$ sq ping
2021-01-04 07:44:09 +03:00
@sakila_sl3 1ms pong
2021-01-04 03:40:32 +03:00
```
### Query
2021-01-04 08:09:29 +03:00
Fundamentally, `sq` is for querying data. Using our jq-style syntax:
2021-01-04 03:40:32 +03:00
```shell
2021-01-04 03:40:32 +03:00
$ sq '.actor | .actor_id < 100 | .[0:3]'
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2020-02-15T06:59:28Z
2 NICK WAHLBERG 2020-02-15T06:59:28Z
3 ED CHASE 2020-02-15T06:59:28Z
```
The above query selected some rows from the `actor` table. You could also
use native SQL, e.g.:
2021-01-04 03:40:32 +03:00
```shell
2021-01-04 03:40:32 +03:00
$ sq sql 'SELECT * FROM actor WHERE actor_id < 100 LIMIT 3'
actor_id first_name last_name last_update
1 PENELOPE GUINESS 2020-02-15T06:59:28Z
2 NICK WAHLBERG 2020-02-15T06:59:28Z
3 ED CHASE 2020-02-15T06:59:28Z
```
But we're flying a bit blind here: how did we know about the `actor` table?
### Inspect
`sq inspect` is your friend (output abbreviated):
```shell
HANDLE DRIVER NAME FQ NAME SIZE TABLES LOCATION
@sakila_sl3 sqlite3 sakila.db sakila.db/main 5.6MB 21 sqlite3:/Users/neilotoole/work/sq/sq/drivers/sqlite3/testdata/sakila.db
TABLE ROWS COL NAMES
actor 200 actor_id, first_name, last_name, last_update
address 603 address_id, address, address2, district, city_id, postal_code, phone, last_update
category 16 category_id, name, last_update
2021-01-04 03:40:32 +03:00
```
2023-04-19 03:53:55 +03:00
Use the `--verbose` (`-v`) flag to see more detail. And use `--json` (`-j`) to output in JSON (output abbreviated):
2021-01-04 03:40:32 +03:00
2021-01-04 08:11:30 +03:00
```shell
$ sq inspect -j
2021-01-04 03:40:32 +03:00
{
2021-01-04 07:44:09 +03:00
"handle": "@sakila_sl3",
2021-01-04 03:40:32 +03:00
"name": "sakila.db",
"driver": "sqlite3",
"db_version": "3.31.1",
"location": "sqlite3:///root/sakila.db",
"size": 5828608,
"tables": [
{
"name": "actor",
"table_type": "table",
"row_count": 200,
"columns": [
{
"name": "actor_id",
"position": 0,
"primary_key": true,
"base_type": "numeric",
"column_type": "numeric",
"kind": "decimal",
"nullable": false
}
```
Combine `sq inspect` with [jq](https://stedolan.github.io/jq/) for some useful capabilities. Here's
2023-03-12 06:25:19 +03:00
how to [list](https://sq-web.netlify.app/docs/cookbook/#list-table-names)
all the table names in the active source:
2021-01-04 03:40:32 +03:00
```shell
2021-01-04 03:40:32 +03:00
$ sq inspect -j | jq -r '.tables[] | .name'
actor
address
category
city
country
customer
[...]
```
And here's how you
2023-03-15 08:40:18 +03:00
could [export](https://sq.io/docs/cookbook/#export-all-table-data-to-csv) each table
to a CSV file:
2021-01-04 03:40:32 +03:00
```shell
2021-01-04 03:40:32 +03:00
$ sq inspect -j | jq -r '.tables[] | .name' | xargs -I % sq .% --csv --output %.csv
$ ls
actor.csv city.csv customer_list.csv film_category.csv inventory.csv rental.csv staff.csv
address.csv country.csv film.csv film_list.csv language.csv sales_by_film_category.csv staff_list.csv
category.csv customer.csv film_actor.csv film_text.csv payment.csv sales_by_store.csv store.csv
```
Note that you can also inspect an individual table:
```shell
$ sq inspect -v @sakila_sl3.actor
2021-01-04 03:40:32 +03:00
TABLE ROWS TYPE SIZE NUM COLS COL NAMES COL TYPES
actor 200 table - 4 actor_id, first_name, last_name, last_update numeric, VARCHAR(45), VARCHAR(45), TIMESTAMP
```
2021-01-04 08:07:12 +03:00
### Insert Output Into Database Source
2021-01-04 07:41:36 +03:00
`sq` query results can be output in various formats (JSON, XML, CSV, etc), and can also be "
outputted" as an *insert* into database sources.
2021-01-04 07:41:36 +03:00
That is, you can use `sq` to insert results from a Postgres query into a MySQL table, or copy an
Excel worksheet into a SQLite table, or a push a CSV file into a SQL Server table etc.
2021-01-04 07:41:36 +03:00
> **Note:** If you want to copy a table inside the same (database) source, use `sq tbl copy`
> instead, which uses the database's native table copy functionality.
2021-01-04 07:41:36 +03:00
For this example, we'll insert an Excel worksheet into our `@sakila_sl3` SQLite database. First, we
download the XLSX file, and `sq add` it as a source.
2021-01-04 07:41:36 +03:00
```shell
2021-01-04 07:41:36 +03:00
$ wget https://sq.io/testdata/xl_demo.xlsx
$ sq add ./xl_demo.xlsx --opts header=true
@xl_demo_xlsx xlsx xl_demo.xlsx
$ sq @xl_demo_xlsx.person
uid username email address_id
1 neilotoole neilotoole@apache.org 1
2 ksoze kaiser@soze.org 2
3 kubla kubla@khan.mn NULL
[...]
```
Now, execute the same query, but this time `sq` inserts the results into a new table (`person`)
in `@sakila_sl3`:
2021-01-04 07:41:36 +03:00
2021-01-04 07:44:09 +03:00
```shell
$ sq @xl_demo_xlsx.person --insert @sakila_sl3.person
Inserted 7 rows into @sakila_sl3.person
2021-01-04 07:41:36 +03:00
$ sq inspect -v @sakila_sl3.person
2021-01-04 07:44:09 +03:00
TABLE ROWS TYPE SIZE NUM COLS COL NAMES COL TYPES
person 7 table - 4 uid, username, email, address_id INTEGER, TEXT, TEXT, INTEGER
2021-01-04 07:41:36 +03:00
2021-01-04 07:44:09 +03:00
$ sq @sakila_sl3.person
uid username email address_id
1 neilotoole neilotoole@apache.org 1
2 ksoze kaiser@soze.org 2
3 kubla kubla@khan.mn NULL
[...]
```
2021-01-04 07:41:36 +03:00
### Cross-Source Join
2021-01-04 03:40:32 +03:00
`sq` has rudimentary support for cross-source joins. That is, you can join an Excel worksheet with a
CSV file, or Postgres table, etc.
2021-01-04 03:40:32 +03:00
> **Note:** The current mechanism for these joins is highly naive: `sq` copies the joined table from
> each source to a "scratch database" (SQLite by default), and then performs the JOIN using the
2023-03-12 06:25:19 +03:00
> scratch database's SQL interface. Thus, performance is abysmal for larger tables. There are
> massive
> optimizations to be made, but none have been implemented yet.
2021-01-04 03:40:32 +03:00
2023-03-12 06:25:19 +03:00
See the [tutorial](https://sq.io/docs/tutorial/#join) for further details, but
given an Excel source `@xl_demo` and a CSV source `@csv_demo`, you can do:
2021-01-04 03:40:32 +03:00
```shell
2021-01-04 03:40:32 +03:00
$ sq '@csv_demo.data, @xl_demo.address | join(.D == .address_id) | .C, .city'
C city
neilotoole@apache.org Washington
kaiser@soze.org Ulan Bator
nikola@tesla.rs Washington
augustus@caesar.org Ulan Bator
plato@athens.gr Washington
```
2021-01-04 07:44:09 +03:00
### Table Commands
2021-01-04 03:40:32 +03:00
`sq` provides several handy commands for working with tables. Note that these commands work directly
against SQL database sources, using their native SQL commands.
2021-01-04 03:40:32 +03:00
```shell
2021-01-04 03:40:32 +03:00
$ sq tbl copy .actor .actor_copy
2021-01-04 07:44:09 +03:00
Copied table: @sakila_sl3.actor --> @sakila_sl3.actor_copy (200 rows copied)
2021-01-04 03:40:32 +03:00
$ sq tbl truncate .actor_copy
2021-01-04 07:44:09 +03:00
Truncated 200 rows from @sakila_sl3.actor_copy
2021-01-04 03:40:32 +03:00
$ sq tbl drop .actor_copy
2021-01-04 07:44:09 +03:00
Dropped table @sakila_sl3.actor_copy
2016-10-17 07:14:01 +03:00
```
2016-10-21 19:14:48 +03:00
2021-01-04 03:40:32 +03:00
### UNIX Pipes
For file-based sources (such as CSV or XLSX), you can `sq add` the source file, but you can also
pipe it:
2021-01-04 03:40:32 +03:00
2021-01-04 10:39:15 +03:00
```shell
2021-01-04 10:39:43 +03:00
$ cat ./example.xlsx | sq .Sheet1
2021-01-04 10:39:15 +03:00
```
2021-01-04 10:40:05 +03:00
Similarly, you can inspect:
2021-01-04 10:39:15 +03:00
```shell
2021-01-04 10:39:43 +03:00
$ cat ./example.xlsx | sq inspect
2021-01-04 10:39:15 +03:00
```
2021-01-04 03:40:32 +03:00
## Data Source Drivers
`sq` knows how to deal with a data source type via a _driver_ implementation. To view the
installed/supported drivers:
```shell
$ sq driver ls
DRIVER DESCRIPTION
sqlite3 SQLite
postgres PostgreSQL
sqlserver Microsoft SQL Server / Azure SQL Edge
mysql MySQL
csv Comma-Separated Values
tsv Tab-Separated Values
json JSON
jsona JSON Array: LF-delimited JSON arrays
jsonl JSON Lines: LF-delimited JSON objects
xlsx Microsoft Excel XLSX
```
2021-01-04 07:44:09 +03:00
2021-01-04 03:40:32 +03:00
## Output Formats
2021-01-04 10:40:51 +03:00
`sq` has many output formats:
2021-01-04 03:40:32 +03:00
2021-01-04 15:07:44 +03:00
- `--table`: Text/Table
2021-01-04 03:40:32 +03:00
- `--json`: JSON
- `--jsona`: JSON Array
- `--jsonl`: JSON Lines
- `--csv` / `--tsv` : CSV / TSV
- `--xlsx`: XLSX (Microsoft Excel)
- `--html`: HTML
- `--xml`: XML
- `--markdown`: Markdown
- `--raw`: Raw (bytes)
2021-01-04 07:44:09 +03:00
## Changelog
See [CHANGELOG.md](./CHANGELOG.md).
2016-10-21 19:14:48 +03:00
2020-08-06 20:58:47 +03:00
## Acknowledgements
2016-10-21 19:14:48 +03:00
2023-03-12 06:25:19 +03:00
- Thanks to [Diego Souza](https://github.com/diegosouza) for creating
the [Arch Linux package](https://aur.archlinux.org/packages/sq-bin).
- Much inspiration is owed to [jq](https://stedolan.github.io/jq/).
- See [`go.mod`](https://github.com/neilotoole/sq/blob/master/go.mod) for a list of third-party
packages.
- Additionally, `sq` incorporates modified versions of:
2023-03-12 06:25:19 +03:00
- [`olekukonko/tablewriter`](https://github.com/olekukonko/tablewriter)
- [`segmentio/encoding`](https://github.com/segmentio/encoding) for JSON encoding.
- The [_Sakila_](https://dev.mysql.com/doc/sakila/en/) example databases were lifted
from [jOOQ](https://github.com/jooq/jooq), which in turn owe their heritage to earlier work on
Sakila.
2021-01-04 03:40:32 +03:00
## Similar / Related / Noteworthy Projects
- [usql](https://github.com/xo/usql)
- [textql](https://github.com/dinedal/textql)
- [golang-migrate](https://github.com/golang-migrate/migrate)
- [octosql](https://github.com/cube2222/octosql)
- [rq](https://github.com/dflemstr/rq)