2019-01-22 06:12:12 +00:00
|
|
|
---
|
2019-01-22 16:46:01 +00:00
|
|
|
title: Execute queries
|
|
|
|
seotitle: Different ways to query InfluxDB
|
2019-02-20 21:55:01 +00:00
|
|
|
description: There are multiple ways to query data from InfluxDB including the InfluxDB UI, CLI, and API.
|
2020-03-06 15:40:37 +00:00
|
|
|
weight: 103
|
2019-01-22 06:12:12 +00:00
|
|
|
menu:
|
|
|
|
v2_0:
|
2019-01-22 16:46:01 +00:00
|
|
|
name: Execute queries
|
|
|
|
parent: Query data
|
2019-02-20 00:04:06 +00:00
|
|
|
v2.0/tags: [query]
|
2019-01-22 06:12:12 +00:00
|
|
|
---
|
|
|
|
|
2019-01-22 16:46:01 +00:00
|
|
|
There are multiple ways to execute queries with InfluxDB.
|
2019-01-22 06:12:12 +00:00
|
|
|
This guide covers the different options:
|
|
|
|
|
2019-10-23 00:05:57 +00:00
|
|
|
- [Data Explorer](#data-explorer)
|
|
|
|
- [Influx REPL](#influx-repl)
|
|
|
|
- [Influx query command](#influx-query-command)
|
|
|
|
- [InfluxDB API](#influxdb-api)
|
2019-01-22 06:12:12 +00:00
|
|
|
|
|
|
|
## Data Explorer
|
2019-01-22 16:46:01 +00:00
|
|
|
Queries can be built, executed, and visualized in InfluxDB UI's Data Explorer.
|
2019-01-22 06:12:12 +00:00
|
|
|
|
2019-03-20 21:14:28 +00:00
|
|
|

|
2019-01-22 06:12:12 +00:00
|
|
|
|
|
|
|
## Influx REPL
|
|
|
|
The [`influx repl` command](/v2.0/reference/cli/influx/repl) starts an interactive
|
|
|
|
read-eval-print-loop (REPL) where you can write and execute Flux queries.
|
|
|
|
|
|
|
|
```bash
|
|
|
|
influx repl --org org-name
|
|
|
|
```
|
|
|
|
|
2019-11-06 19:25:06 +00:00
|
|
|
{{% note %}}
|
|
|
|
`ctrl-d` will close the REPL.
|
|
|
|
{{% /note %}}
|
2019-01-22 17:20:10 +00:00
|
|
|
|
2019-01-22 06:12:12 +00:00
|
|
|
## Influx query command
|
2019-01-22 16:46:01 +00:00
|
|
|
You can pass queries to the [`influx query` command](/v2.0/reference/cli/influx/query)
|
2019-01-22 06:12:12 +00:00
|
|
|
as either a file or raw Flux via stdin.
|
|
|
|
|
2019-01-22 16:46:01 +00:00
|
|
|
###### Run a query from a file
|
2019-01-22 06:12:12 +00:00
|
|
|
```bash
|
|
|
|
influx query @/path/to/query.flux
|
|
|
|
```
|
|
|
|
|
|
|
|
###### Pass raw Flux via stdin pipe
|
|
|
|
```bash
|
|
|
|
influx query - # Return to open the pipe
|
|
|
|
|
|
|
|
data = from(bucket: "example-bucket") |> range(start: -10m) # ...
|
|
|
|
# ctrl-d to close the pipe and submit the query
|
|
|
|
```
|
|
|
|
|
|
|
|
## InfluxDB API
|
2019-04-18 14:59:43 +00:00
|
|
|
The [InfluxDB v2 API](/v2.0/reference/api) provides a programmatic
|
|
|
|
interface for all interactions with InfluxDB.
|
2019-01-22 15:54:31 +00:00
|
|
|
Query InfluxDB through the `/api/v2/query` endpoint.
|
2019-01-22 06:12:12 +00:00
|
|
|
Queried data is returned in annotated CSV format.
|
|
|
|
|
|
|
|
In your request, set the following:
|
|
|
|
|
2019-02-27 21:24:00 +00:00
|
|
|
- Your organization via the `org` or `orgID` URL parameters.
|
2019-01-22 15:54:31 +00:00
|
|
|
- `Authorization` header to `Token ` + your authentication token.
|
2019-09-19 15:31:28 +00:00
|
|
|
- `Accept` header to `application/csv`.
|
|
|
|
- `Content-type` header to `application/vnd.flux`.
|
|
|
|
- Your plain text query as the request's raw data.
|
2019-01-22 06:12:12 +00:00
|
|
|
|
2019-10-30 19:24:58 +00:00
|
|
|
InfluxDB returns the query results in [annotated CSV](/v2.0/reference/syntax/annotated-csv/).
|
2019-09-19 15:31:28 +00:00
|
|
|
|
|
|
|
{{% note %}}
|
|
|
|
#### Use gzip to compress the query response
|
|
|
|
To compress the query response, set the `Accept-Encoding` header to `gzip`.
|
|
|
|
This saves network bandwidth, but increases server-side load.
|
|
|
|
{{% /note %}}
|
2019-01-22 06:12:12 +00:00
|
|
|
|
2019-01-22 15:54:31 +00:00
|
|
|
Below is an example `curl` command that queries InfluxDB:
|
2019-01-22 06:12:12 +00:00
|
|
|
|
|
|
|
{{< code-tabs-wrapper >}}
|
|
|
|
{{% code-tabs %}}
|
2019-09-19 15:31:28 +00:00
|
|
|
[Without compression](#)
|
|
|
|
[With compression](#)
|
2019-01-22 06:12:12 +00:00
|
|
|
{{% /code-tabs %}}
|
|
|
|
|
|
|
|
{{% code-tab-content %}}
|
|
|
|
```bash
|
2019-02-27 21:24:00 +00:00
|
|
|
curl http://localhost:9999/api/v2/query?org=my-org -XPOST -sS \
|
2019-09-19 15:31:28 +00:00
|
|
|
-H 'Authorization: Token YOURAUTHTOKEN' \
|
|
|
|
-H 'Accept: application/csv' \
|
|
|
|
-H 'Content-type: application/vnd.flux' \
|
2019-09-30 16:50:04 +00:00
|
|
|
-d 'from(bucket:"example-bucket")
|
2019-09-19 15:31:28 +00:00
|
|
|
|> range(start:-1000h)
|
2019-09-30 16:50:04 +00:00
|
|
|
|> group(columns:["_measurement"], mode:"by")
|
2019-09-19 15:31:28 +00:00
|
|
|
|> sum()'
|
2019-01-22 06:12:12 +00:00
|
|
|
```
|
|
|
|
{{% /code-tab-content %}}
|
|
|
|
|
|
|
|
{{% code-tab-content %}}
|
|
|
|
```bash
|
2019-09-19 15:31:28 +00:00
|
|
|
curl http://localhost:9999/api/v2/query?org=my-org -XPOST -sS \
|
|
|
|
-H 'Authorization: Token YOURAUTHTOKEN' \
|
|
|
|
-H 'Accept: application/csv' \
|
|
|
|
-H 'Content-type: application/vnd.flux' \
|
|
|
|
-H 'Accept-Encoding: gzip' \
|
2019-09-30 16:50:04 +00:00
|
|
|
-d 'from(bucket:"example-bucket")
|
2019-09-19 15:31:28 +00:00
|
|
|
|> range(start:-1000h)
|
2019-09-30 16:50:04 +00:00
|
|
|
|> group(columns:["_measurement"], mode:"by")
|
2019-09-19 15:31:28 +00:00
|
|
|
|> sum()'
|
2019-01-22 06:12:12 +00:00
|
|
|
```
|
|
|
|
{{% /code-tab-content %}}
|
|
|
|
{{< /code-tabs-wrapper >}}
|