docs-v2/content/v2.0/query-data/execute-queries.md

93 lines
2.7 KiB
Markdown
Raw Normal View History

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
description: There are multiple ways to query data from InfluxDB including the InfluxDB UI, CLI, and API.
weight: 102
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:
1. [Data Explorer](#data-explorer)
2. [Influx REPL](#influx-repl)
3. [Influx query command](#influx-query-command)
5. [InfluxDB API](#influxdb-api)
## 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
![Data Explorer with Flux](/img/2-0-data-explorer-ui.png)
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-01-22 17:20:10 +00:00
_**Note:** `ctrl-d` will close the REPL._
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
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:
- Your organization via the `org` or `orgID` URL parameters.
- `Authorization` header to `Token ` + your authentication token.
- `accept` header to `application/csv`.
- `content-type` header to `application/vnd.flux`.
2019-01-22 06:12:12 +00:00
This allows you to POST the Flux query in plain text and receive the annotated CSV response.
Below is an example `curl` command that queries InfluxDB:
2019-01-22 06:12:12 +00:00
{{< code-tabs-wrapper >}}
{{% code-tabs %}}
[Multi-line](#)
[Single-line](#)
{{% /code-tabs %}}
{{% code-tab-content %}}
```bash
curl http://localhost:9999/api/v2/query?org=my-org -XPOST -sS \
-H 'Authorization: Token YOURAUTHTOKEN' \
2019-01-22 06:12:12 +00:00
-H 'accept:application/csv' \
-H 'content-type:application/vnd.flux' \
-d 'from(bucket:“test”)
|> range(start:-1000h)
|> group(columns:[“_measurement”], mode:“by”)
|> sum()'
2019-01-22 06:12:12 +00:00
```
{{% /code-tab-content %}}
{{% code-tab-content %}}
```bash
curl http://localhost:9999/api/v2/query?org=my-org -XPOST -sS -H 'Authorization: Token TOKENSTRINGHERE' -H 'accept:application/csv' -H 'content-type:application/vnd.flux' -d 'from(bucket:“test”) |> range(start:-1000h) |> group(columns:[“_measurement”], mode:“by”) |> sum()'
2019-01-22 06:12:12 +00:00
```
{{% /code-tab-content %}}
{{< /code-tabs-wrapper >}}