Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add write and query CLI sub-commands #24671

Merged
merged 17 commits into from
Feb 20, 2024
Merged

Conversation

hiltontj
Copy link
Contributor

@hiltontj hiltontj commented Feb 16, 2024

This builds on #24665 and is meant to close #24651

Two new sub-commands are added to the influxdb3 CLI:

  • query: for performing queries against the running server
  • write: for performing writes to the running server

The generated help documentation summarizes each below.

query

Perform a query against a running InfluxDB 3.0 server

Usage: influxdb3 query [OPTIONS] --dbname <DATABASE_NAME> [QUERY]...

Arguments:
  [QUERY]...
          The query string to execute

Options:
  -h, --host <HOST_URL>
          The host URL of the running InfluxDB 3.0 server
          
          [env: INFLUXDB3_HOST_URL=]
          [default: http://127.0.0.1:8181]

  -d, --dbname <DATABASE_NAME>
          The database name to run the query against
          
          [env: INFLUXDB3_DATABASE_NAME=]

      --token <AUTH_TOKEN>
          The token for authentication with the InfluxDB 3.0 server
          
          [env: INFLUXDB3_AUTH_TOKEN=]

      --help
          Print help information

  -l, --lang <LANGUAGE>
          The query language used to format the provided query string
          
          [default: sql]
          [possible values: sql]

      --fmt <OUTPUT_FORMAT>
          The format in which to output the query
          
          If `--fmt` is set to `parquet`, then you must also specify an output file path with `--output`.
          
          [default: pretty]
          [possible values: pretty, json, csv, parquet]

  -o, --output <OUTPUT_FILE_PATH>
          Put all query output into `output`

write

Perform a set of writes to a running InfluxDB 3.0 server

Usage: influxdb3 write [OPTIONS] --dbname <DATABASE_NAME> --file <FILE_PATH>

Options:
  -h, --host <HOST_URL>
          The host URL of the running InfluxDB 3.0 server
          
          [env: INFLUXDB3_HOST_URL=]
          [default: http://127.0.0.1:8181]

  -d, --dbname <DATABASE_NAME>
          The database name to run the query against
          
          [env: INFLUXDB3_DATABASE_NAME=]

      --token <AUTH_TOKEN>
          The token for authentication with the InfluxDB 3.0 server
          
          [env: INFLUXDB3_AUTH_TOKEN=]

  -f, --file <FILE_PATH>
          File path to load the write data from
          
          Currently, only files containing line protocol are supported.

      --help
          Print help information

      --accept-partial
          Flag to request the server accept partial writes
          
          Invalid lines in the input data will be ignored by the server.

A new crate, influxdb3_client, was added, which provides the Client
struct. This gives programmatic access to the infludxb3 HTTP API.

This commit implements a partial version of the api_v3_write_lp method
along with a test for ensuring the happy path. The underlying API is
not yet complete, so the response handling is still a TODO.
The new Client and types related to the Client::api_v3_write_lp method
have been documented

The stub for the write_lp_from_file test was removed, to be done later
The api_v3_query_sql method was added to the influxdb3 Client, which
allows caller to compose a request to the respective API.

Similar to the write_lp API, this uses a builder approach to composing
the request. The handling of the response was kept naive and just gives
back the bytes returned. We may improve this in future once the format
handling in the API is nailed down and we have JSON lines supported.
Switched reqwest to use rustls and disabled default features in mockito
Clean up some rustdoc comments and TODO comments

Rename the with_auth_header method to with_auth_token to distinguish
setting the token itself vs. the entire header; and rename the struct
field on Client accordingly

Indent the write_lp payload in the test code

Remove the reqwest::Client as a parameter to the Client new method
Adds two new sub-commands to the influxdb3 CLI:

- query: perform queries against the running server
- write: perform writes against the running server

Both share a common set of parameters for connecting to the database
which are managed in influxdb3/src/commands/common.rs.

Currently, query supports all underlying output formats, and can
write the output to a file on disk. It only supports SQL as the
query language, but will eventually also support InfluxQL.

Write supports line protocol for input and expects the source of
data to be from a file.
@hiltontj hiltontj self-assigned this Feb 16, 2024
@hiltontj hiltontj added the v3 label Feb 16, 2024
@hiltontj hiltontj marked this pull request as ready for review February 16, 2024 15:45
Copy link
Contributor

@mgattozzi mgattozzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should get #24665 merged in first, but this looks good to me and I saw the loom where you demonstrate it. Should make testing things out a bit easier. I'll give it the green check once that is in and this PR has merged in those changes.

influxdb3/src/main.rs Outdated Show resolved Hide resolved
eprintln!("Serve command failed: {e}");
std::process::exit(ReturnCode::Failure as _)
}
}
Some(Command::Query(config)) => {
if let Err(e) = commands::query::command(config).await {
eprintln!("Query command failed: {e}");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tangential to this, but not for this PR, but I feel like we should invest some time in the output of these errors or just the regular command output to look really nice. Possibly something like miette would be good. This is beyond the scope of this PR though. I should probably just open an issue for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened #24673

@hiltontj
Copy link
Contributor Author

@mgattozzi - I pulled down main and merged into this branch in fe7aa53 and also did the clippy thing.

@hiltontj hiltontj merged commit 6ce3165 into main Feb 20, 2024
11 checks passed
@hiltontj hiltontj deleted the hiltontj/cli-query-write branch February 20, 2024 21:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLI to write data and query using SQL
2 participants