使用Node-red插入TDengine时序数据库的方法

安装node节点模块

目前有两个模块可以支持TDengine的连接驱动

图片

分别是以下三个,注意版本的区别,其中第一个的作者是TD官方,后两个安装一个即可,功能一样。

  • node-red-node-tdengine V1.00
  • node-red-contrib-tdengine V0.05
  • node-red-lvsy V0.01

模块的说明

图片

可以在帮助中,查看详细的使用手册。在此简单说明重点。
node-red-node-tdengine模组,使用topic 传递 SQL 语句
msg = { topic: "SQL 语句" }
负载格式

msg = {
 topic: "insert into ...",
 isQuery: false, // 查询类为 true, 否则 false
 payload: {
   affectRows: 2,  // 影响行数
   totalTime: 3,   // 写入总耗时(毫秒)
   timing: 1683311 // 写入在服务器上执行时间(纳秒)
 }
}

数据库配置

填写数据库名称

图片

这里有两种连接配置可选
URL的格式为:协议类型- 用户名-密码-IP-端口
第二种配置内容和第一种相同。

图片

flows

使用inject节点,充当外部激励源,使用debug节点查看节点的响应。
图片
payload默认时间戳即可,在topic中填写插入SQL。
这句sql的含义翻译一下就是:插入 null 表中,使用JYdetected_data数据库,标签为null、null、null,时间戳和插入值
关于TD的结构和特性不做赘述,还是很有趣的,分享一个分析方法的概念不知为何就关联到一起了。>https://www.cnblogs.com/abaelhe/p/19029910

INSERT INTO 'null' USING JYdetected_data TAGS ('null', 'null', 'null') VALUES (NOW(), null)

图片

可以在debug中看到插入成功的返回响应。

图片

关于第二种方法

数据库的配置内容差不多

图片

存在差异区别的是,插入的sql语句在payload中,插入成功的响应信息比较简单

图片

附上流程demo可直接导入 欢迎批评指正。

[
    {
        "id": "7f4134ce9b2ef83c",
        "type": "tab",
        "label": "流程 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "f4be8f82c8c02d38",
        "type": "inject",
        "z": "7f4134ce9b2ef83c",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "INSERT INTO 'null' USING JYdetected_data TAGS ('null', 'null', 'null') VALUES (NOW(), null)",
        "payloadType": "str",
        "x": 290,
        "y": 380,
        "wires": [
            [
                "f546d519a7c3fbba"
            ]
        ]
    },
    {
        "id": "dfb73bd212f62c71",
        "type": "debug",
        "z": "7f4134ce9b2ef83c",
        "name": "debug 1",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 740,
        "y": 380,
        "wires": []
    },
    {
        "id": "0b267235a21e6e59",
        "type": "tdengine-operator",
        "z": "7f4134ce9b2ef83c",
        "db": "1e4bdd141ec04aca",
        "name": "",
        "x": 520,
        "y": 260,
        "wires": [
            [
                "14858415b5520b1e"
            ]
        ]
    },
    {
        "id": "14858415b5520b1e",
        "type": "debug",
        "z": "7f4134ce9b2ef83c",
        "name": "debug 2",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "false",
        "statusVal": "",
        "statusType": "auto",
        "x": 740,
        "y": 260,
        "wires": []
    },
    {
        "id": "aa4453dbd3b3c7a8",
        "type": "inject",
        "z": "7f4134ce9b2ef83c",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "INSERT INTO 'null' USING JYdetected_data TAGS ('null', 'null', 'null') VALUES (NOW(), null)",
        "payload": "",
        "payloadType": "date",
        "x": 290,
        "y": 260,
        "wires": [
            [
                "0b267235a21e6e59"
            ]
        ]
    },
    {
        "id": "f546d519a7c3fbba",
        "type": "taos-query",
        "z": "7f4134ce9b2ef83c",
        "server": "5d67ed4db40ca2e9",
        "database": "node_red",
        "sql": "",
        "x": 540,
        "y": 380,
        "wires": [
            [
                "dfb73bd212f62c71"
            ]
        ]
    },
    {
        "id": "1e4bdd141ec04aca",
        "type": "TDengineServer",
        "name": "node_red",
        "uri": "ws://root:taosdata@192.71.65.111:6041",
        "connType": "connection-string",
        "host": "192.71.65.111",
        "port": "6041",
        "db": "node_red"
    },
    {
        "id": "5d67ed4db40ca2e9",
        "type": "taos-config",
        "host": "192.71.65.111",
        "port": "6041",
        "username": "root",
        "password": "taosdata"
    },
    {
        "id": "559e487e73af514e",
        "type": "global-config",
        "env": [],
        "modules": {
            "node-red-node-tdengine": "1.0.0",
            "node-red-contrib-tdengine": "0.0.5"
        }
    }
]
posted @ 2025-08-18 14:49  麻洛美  阅读(135)  评论(0)    收藏  举报