dsq

repository·main·Indexed 26 days ago

https://github.com/multiprocessio/dsq

A command-line tool for running SQLite-dialect SQL queries against various data formats, including JSON, CSV, Excel, Parquet, ORC, Avro, YAML, and logfmt. It serves as a CLI companion to the DataStation GUI and allows users to join multiple files, use an interactive REPL, and convert supported file types to JSON.

Tokens
2.9K
Snippets
13
Records
26
Agent score
87%

What's inside dsq

  1. Understand the dsq engine and SQL capabilities

    main

    dsq uses DataStation as a library, which in turn uses SQLite to power SQL queries on arbitrary structured data.

    Because it relies on SQLite, dsq provides a well-tested and well-documented SQL engine. It also includes numerous useful functions on top of SQLite builtins, such as:

    • Best-effort date parsing
    • URL parsing and extraction
    • Statistics functions
  2. Join multiple files using index aliases

    main

    You can pass multiple files to dsq. Each file is treated as a table that can be accessed using the 0-based index {N} (e.g., {0}, {1}). You can also assign aliases to these tables using standard SQL syntax.

    # Joining CSV and JSON using indices
    $ dsq testdata/join/users.csv testdata/join/ages.json \
      "select {0}.name, {1}.age from {0} join {1} on {0}.id = {1}.id"
    
    # Joining using aliases
    $ dsq testdata/join/users.csv testdata/join/ages.json \
      "select u.name, a.age from {0} u join {1} a on u.id = a.id"
  3. Format output with Pretty Print or jq

    main

    By default, dsq outputs compact JSON. To get more readable output, you can either:

    1. Pipe the output to jq for pretty-printed JSON.
    2. Use the -p or --pretty flag to display results in an ASCII table.
  4. Pipe data to dsq

    main

    To pipe data into dsq, you must use the -s flag to specify the file extension or MIME type.

    Note: Piping data is not supported on Windows.

    $ cat testdata.csv | dsq -s csv "SELECT * FROM {} LIMIT 1"
    $ cat testdata.parquet | dsq -s parquet "SELECT COUNT(1) FROM {}"
  5. Enable caching for large datasets

    main

    Use the --cache or -C flag to store imported data on disk. dsq calculates a SHA1 sum of the input files; if the files haven't changed, subsequent queries will reuse the existing SQLite database instead of re-importing, making them significantly faster.

    You can also enable this permanently by setting the DSQ_CACHE=true environment variable.

    $ dsq some-large-file.json --cache 'SELECT COUNT(1) FROM {}'
  6. Install dsq on macOS, Linux, or WSL via binaries

    main

    To install the pre-compiled binaries on macOS, Linux, or WSL, you can use the following script to download, unzip, and move the binary to your /usr/local/bin directory. Note that this example uses version v0.23.0.

    $ VERSION="v0.23.0"
    $ FILE="dsq-$(uname -s | awk '{ print tolower($0) }')-x64-$VERSION.zip"
    $ curl -LO "https://github.com/multiprocessio/dsq/releases/download/$VERSION/$FILE"
    $ unzip $FILE
    $ sudo mv ./dsq /usr/local/bin/dsq