查询 CSV 数据源
使用 csv.from()
和 experimental csv.from()
以 Flux 查询 CSV 数据。查询 CSV 字符串、CSV 文件或来自 URL 的 CSV 数据。导入 csv
或 experimental/csv
包。
CSV 解析模式
csv.from()
支持两种 CSV 解析模式
- annotations: (默认) 使用 CSV 注释来确定列数据类型和表分组。
- raw: 将所有列解析为字符串,并将第一行用作标题行,所有后续行用作数据。
当使用 annotations 解析模式时,CSV 数据必须包含所有注释行 (#datatype
、#group
和 #default
)。
结果结构
csv.from()
返回的结果结构取决于使用的 解析模式。
- annotations:
csv.from()
返回按#group
注释行中定义为true
的列分组的表流。 - raw:
csv.from()
返回没有分组的表流(所有行都在单个表中)。所有数据都格式化为字符串。
示例
如果刚刚开始,请使用 Flux REPL 或 InfluxDB 数据浏览器 来执行 Flux 查询。
查询带注释的 CSV 字符串
- 导入
csv
包。 - 使用
csv.from()
和csv
参数来指定要查询的带注释的 CSV 字符串。
查询
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)
结果
数据集 | 指标 | 传感器 ID | 时间戳 | 值 |
---|---|---|---|---|
空气传感器 | 湿度 | TLM0100 | 1627049400000000000 | 34.79 |
空气传感器 | 湿度 | TLM0100 | 1627049700000000000 | 34.65 |
数据集 | 指标 | 传感器 ID | 时间戳 | 值 |
---|---|---|---|---|
空气传感器 | 湿度 | TLM0200 | 1627049400000000000 | 35.64 |
空气传感器 | 湿度 | TLM0200 | 1627049700000000000 | 35.67 |
数据集 | 指标 | 传感器 ID | 时间戳 | 值 |
---|---|---|---|---|
空气传感器 | 温度 | TLM0100 | 1627049400000000000 | 71.84 |
空气传感器 | 温度 | TLM0100 | 1627049700000000000 | 71.87 |
数据集 | 指标 | 传感器 ID | 时间戳 | 值 |
---|---|---|---|---|
空气传感器 | 温度 | TLM0200 | 1627049400000000000 | 74.10 |
空气传感器 | 温度 | TLM0200 | 1627049700000000000 | 74.17 |
查询原始 CSV 字符串
导入
csv
包。使用
csv.from()
并提供以下参数- csv: 要查询的 CSV 字符串
- mode: raw
查询
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")
结果
当使用 raw CSV 解析模式时,所有列值都是字符串。
数据集 | 指标 | 传感器 ID | 时间戳 | 值 |
---|---|---|---|---|
空气传感器 | 湿度 | TLM0100 | 1627049400000000000 | 34.79 |
空气传感器 | 湿度 | TLM0100 | 1627049700000000000 | 34.65 |
空气传感器 | 湿度 | TLM0200 | 1627049400000000000 | 35.64 |
空气传感器 | 湿度 | TLM0200 | 1627049700000000000 | 35.67 |
空气传感器 | 温度 | TLM0100 | 1627049400000000000 | 71.84 |
空气传感器 | 温度 | TLM0100 | 1627049700000000000 | 71.87 |
空气传感器 | 温度 | TLM0200 | 1627049400000000000 | 74.10 |
空气传感器 | 温度 | TLM0200 | 1627049700000000000 | 74.17 |
从文件查询 CSV 数据
- 导入
csv
包。 - 使用
csv.from()
和file
参数来从文件查询 CSV 数据。
Flux 必须有权访问文件系统
要从文件查询 CSV 数据,Flux 必须有权访问文件系统。如果 Flux 没有文件系统访问权限,查询将返回类似于以下的错误
failed to read file: filesystem service is uninitialized
如果使用 InfluxDB Cloud 或 InfluxDB OSS,Flux 进程没有文件系统访问权限。
查询
import "csv"
csv.from(file: "/path/to/example.csv")
/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
结果
数据集 | 指标 | 传感器 ID | 时间戳 | 值 |
---|---|---|---|---|
空气传感器 | 湿度 | TLM0100 | 1627049400000000000 | 34.79 |
空气传感器 | 湿度 | TLM0100 | 1627049700000000000 | 34.65 |
数据集 | 指标 | 传感器 ID | 时间戳 | 值 |
---|---|---|---|---|
空气传感器 | 湿度 | TLM0200 | 1627049400000000000 | 35.64 |
空气传感器 | 湿度 | TLM0200 | 1627049700000000000 | 35.67 |
数据集 | 指标 | 传感器 ID | 时间戳 | 值 |
---|---|---|---|---|
空气传感器 | 温度 | TLM0100 | 1627049400000000000 | 71.84 |
空气传感器 | 温度 | TLM0100 | 1627049700000000000 | 71.87 |
数据集 | 指标 | 传感器 ID | 时间戳 | 值 |
---|---|---|---|---|
空气传感器 | 温度 | TLM0200 | 1627049400000000000 | 74.10 |
空气传感器 | 温度 | TLM0200 | 1627049700000000000 | 74.17 |
从 URL 查询 CSV 数据
- 导入
experimental/csv
包。 - 使用 experimental
csv.from()
函数 和url
参数来指定要查询的 URL。
experimental csv.from()
函数不支持多种解析模式,仅适用于 带注释的 CSV。
import "experimental/csv"
csv.from(url: "https://example.com/example.csv")
要使用 csv.from()
中提供的解析模式
- 导入
csv
和experimental/http
包。 - 使用
http.get()
来获取 CSV 数据。 - 使用
string()
将响应体转换为字符串。 - 使用
csv.from()
来解析 CSV 数据并返回结果。
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")
此页是否对您有帮助?
感谢您的反馈!