2019-01-22 00:05:24 +00:00
---
title: Query InfluxDB with Flux
description: Learn the basics of using Flux to query data from InfluxDB.
2019-02-13 21:53:59 +00:00
v2.0/tags: [query, flux]
2019-01-22 00:05:24 +00:00
menu:
v2_0:
name: Query InfluxDB
2019-01-22 16:46:01 +00:00
parent: Get started with Flux
2019-02-06 17:48:09 +00:00
weight: 201
2019-05-21 21:21:24 +00:00
related:
- /v2.0/query-data/guides/
2019-09-10 18:38:38 +00:00
- /v2.0/reference/flux/stdlib/built-in/inputs/from
- /v2.0/reference/flux/stdlib/built-in/transformations/range
- /v2.0/reference/flux/stdlib/built-in/transformations/filter
2019-01-22 00:05:24 +00:00
---
This guide walks through the basics of using Flux to query data from InfluxDB.
Every Flux query needs the following:
1. [A data source ](#1-define-your-data-source )
2. [A time range ](#2-specify-a-time-range )
3. [Data filters ](#3-filter-your-data )
## 1. Define your data source
2019-09-10 18:38:38 +00:00
Flux's [`from()` ](/v2.0/reference/flux/stdlib/built-in/inputs/from ) function defines an InfluxDB data source.
It requires a [`bucket` ](/v2.0/reference/flux/stdlib/built-in/inputs/from#bucket ) parameter.
2019-01-22 00:05:24 +00:00
The following examples use `example-bucket` as the bucket name.
```js
from(bucket:"example-bucket")
```
## 2. Specify a time range
Flux requires a time range when querying time series data.
"Unbounded" queries are very resource-intensive and as a protective measure,
Flux will not query the database without a specified range.
2019-09-10 18:38:38 +00:00
Use the pipe-forward operator (`|>`) to pipe data from your data source into the [`range()` ](/v2.0/reference/flux/stdlib/built-in/transformations/range )
2019-01-22 00:05:24 +00:00
function, which specifies a time range for your query.
2019-10-14 17:19:55 +00:00
It accepts two parameters: `start` and `stop` .
2019-01-22 05:31:27 +00:00
Ranges can be **relative** using negative [durations ](/v2.0/reference/flux/language/lexical-elements#duration-literals )
or **absolute** using [timestamps ](/v2.0/reference/flux/language/lexical-elements#date-and-time-literals ).
2019-01-22 00:05:24 +00:00
###### Example relative time ranges
```js
// Relative time range with start only. Stop defaults to now.
from(bucket:"example-bucket")
|> range(start: -1h)
// Relative time range with start and stop
from(bucket:"example-bucket")
|> range(start: -1h, stop: -10m)
```
2019-01-22 16:46:01 +00:00
{{% note %}}
Relative ranges are relative to "now."
{{% /note %}}
2019-01-22 00:05:24 +00:00
###### Example absolute time range
```js
from(bucket:"example-bucket")
|> range(start: 2018-11-05T23:30:00Z, stop: 2018-11-06T00:00:00Z)
```
#### Use the following:
For this guide, use the relative time range, `-15m` , to limit query results to data from the last 15 minutes:
```js
from(bucket:"example-bucket")
|> range(start: -15m)
```
## 3. Filter your data
Pass your ranged data into the `filter()` function to narrow results based on data attributes or columns.
The `filter()` function has one parameter, `fn` , which expects an anonymous function
with logic that filters data based on columns or attributes.
Flux's anonymous function syntax is similar to Javascript's.
Records or rows are passed into the `filter()` function as an object (`r`).
The anonymous function takes the object and evaluates it to see if it matches the defined filters.
Use the `and` relational operator to chain multiple filters.
```js
// Pattern
(r) => (r.objectProperty comparisonOperator comparisonExpression)
// Example with single filter
(r) => (r._measurement == "cpu")
// Example with multiple filters
(r) => (r._measurement == "cpu") and (r._field != "usage_system" )
```
#### Use the following:
For this example, filter by the `cpu` measurement, the `usage_system` field, and the `cpu-total` tag value:
```js
from(bucket:"example-bucket")
|> range(start: -15m)
|> filter(fn: (r) =>
r._measurement == "cpu" and
r._field == "usage_system" and
r.cpu == "cpu-total"
)
```
## 4. Yield your queried data
2019-10-14 17:19:55 +00:00
Flux's `yield()` function outputs the filtered tables as the result of the query.
2019-01-22 00:05:24 +00:00
```js
from(bucket:"example-bucket")
|> range(start: -15m)
|> filter(fn: (r) =>
r._measurement == "cpu" and
r._field == "usage_system" and
r.cpu == "cpu-total"
)
|> yield()
```
2019-10-14 17:19:55 +00:00
Flux automatically assumes a `yield()` function at
2019-01-22 00:05:24 +00:00
the end of each script in order to output and visualize the data.
2019-10-14 17:19:55 +00:00
Explicitly calling `yield()` is only necessary when including multiple queries in the same Flux query.
2019-01-22 00:05:24 +00:00
Each set of returned data needs to be named using the `yield()` function.
## Congratulations!
You have now queried data from InfluxDB using Flux.
2019-10-14 17:19:55 +00:00
The query shown here is a barebones example.
Flux queries can be extended in many ways to form powerful scripts.
2019-01-22 00:05:24 +00:00
< div class = "page-nav-btns" >
2019-01-22 16:46:01 +00:00
< a class = "btn prev" href = "/v2.0/query-data/get-started/" > Get started with Flux< / a >
< a class = "btn next" href = "/v2.0/query-data/get-started/transform-data/" > Transform your data< / a >
2019-01-22 00:05:24 +00:00
< / div >