content/flux/v0/query-data/csv.md
Use csv.from() and
experimental csv.from()
to query CSV data with Flux.
Query a CSV string, CSV file, or CSV data from a URL.
Import the csv or experimental/csv package.
csv.from() supports two CSV parsing modes:
{{% note %}}
When using the annotations parsing mode, CSV data must include all annotation rows
(#datatype, #group, and #default).
{{% /note %}}
The structure of results returned by csv.from() depends on the
parsing mode used.
csv.from() returns a stream of tables grouped by columns
defined as true in the #group annotation row.csv.from()returns a stream of tables with no grouping (all rows are in a single table).
All data is formatted as strings.If just getting started, use the Flux REPL or the InfluxDB Data Explorer to execute Flux queries.
csv package.csv.from() and the csv parameter to
specify the annotated CSV string to query.import "csv"
csvData =
"
#group,false,false,true,true,true,false,false
#datatype,string,long,string,string,string,long,double
#default,_result,,,,,,
,result,table,dataset,metric,sensorID,timestamp,value
,,0,air-sensors,humidity,TLM0100,1627049400000000000,34.79
,,0,air-sensors,humidity,TLM0100,1627049700000000000,34.65
,,1,air-sensors,humidity,TLM0200,1627049400000000000,35.64
,,1,air-sensors,humidity,TLM0200,1627049700000000000,35.67
,,2,air-sensors,temperature,TLM0100,1627049400000000000,71.84
,,2,air-sensors,temperature,TLM0100,1627049700000000000,71.87
,,3,air-sensors,temperature,TLM0200,1627049400000000000,74.10
,,3,air-sensors,temperature,TLM0200,1627049700000000000,74.17
"
csv.from(csv: csvData)
| dataset | metric | sensorID | timestamp | value |
|---|---|---|---|---|
| air-sensors | humidity | TLM0100 | 1627049400000000000 | 34.79 |
| air-sensors | humidity | TLM0100 | 1627049700000000000 | 34.65 |
| dataset | metric | sensorID | timestamp | value |
|---|---|---|---|---|
| air-sensors | humidity | TLM0200 | 1627049400000000000 | 35.64 |
| air-sensors | humidity | TLM0200 | 1627049700000000000 | 35.67 |
| dataset | metric | sensorID | timestamp | value |
|---|---|---|---|---|
| air-sensors | temperature | TLM0100 | 1627049400000000000 | 71.84 |
| air-sensors | temperature | TLM0100 | 1627049700000000000 | 71.87 |
| dataset | metric | sensorID | timestamp | value |
|---|---|---|---|---|
| air-sensors | temperature | TLM0200 | 1627049400000000000 | 74.10 |
| air-sensors | temperature | TLM0200 | 1627049700000000000 | 74.17 |
Import the csv package.
Use csv.from() and provide the following parameters:
import "csv"
csvData =
"
dataset,metric,sensorID,timestamp,value
air-sensors,humidity,TLM0100,1627049400000000000,34.79
air-sensors,humidity,TLM0100,1627049700000000000,34.65
air-sensors,humidity,TLM0200,1627049400000000000,35.64
air-sensors,humidity,TLM0200,1627049700000000000,35.67
air-sensors,temperature,TLM0100,1627049400000000000,71.84
air-sensors,temperature,TLM0100,1627049700000000000,71.87
air-sensors,temperature,TLM0200,1627049400000000000,74.10
air-sensors,temperature,TLM0200,1627049700000000000,74.17
"
csv.from(csv: csvData, mode: "raw")
{{% note %}} When using the raw CSV parsing mode, all columns values are strings. {{% /note %}}
| dataset | metric | sensorID | timestamp | value |
|---|---|---|---|---|
| air-sensors | humidity | TLM0100 | 1627049400000000000 | 34.79 |
| air-sensors | humidity | TLM0100 | 1627049700000000000 | 34.65 |
| air-sensors | humidity | TLM0200 | 1627049400000000000 | 35.64 |
| air-sensors | humidity | TLM0200 | 1627049700000000000 | 35.67 |
| air-sensors | temperature | TLM0100 | 1627049400000000000 | 71.84 |
| air-sensors | temperature | TLM0100 | 1627049700000000000 | 71.87 |
| air-sensors | temperature | TLM0200 | 1627049400000000000 | 74.10 |
| air-sensors | temperature | TLM0200 | 1627049700000000000 | 74.17 |
csv package.csv.from() and the file parameter to
query CSV data from a file.{{% note %}}
To query CSV data from a file, Flux must have access to the filesystem. If Flux does not have access to the file system, the query will return an error similar to:
failed to read file: filesystem service is uninitialized
If using InfluxDB Cloud or InfluxDB OSS, the Flux process does not have access to the filesystem. {{% /note %}}
import "csv"
csv.from(file: "/path/to/example.csv")
#group,false,false,true,true,true,false,false
#datatype,string,long,string,string,string,long,double
#default,_result,,,,,,
,result,table,dataset,metric,sensorID,timestamp,value
,,0,air-sensors,humidity,TLM0100,1627049400000000000,34.79
,,0,air-sensors,humidity,TLM0100,1627049700000000000,34.65
,,1,air-sensors,humidity,TLM0200,1627049400000000000,35.64
,,1,air-sensors,humidity,TLM0200,1627049700000000000,35.67
,,2,air-sensors,temperature,TLM0100,1627049400000000000,71.84
,,2,air-sensors,temperature,TLM0100,1627049700000000000,71.87
,,3,air-sensors,temperature,TLM0200,1627049400000000000,74.10
,,3,air-sensors,temperature,TLM0200,1627049700000000000,74.17
| dataset | metric | sensorID | timestamp | value |
|---|---|---|---|---|
| air-sensors | humidity | TLM0100 | 1627049400000000000 | 34.79 |
| air-sensors | humidity | TLM0100 | 1627049700000000000 | 34.65 |
| dataset | metric | sensorID | timestamp | value |
|---|---|---|---|---|
| air-sensors | humidity | TLM0200 | 1627049400000000000 | 35.64 |
| air-sensors | humidity | TLM0200 | 1627049700000000000 | 35.67 |
| dataset | metric | sensorID | timestamp | value |
|---|---|---|---|---|
| air-sensors | temperature | TLM0100 | 1627049400000000000 | 71.84 |
| air-sensors | temperature | TLM0100 | 1627049700000000000 | 71.87 |
| dataset | metric | sensorID | timestamp | value |
|---|---|---|---|---|
| air-sensors | temperature | TLM0200 | 1627049400000000000 | 74.10 |
| air-sensors | temperature | TLM0200 | 1627049700000000000 | 74.17 |
experimental/csv package.csv.from() function
and url parameter to specify the URL to query.{{% note %}}
The experimental csv.from() function does not support multiple parsing modes
and only works with annotated CSV.
{{% /note %}}
import "experimental/csv"
csv.from(url: "https://example.com/example.csv")
To use the parsing modes available in csv.from():
csv and experimental/http packages.http.get() to fetch the CSV data.string() to convert the response body to a string.csv.from() to parse the CSV data and return results.import "csv"
import "experimental/http"
url = "https://example.com/example.csv"
csvData = string(v: http.get(url: url).body)
csv.from(csv: csvData, mode: "raw")