目标:

在dockers中分别运行influxdb2和Node-RED,Node-RED中node-red-contrib-influxdb(0.6.1)作为客户端读写influxdb2

架构:

 

前提:

Docker (18)

Python(3.7.3,optional)

安装环境:

​​​​​​​

1. Influxdb[1]

1.1  Install

  • Create a new directory to store your data in and navigate into the directory.
  • From within your new directory, run the InfluxDB Docker container with the --volume flag to persist data from /root/.influxdb2/ inside the container to the current working directory in the host file system.

docker run --name influxdb -p 8086:8086 --volume $PWD:/var/lib/influxdb2 influxdb:2.0.7

可选通过CLI 控制数据库

To use the influx command line interface, console into the influxdb Docker container:

docker exec -it influxdb /bin/bash

 You could use default default configuration

1.2 Set up InfluxDB

第一次访问host Linux: 127.0.0.1:8086

 

MySQL

InfluxDB

数据库

database

bucket

表名

table

measurement

记录

rows

point

字段

columns

time+tag+field

  • time时间戳,就像是所有数据的主键一样。
  • Tag_key = tag_value 键值对存储具体的数据,会构建索引有利于查询。tag set 就是 tag key-value 键值对的不同组合。
  • Field_key = field_value  键值对也是存储具体的数据,但不会被索引。类似的 field set 就是 field key-value 的组合。

 RP 策略( retention policy ),它由三个部分构成:

  • DURATION:数据的保留时长。
  • REPLICATION:集群模式下数据的副本数,单节点无效。
  • SHARD DURATION:可选项,shard group 划分的时间范围。

 

1.3 查看token

 

 

1.4 Python 读写数据库[2]

首页,选择data

 

安装

pip3 install influxdb-client

程序

from datetime import datetime

from influxdb_client import InfluxDBClient, Point, WritePrecision

from influxdb_client.client.write_api import SYNCHRONOUS

from random import *

import time

# You can generate a Token from the "Tokens Tab" in the UI

token = "你的token"

org = "org1"

bucket = "bucket1"

client = InfluxDBClient(url="http://192.168.8.148:8086", token=token)

write_api = client.write_api(write_options=SYNCHRONOUS)

for num in range(1,10):

    print(num)

    point = Point("my_measurement").field("current", random()*10)

    time.sleep(1)

    write_api.write(bucket, org, point)

sql = '''from(bucket: "bucket1")

  |> range(start: -1h)

  |> filter(fn: (r) => r["_measurement"] == "my_measurement")

  |> filter(fn: (r) => r["_field"] == "current")

  |> unique()

  |> yield(name: "unique")

'''

tables = client.query_api().query(sql, org=org)

for table in tables:

    for row in table.records:

        print(row.values)

黄色为可修改参数

注意:192.168.8.148为host 地址

结果:

 

查看数据

 对应的查询语句为:

 

可直接用于为python

2 Node-RED

2.1 安装Node-RED[3]

docker run -it -p 1880:1880 --name mynodered nodered/node-red

 

1.2 安装 node-red-contrib-influxdb[4]

 

 1.3 示例程序

参见[4]

需要修改的是:其中URL可以为:

 

 

Host地址:192.168.8.148:8086

Influxdb docker地址:172.17.0.6:8086

不能用127.0.0.1:8086(因为Node-RED访问的是自己<172.17.0.5:8086>的地址),会导致错误

 

正常结果是:

写入

 

读出

 

参考:

  1. https://docs.influxdata.com/influxdb/v2.0/install/?t=Docker
  2. https://influxdb-client.readthedocs.io/en/stable/
  3. https://nodered.org/docs/getting-started/local
  4. https://flows.nodered.org/node/node-red-contrib-influxdb
  5. https://flows.nodered.org/node/node-red-contrib-stackhero-influxdb-v2
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐