NodeRed 向TDengine写入和查询数据

 软件环境:

ubuntu Server  20.04.3 LTS
TDengine-server-2.2.2.0-Linux-x64.deb (7.4 M)

 Docker 安装版 Node-RED version: v2.1.3

为了适应中国时区,运行如下命令:

docker run -it -e TZ="Asia/Shanghai" -p 1880:1880 -v node_red_data:/data --name mynodered nodered/node-red

退出后查看容器ID:

docker ps -a

删除nodered容器

docker rm 容器ID

首先在TDeigine创建数据库:

create database atestdb; //建立数据库atestdb
show databases;       //显示所有数据库
use atestdb;          //选择数据库
show tables;          //查看表
create table t (ts timestamp, counter int);//创建字段

NodeRed代码包含连接测试、查询测试、数据写入测试。

重点介绍一下数据的生成,

 

 

 

 

数据生成流程序如下,可单独运行测试:

[
    {
        "id": "7e0b867b.85d93",
        "type": "tab",
        "label": "生成SQL语句试验",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "44349758.22dd3",
        "type": "inject",
        "z": "7e0b867b.85d93",
        "name": "写入数据",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 140,
        "y": 240,
        "wires": [
            [
                "4192067f38ec4917",
                "f86ff67753c7260c"
            ]
        ]
    },
    {
        "id": "260aca0c.2c666e",
        "type": "debug",
        "z": "7e0b867b.85d93",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 870,
        "y": 240,
        "wires": []
    },
    {
        "id": "4192067f38ec4917",
        "type": "function",
        "z": "7e0b867b.85d93",
        "name": "时间戳",
        "func": "msg.payload=msg.payload;\nmsg.topic=\"time\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 280,
        "wires": [
            [
                "eee4cdf8844ac008"
            ]
        ]
    },
    {
        "id": "f86ff67753c7260c",
        "type": "function",
        "z": "7e0b867b.85d93",
        "name": "计数",
        "func": "var count=context.get('count') || 0;\ncount+=1;\nmsg.payload=count;\ncontext.set('count',count);\nmsg.topic=\"cnt\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 370,
        "y": 160,
        "wires": [
            [
                "eee4cdf8844ac008"
            ]
        ]
    },
    {
        "id": "eee4cdf8844ac008",
        "type": "join",
        "z": "7e0b867b.85d93",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "0",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 530,
        "y": 240,
        "wires": [
            [
                "24d7b23bf3dcd86b"
            ]
        ]
    },
    {
        "id": "24d7b23bf3dcd86b",
        "type": "function",
        "z": "7e0b867b.85d93",
        "name": "",
        "func": "var cnt=msg.payload.cnt;\nvar time=msg.payload.time\nconst table='atestdb.t'\nmsg.payload='insert into '+ table+' values ('+time +','+cnt+');';\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 690,
        "y": 240,
        "wires": [
            [
                "260aca0c.2c666e"
            ]
        ]
    }
]

 

 完整代码:

[
    {
        "id": "fc190dc.51b8a7",
        "type": "tab",
        "label": "连接TDengine",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "5d67a47a.19f2cc",
        "type": "inject",
        "z": "fc190dc.51b8a7",
        "name": "连接测试",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 100,
        "y": 120,
        "wires": [
            [
                "73dfda3f2edaa026"
            ]
        ]
    },
    {
        "id": "bfb93006.ddc1",
        "type": "http request",
        "z": "fc190dc.51b8a7",
        "name": "TDengine连接请求",
        "method": "POST",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "10.0.0.7:6041/rest/sql/testdb",
        "tls": "",
        "persist": true,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "x": 910,
        "y": 120,
        "wires": [
            [
                "60cf07ac.eab4a"
            ]
        ]
    },
    {
        "id": "60cf07ac.eab4a",
        "type": "debug",
        "z": "fc190dc.51b8a7",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 1090,
        "y": 120,
        "wires": []
    },
    {
        "id": "73dfda3f2edaa026",
        "type": "function",
        "z": "fc190dc.51b8a7",
        "name": "添加请求参数",
        "func": "msg.payload = \"show databases;\";\nmsg.headers = {};\nmsg.headers['Authorization'] = 'Basic cm9vdDp0YW9zZGF0YQ==';\nreturn msg;\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 300,
        "y": 120,
        "wires": [
            [
                "bfb93006.ddc1"
            ]
        ]
    },
    {
        "id": "f9ee71eb87d3961f",
        "type": "comment",
        "z": "fc190dc.51b8a7",
        "name": "相关参考",
        "info": "https://cookbook.nodered.org/http/set-request-header",
        "x": 150,
        "y": 40,
        "wires": []
    },
    {
        "id": "051e3a4552d9b09c",
        "type": "inject",
        "z": "fc190dc.51b8a7",
        "name": "查询表中数据",
        "props": [],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 110,
        "y": 240,
        "wires": [
            [
                "6ceef2f3ac387b8c"
            ]
        ]
    },
    {
        "id": "6ceef2f3ac387b8c",
        "type": "function",
        "z": "fc190dc.51b8a7",
        "name": "",
        "func": "msg.payload = 'select * from atestdb.t';\nmsg.headers = {};\nmsg.headers['Authorization'] = 'Basic cm9vdDp0YW9zZGF0YQ==';\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 270,
        "y": 240,
        "wires": [
            [
                "bfb93006.ddc1"
            ]
        ]
    },
    {
        "id": "fd23e483554e9ddd",
        "type": "function",
        "z": "fc190dc.51b8a7",
        "name": "向数据库表中写入数据",
        "func": "// msg.payload = \"insert into atestdb.t values ('2021-11-19 08:32:00', 21);\";\nmsg.payload=msg.payload;\nmsg.headers = {};\nmsg.headers['Authorization'] = 'Basic cm9vdDp0YW9zZGF0YQ==';\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 660,
        "y": 400,
        "wires": [
            [
                "bfb93006.ddc1"
            ]
        ]
    },
    {
        "id": "df48649be8d25dfc",
        "type": "inject",
        "z": "fc190dc.51b8a7",
        "name": "写入数据",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payloadType": "date",
        "x": 100,
        "y": 400,
        "wires": [
            [
                "10bb5ed9db1a923d",
                "1b5e119bcd6ec8e3"
            ]
        ]
    },
    {
        "id": "10bb5ed9db1a923d",
        "type": "function",
        "z": "fc190dc.51b8a7",
        "name": "时间戳",
        "func": "msg.payload=msg.payload;\nmsg.topic=\"time\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 230,
        "y": 420,
        "wires": [
            [
                "949292cf788644b2"
            ]
        ]
    },
    {
        "id": "1b5e119bcd6ec8e3",
        "type": "function",
        "z": "fc190dc.51b8a7",
        "name": "计数",
        "func": "var count=context.get('count') || 0;\ncount+=1;\nmsg.payload=count;\ncontext.set('count',count);\nmsg.topic=\"cnt\";\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 230,
        "y": 360,
        "wires": [
            [
                "949292cf788644b2"
            ]
        ]
    },
    {
        "id": "949292cf788644b2",
        "type": "join",
        "z": "fc190dc.51b8a7",
        "name": "",
        "mode": "custom",
        "build": "object",
        "property": "payload",
        "propertyType": "msg",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "0",
        "count": "2",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 370,
        "y": 400,
        "wires": [
            [
                "ac036bddd0cf8f01"
            ]
        ]
    },
    {
        "id": "ac036bddd0cf8f01",
        "type": "function",
        "z": "fc190dc.51b8a7",
        "name": "",
        "func": "var cnt=msg.payload.cnt;\nvar time=msg.payload.time\nconst table='atestdb.t'\nmsg.payload='insert into '+ table+' values ('+time +','+cnt+');';\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 490,
        "y": 400,
        "wires": [
            [
                "fd23e483554e9ddd"
            ]
        ]
    }
]

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

armcsdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值