InfluxDB2.x数据写入和查询

目录

新建bucket

准备数据

数据写入

UI方式写入

InfluxDB API方式写入

Influx CLI方式写入

数据查询

使用Flux查询

InfluxQL查询数据

执行查询

UI执行Flux

Influx CLI执行Flux

InfluxDB API执行Flux

REPL执行Flux


新建bucket

influx bucket create --name test

准备数据

test.line文件内容如下:

myMeasurement,tag1=value1,tag2=value2 fieldKey="fieldValue"

 文件内容没有时间戳,仅为了查询方便。

数据写入

UI方式写入

        Load Data-->Sources,如图:

InfluxDB2.x数据写入和查询_第1张图片

点击Line Protocol,如图:

InfluxDB2.x数据写入和查询_第2张图片

 选择要写入的bucket和Precision时间精度,上传test.line文件,数据写入成功后如图:

InfluxDB2.x数据写入和查询_第3张图片 

InfluxDB API方式写入

InfluxDB2.x数据写入和查询_第4张图片

 例如:

curl --request POST \
"http://localhost:8086/api/v2/write?org=copote&bucket=test&precision=ns" \
  --header "Authorization: Token VhDx9snZcs4g0wWZwMQXeu2sZjaAWk599ddJRlm98mejSJfDGP038a5IVLEClPDFYqJ7uescw3W1hqAaIYUSLg==" \
  --header "Content-Type: text/plain; charset=utf-8" \
  --header "Accept: application/json" \
  --data-binary '
    myMeasurement,tag1=t1,tag2=t2 fieldKey="fk3"
    '

 

 

Influx CLI方式写入

InfluxDB2.x数据写入和查询_第5张图片

 

influx write \
  -b test \
  -o copote \
  -p ns \
  'myMeasurement,tag1=influx-cli,tag2=influx-cli fieldKey="influx-cli"'

如图: 

 

数据查询

        InfluxDB2.x提供了两种语法查询数据,Flux和InfluxQL。

使用Flux查询

       Flux是一种功能性数据脚本语言,旨在将查询、处理、分析和对数据的操作统一为一个语法。每个Flux查询需要以下步骤:

1、数据源;

2、时间范围;

3、数据过滤器。

(1)from()定义InfluxDB数据源

from()需要一个bucket参数,例如从test桶查询数据:

from(bucket:"test")

 (2)确定时间范围

Flux查询时间序列数据时需要一个时间范围。“无界”查询是非常消耗资源的,作为一种保护措施,Flux不会在没有指定范围的情况下查询数据库。使用管道转发操作符(|>)将数据源中的数据输出到range(),range()接受两个参数:start和stop,这两个参数可以是相对的(使用duration),也可以是绝对的(使用时间戳),但都是负的,例如:

from(bucket: "test")
    |> range(start: -15m)

(3) 过滤器

        使用filter()对输入的数据进行过滤,它有一个参数fn,这个参数是一个函数,fn函数有一个参数,这个参数表示记录,函数体是过滤条件,例如:

from(bucket:"test")
    |> range(start: -15d)
    |> filter(fn: (r) => r.tag1== "value1")

InfluxDB2.x数据写入和查询_第6张图片

 (4)输出查询结果

yield输出查询结果,例如

from(bucket: "test")
    |> range(start: -15m)
    |> yield(name: "results")

 

InfluxQL查询数据

        InfluxDB 1.x数据存储在数据库(database)中,InfluxDB OSS 2.2中,数据存储在桶(bucket)中。因为InfluxQL使用了1.x数据模型,在使用InfluxQL进行查询之前,必须将桶映射到一个数据库和保留策略(DBRP)。可以这样理解:InfluxQL只有数据库才能使用,如果想要在桶上也能使用只有将桶映射成数据库。

使用InfluxQL查询桶数据,需要完成以下步骤:

1、确认桶有映射;

2、映射未映射的桶;

3、使用InfluxQL查询已映射的桶。

(1)确认桶有映射

如下命令可以查询所有的DBRP(DataBase Retention Police)映射

influx v1 dbrp list

或者根据bucket id来过滤

influx v1 dbrp list --bucket-id 04deb39109ae7bc6

如图:

结果显示名为test的桶并没有DBRP映射。

(2)映射未映射的桶

  influx v1 dbrp create \
  --db test_db \
  --rp test_rp \
  --bucket-id 04deb39109ae7bc6 \
  --default

(3) 使用InfluxQL查询已映射的桶

curl --get http://localhost:8086/query?db=test-db \
  --header "Authorization: Token VhDx9snZcs4g0wWZwMQXeu2sZjaAWk599ddJRlm98mejSJfDGP038a5IVLEClPDFYqJ7uescw3W1hqAaIYUSLg==" \
  --data-urlencode "q=SELECT * FROM test_db.test_rp.myMeasurement"

 查询结果如图:

执行查询

        InfluxDB2.x提供了很多种方式执行Flux或者InfluxQL,例如,UI的Data Explorer、Influx CLI、InfluxDB API和REPL。

UI执行Flux

        Data-->Explorer,如图:

InfluxDB2.x数据写入和查询_第7张图片

 点击Script Editor,写入Flux查询语句:

from(bucket: "test")
    |> range(start: -15m)
    |> yield(name: "results")

单击submit,成功查询如图:

InfluxDB2.x数据写入和查询_第8张图片

Influx CLI执行Flux

        首先输入influx query打开查询管道(pipe),然后输入Flux查询语句,如图:

 按住Ctrl+D,执行查询,如图:

InfluxDB2.x数据写入和查询_第9张图片 

InfluxDB API执行Flux

        参考【InfluxQL查询数据】章节。

REPL执行Flux

        Read-Eval-Print Loop交互式解释器。需要安装

Use the Interactive Flux REPL | InfluxDB OSS 2.2 DocumentationUse the Flux REPL (Read–Eval–Print Loop) to execute Flux scripts and interact with InfluxDB and other data sources.https://docs.influxdata.com/influxdb/v2.2/tools/repl/

你可能感兴趣的:(InfluxDB,influxdb,数据查询,InfluxQL,Flux,数据写入)