docs-v2/content/shared/influxdb3-query-guides/execute-queries/influxdb3-cli.md

6.5 KiB

Use the influxdb3 query command to query data in {{< product-name >}} with SQL or InfluxQL.

Provide the following with your command:

  • Database name: The name of the database to query. Provide this using one of the following:

    • -d, --database command option
    • INFLUXDB3_DATABASE_NAME environment variable
  • Query language (Optional): The query language of the query. Use the -l, --language option to specify one of the following query languages:

    • sql (default)
    • influxql
  • Query: SQL or InfluxQL query to execute. Provide the query in one of the following ways:

    • a string
    • the --file option and the path to a file that contains the query
    • from stdin

{{% code-placeholders "(DATABASE|AUTH)_(TOKEN|NAME)" %}}

{{< tabs-wrapper >}} {{% tabs %}} SQL InfluxQL {{% /tabs %}}

{{% tab-content %}}

{{< code-tabs-wrapper >}} {{% code-tabs %}} string file stdin {{% /code-tabs %}} {{% code-tab-content %}}

influxdb3 query \
  --database DATABASE_NAME \
  "SELECT * FROM home"

{{% /code-tab-content %}} {{% code-tab-content %}}

influxdb3 query \
  --database DATABASE_NAME \
  --file ./query.sql

{{% /code-tab-content %}} {{% code-tab-content %}}

cat ./query.sql | influxdb3 query --database DATABASE_NAME

{{% /code-tab-content %}} {{< /code-tabs-wrapper >}}

{{% /tab-content %}}

{{% tab-content %}}

{{< code-tabs-wrapper >}} {{% code-tabs %}} string file stdin {{% /code-tabs %}} {{% code-tab-content %}}

influxdb3 query \
  --language influxql \
  --database DATABASE_NAME \
  "SELECT * FROM home"

{{% /code-tab-content %}} {{% code-tab-content %}}

influxdb3 query \
  --language influxql \
  --database DATABASE_NAME \
  --file ./query.influxql

{{% /code-tab-content %}} {{% code-tab-content %}}

cat ./query.influxql | influxdb3 query \
  --language influxql \
  --database DATABASE_NAME

{{% /code-tab-content %}} {{< /code-tabs-wrapper >}}

{{% /tab-content %}} {{< /tabs-wrapper >}}

{{% /code-placeholders %}}

In the examples above and below, replace the following:

  • {{% code-placeholder-key %}}DATABASE_NAME{{% /code-placeholder-key %}}: Name of the database to query

Output format

The influxdb3 query command supports the following output formats:

Use the --format flag to specify the output format:

{{% code-placeholders "(DATABASE|AUTH)_(TOKEN|NAME)" %}} {{% influxdb/custom-timestamps %}}

influxdb3 query \
  --database DATABASE_NAME \
  --format json \
  "SELECT * FROM home WHERE time >= '2022-01-01T08:00:00Z' LIMIT 5"

{{% /influxdb/custom-timestamps %}} {{% /code-placeholders %}}

{{< expand-wrapper >}} {{% expand "View example pretty-formatted results" %}} {{% influxdb/custom-timestamps %}}

+----+------+-------------+------+---------------------+
| co | hum  | room        | temp | time                |
+----+------+-------------+------+---------------------+
| 0  | 35.9 | Living Room | 21.1 | 2022-01-01T08:00:00 |
| 0  | 35.9 | Kitchen     | 21.0 | 2022-01-01T08:00:00 |
| 0  | 35.9 | Living Room | 21.4 | 2022-01-01T09:00:00 |
| 0  | 36.2 | Kitchen     | 23.0 | 2022-01-01T09:00:00 |
| 0  | 36.0 | Living Room | 21.8 | 2022-01-01T10:00:00 |
+----+------+-------------+------+---------------------+

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{% expand "View example JSON-formatted results" %}} {{% influxdb/custom-timestamps %}}

[{"co":0,"hum":35.9,"room":"Living Room","temp":21.1,"time":"2022-01-01T08:00:00"},{"co":0,"hum":35.9,"room":"Kitchen","temp":21.0,"time":"2022-01-01T08:00:00"},{"co":0,"hum":35.9,"room":"Living Room","temp":21.4,"time":"2022-01-01T09:00:00"},{"co":0,"hum":36.2,"room":"Kitchen","temp":23.0,"time":"2022-01-01T09:00:00"},{"co":0,"hum":36.0,"room":"Living Room","temp":21.8,"time":"2022-01-01T10:00:00"}]

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{% expand "View example JSON-line-formatted results" %}} {{% influxdb/custom-timestamps %}}

{"co":0,"hum":35.9,"room":"Living Room","temp":21.1,"time":"2022-01-01T08:00:00"}
{"co":0,"hum":35.9,"room":"Kitchen","temp":21.0,"time":"2022-01-01T08:00:00"}
{"co":0,"hum":35.9,"room":"Living Room","temp":21.4,"time":"2022-01-01T09:00:00"}
{"co":0,"hum":36.2,"room":"Kitchen","temp":23.0,"time":"2022-01-01T09:00:00"}
{"co":0,"hum":36.0,"room":"Living Room","temp":21.8,"time":"2022-01-01T10:00:00"}

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{% expand "View example CSV-formatted results" %}} {{% influxdb/custom-timestamps %}}

co,hum,room,temp,time
0,35.9,Living Room,21.1,2022-01-01T08:00:00
0,35.9,Kitchen,21.0,2022-01-01T08:00:00
0,35.9,Living Room,21.4,2022-01-01T09:00:00
0,36.2,Kitchen,23.0,2022-01-01T09:00:00
0,36.0,Living Room,21.8,2022-01-01T10:00:00

{{% /influxdb/custom-timestamps %}} {{% /expand %}} {{< /expand-wrapper >}}

Output query results to a Parquet file

To output query results to a Parquet file, provide the following options with the influxdb3 query command:

  • --format: parquet
  • -o, --output: the filepath to the Parquet file to store results in

{{% code-placeholders "(DATABASE|AUTH)_(TOKEN|NAME)" %}} {{% influxdb/custom-timestamps %}}

influxdb3 query \
  --database DATABASE_NAME \
  --format parquet \
  --output path/to/results.parquet \
  "SELECT * FROM home WHERE time >= '2022-01-01T08:00:00Z' LIMIT 5"

{{% /influxdb/custom-timestamps %}} {{% /code-placeholders %}}