TDengine3.0全方位安装体验与数据订阅进阶功能实践

这篇具有很好参考价值的文章主要介绍了TDengine3.0全方位安装体验与数据订阅进阶功能实践。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

牛晓青

背景

2021年我曾写了一个专栏,介绍 TDengine2.x 的基础实践以及遇到的问题,2022年初又发布了基于EMQX与TDengine的前后端分离项目实践系列文章,前面这些实践中主要用到了 TDengine 作为时序数据库(Time Series Database)能够高效完成海量时序数据的存储与计算功能,关于 TDengine 的高阶功能,诸如流式计算数据订阅功能并未涉及,因此,这篇文章的内容主要是对最新发布的 TDengine3.x 安装体验以及数据订阅功能的实践。其中,数据订阅的场景为:在一系列的监测电压、电流、温度的时序数据中,一旦发现温度值超过50℃时,进行告警。(我曾使用 TDengine-alert 结合 WebHook 实现过类似需求。)

表设计

Note: 建库建表语句如下(以下是经过简化后的,仅写入两条数据,说明问题即可):

-- ts, 时间戳
-- voltage, 电压
-- currente, 电流
-- temperature, 温度
-- sn, 设备序号
-- city, 时间戳
-- groupid, 分组编号

create database if not exists iot;

-- 超级表
create stable if not exists iot.power(ts timestamp, voltage int, currente float, temperature float) tags(sn int, city nchar(64), groupid int);

-- 子表
create table if not exists iot.device0 using iot.power tags(0, "太原", 0);
create table if not exists iot.device1 using iot.power tags(1, "西安", 1);

-- 模拟数据
insert into iot.device0 values("2022-12-29 17:03:18.734", 1, 1.0, 1.0);
insert into iot.device1 values("2022-12-29 17:03:23.734", 2, 2.0, 2.0);

TDengine3.x

  • 发布说明
    https://github.com/taosdata/TDengine/releases

  • 下载地址
    https://docs.taosdata.com/get-started/package/#!

  • TDengine发布历史及下载链接
    https://docs.taosdata.com/releases/tdengine/#!

我下载的是2022年12月28日刚发布的 3.0.2.2 ,新鲜出炉,香喷喷~

下载安装

TDengine3.x 的下载安装与 2.x 基本一致,除了下载地址更新为 /3.0/ 。这里选用 rpm 方式进行安装。

# 下载
[root@td0 local]# wget https://www.taosdata.com/assets-download/3.0/TDengine-server-3.0.2.2-Linux-x64.rpm
--2022-12-29 03:28:08--  https://www.taosdata.com/assets-download/3.0/TDengine-server-3.0.2.2-Linux-x64.rpm
正在解析主机 www.taosdata.com (www.taosdata.com)... 101.200.125.16
正在连接 www.taosdata.com (www.taosdata.com)|101.200.125.16|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:34081820 (33M) [application/x-redhat-package-manager]
正在保存至: “TDengine-server-3.0.2.2-Linux-x64.rpm”

100%[=========================================>] 34,081,820   993KB/s 用时 25s    

2022-12-29 03:28:34 (1.28 MB/s) - 已保存 “TDengine-server-3.0.2.2-Linux-x64.rpm” [34081820/34081820])

# 安装
[root@td0 local]# rpm -ivh TDengine-server-3.0.2.2-Linux-x64.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:tdengine-3.0.2.2-3.el7           ################################# [100%]
Start to install TDengine...
System hostname is: td0
Enter FQDN:port (like h1.taosdata.com:6030) of an existing TDengine cluster node to join
OR leave it blank to build one:
Enter your email address for priority support or enter empty to skip: 
Created symlink from /etc/systemd/system/multi-user.target.wants/taosd.service to /etc/systemd/system/taosd.service.

To configure TDengine : edit /etc/taos/taos.cfg
To start TDengine     : sudo systemctl start taosd
To access TDengine    : taos -h td0 to login into TDengine server

TDengine is installed successfully!

# 查看状态
[root@td0 local]# systemctl status taosd
● taosd.service - TDengine server service
   Loaded: loaded (/etc/systemd/system/taosd.service; enabled; vendor preset: disabled)
   Active: inactive (dead)

# 启动服务
[root@td0 local]# systemctl start taosd
[root@td0 local]# systemctl status taosd
● taosd.service - TDengine server service
   Loaded: loaded (/etc/systemd/system/taosd.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2022-12-29 03:31:39 EST; 4s ago
  Process: 21678 ExecStartPre=/usr/local/taos/bin/startPre.sh (code=exited, status=0/SUCCESS)
 Main PID: 21684 (taosd)
   CGroup: /system.slice/taosd.service
           ├─21684 /usr/bin/taosd
           └─21694 /usr/bin/udfd -c /etc/taos/

1229 03:31:39 td0 systemd[1]: Starting TDengine server service...
1229 03:31:39 td0 systemd[1]: Started TDengine server service.

# 启动taosadapter(主要实现通过6041端口进行REST访问)
[root@td0 local]# systemctl status taosadapter
● taosadapter.service - TDengine taosAdapter service
   Loaded: loaded (/etc/systemd/system/taosadapter.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@td0 local]# systemctl start taosadapter
[root@td0 local]# systemctl status taosadapter
● taosadapter.service - TDengine taosAdapter service
   Loaded: loaded (/etc/systemd/system/taosadapter.service; disabled; vendor preset: disabled)
   Active: active (running) since 四 2022-12-29 03:32:27 EST; 4s ago
 Main PID: 22455 (taosadapter)
   CGroup: /system.slice/taosadapter.service
           └─22455 /usr/bin/taosadapter

1229 03:32:27 td0 systemd[1]: Started TDengine taosAdapter service.

# 防火墙处理:我这里是虚拟机环境,直接关闭防火墙,实际生产环境只需开放对应端口即可。
[root@td0 local]# firewall-cmd --state
running
[root@td0 local]# systemctl stop firewalld
[root@td0 local]# firewall-cmd --state
not running
[root@td0 local]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

命令行客户端

在服务器上使用 TDengine 自带的命令行客户端验证服务的安装、运行情况。

[root@td0 local]# taos
Welcome to the TDengine Command Line Interface, Client Version:3.0.2.2
Copyright (c) 2022 by TDengine, all rights reserved.

   ******************************  Tab Completion  **********************************
   *   The TDengine CLI supports tab completion for a variety of items,             *
   *   including database names, table names, function names and keywords.          *
   *   The full list of shortcut keys is as follows:                                *
   *    [ TAB ]        ......  complete the current word                            *
   *                   ......  if used on a blank line, display all valid commands  *
   *    [ Ctrl + A ]   ......  move cursor to the st[A]rt of the line               *
   *    [ Ctrl + E ]   ......  move cursor to the [E]nd of the line                 *
   *    [ Ctrl + W ]   ......  move cursor to the middle of the line                *
   *    [ Ctrl + L ]   ......  clear the entire screen                              *
   *    [ Ctrl + K ]   ......  clear the screen after the cursor                    *
   *    [ Ctrl + U ]   ......  clear the screen before the cursor                   *
   **********************************************************************************

Server is Community Edition.

taos> show databases;
              name              |
=================================
 information_schema             |
 performance_schema             |
Query OK, 2 row(s) in set (0.002114s)

远程Windows客户端

我这里远程开发用的是 Win 10 操作系统。注意需要下载与服务器端版本一致的客户端:

https://www.taosdata.com/assets-download/3.0/TDengine-client-3.0.2.2-Windows-x64.exe

TDengine3.0全方位安装体验与数据订阅进阶功能实践
Note: TDengine 默认使用 6030 端口通信,所以在服务器端需要开放 6030TCP 端口。

远程REST客户端

# 测试RESTful远程连接
curl -u root:taosdata -d 'select * from iot.power' td0:6041/rest/sql

TDengine3.0全方位安装体验与数据订阅进阶功能实践

远程REST GUI客户端

tdenginegui Setup 1.0.0 提示:连接失败。。到官方看了一眼,发现有更新!!卸载旧版本,下载安装新版本。

TDengine3.0全方位安装体验与数据订阅进阶功能实践
TDengineGUI.Setup.1.0.3.exe :连接成功。

TDengine3.0全方位安装体验与数据订阅进阶功能实践

数据订阅-场景

在一系列的监测电压、电流、温度的时序数据中,一旦发现温度值超过50℃时,进行告警。

根据场景需求,先通过 taos 客户端创建一个主题: alarm_temperaturecreate topic alarm_temperature as select * from iot.power where temperature >= 50; 这个主题名称在我们实现消费者端时要用到。以下列出了主题相关的管理操作:创建、查看、删除等。

-- 创建主题
taos> create topic alarm_temperature as select * from iot.power where temperature >= 50;
Create OK, 0 row(s) affected (0.009575s)

-- 查看主题
taos> show topics;
           topic_name           |
=================================
 alarm_temperature              |
Query OK, 1 row(s) in set (0.002781s)

taos> create topic alarm_voltage as select * from iot.power where voltage >= 230;
Create OK, 0 row(s) affected (0.002000s)

taos> create topic alarm_currente as select * from iot.power where currente >= 10;
Create OK, 0 row(s) affected (0.002221s)

taos> show topics;
           topic_name           |
=================================
 alarm_currente                 |
 alarm_voltage                  |
 alarm_temperature              |
Query OK, 3 row(s) in set (0.001895s)

-- 查看消费者
taos> show consumers;
Query OK, 0 row(s) in set (0.004082s)

-- 查看订阅信息
taos> show subscriptions;
           topic_name           |         consumer_group         |  vgroup_id  |      consumer_id      |
========================================================================================================
 alarm_temperature              | demo                           |           2 |                  NULL |
 alarm_temperature              | demo                           |           3 |                  NULL |
Query OK, 2 row(s) in set (0.002721s)

-- 删除主题
taos> drop topic alarm_currente;
Drop OK, 0 row(s) affected (0.001335s)

taos> show topics;
           topic_name           |
=================================
 alarm_voltage                  |
 alarm_temperature              |
Query OK, 2 row(s) in set (0.001956s)

数据订阅-消费者

为了降低使用数据订阅的上手复杂度, TDengine 时序数据库(Time Series Database)的数据订阅功能,提供了与 Kafka 一致的接口。

这里的消费者,基于5-TDengine集成SpringBoot,MyBatis,MyBatisPlus中 MyBatisPlus 的实例,进行了改造。除了基本的 CRUD 接口外,重点实践了 TDengine 的数据订阅消费者的实现。

TDengine3.0全方位安装体验与数据订阅进阶功能实践

pom依赖

核心依赖: SpringBootwebmybatis-plustaos-jdbcdriverspring-boot-configuration-processor 以及 lombok

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.17</version>
        </dependency>

        <dependency>
            <groupId>com.taosdata.jdbc</groupId>
            <artifactId>taos-jdbcdriver</artifactId>
            <version>3.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

这里一定注意 taos-jdbcdriver 的版本要与 TDengine 服务端版本一致或兼容。关于版本兼容性说明,见官方这个表(我的服务端 TDengine 版本为:3.0.2.2):

TDengine3.0全方位安装体验与数据订阅进阶功能实践

配置文件

  • application.yml

主要包含数据源、 mybatis-plus 、日志以及自定义的 TDengine 时序数据库(Time Series Database)的数据订阅消费者配置。

spring:
  datasource:
    driver-class-name: com.taosdata.jdbc.TSDBDriver
    url: jdbc:TAOS://td0:6030/iot?charset=UTF-8&locale=en_US.UTF-8&timezone=UTC-8
    username: root
    password: taosdata
    druid:
      initial-size: 5
      min-idle: 5
      max-active: 5
mybatis-plus:
  configuration:
    map-underscore-to-camel-case: false
logging:
  level:
    com:
      taosdata:
        example:
          mybatisplusdemo:
            mapper: debug
td:
  consumer:
    bootstrap-servers: td0:6030
    msg-with-table-name: true
    enable-auto-commit: true
    group-id: demo
    value-deserializer: com.taosdata.example.mybatisplusdemo.deserializer.PowerDeserializer

Note:记得在开发环境配置主机名解析,我这里是 Win10C:\Windows\System32\drivers\etc\hosts

192.168.44.158 td0

TDengine3.0全方位安装体验与数据订阅进阶功能实践

  • TdConfigProperties.java

结合 @ConfigurationProperties 注解实现自定义的 TDengine 的数据订阅消费者配置,将配置与代码解耦。

@ConfigurationProperties(prefix = "td.consumer")
@Component
@Data
public class TdConfigProperties {
    private String bootstrapServers;
    private Boolean msgWithTableName;
    private Boolean enableAutoCommit;
    private String groupId;
    private String valueDeserializer;
}

数据订阅及消费

通过 @PostConstruct 注解,在后端项目启动后自动进行订阅与消费。订阅的实现模型有两种,一种是“推”,即服务器主动将数据发到客户端;另一种是“拉”,即客户端主动向服务器请求数据。 TDengine 时序数据库(Time Series Database)使用的是“拉”模型,因此演示代码中通过一个死循环实现对主题数据的消费(实际生产可考虑其他实现方式)。

@Configuration
@Slf4j
public class SubscribeOnStartUp {
    private static final String TOPIC = "alarm_temperature";

    @Autowired
    private TdConfigProperties tdConfig;

    @Bean
    public Properties config() {
        Properties properties = new Properties();
        properties.setProperty(TMQConstants.BOOTSTRAP_SERVERS, tdConfig.getBootstrapServers());
        properties.setProperty(TMQConstants.MSG_WITH_TABLE_NAME, tdConfig.getMsgWithTableName().toString());
        properties.setProperty(TMQConstants.ENABLE_AUTO_COMMIT, tdConfig.getEnableAutoCommit().toString());
        properties.setProperty(TMQConstants.GROUP_ID, tdConfig.getGroupId());
        properties.setProperty(TMQConstants.VALUE_DESERIALIZER, tdConfig.getValueDeserializer());
        return properties;
    }

    @PostConstruct
    public void subscribe() {
        log.info("PostConstruct");
        // poll data
        TaosConsumer<Power> consumer = null;
        try {
            consumer = new TaosConsumer<>(config());
            consumer.subscribe(Collections.singletonList(TOPIC));
            while (true) {
                ConsumerRecords<Power> powerRecords = consumer.poll(Duration.ofMillis(100));
                for (Power power : powerRecords) {
                    // TODO 告警推送:短信、邮箱、钉钉、企业微信、飞书、WebHook
                    log.info("Consumed: {}", power.toString());
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                if (null != consumer) {
                    consumer.unsubscribe();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

Note:在实际中,当消费了告警数据后,可以进行告警推送:短信、邮箱、钉钉、企业微信、飞书、 WebHook 等,就像下面我们项目中的这样,可选多种告警推送方式。

TDengine3.0全方位安装体验与数据订阅进阶功能实践

告警订阅测试

taos 客户端中模拟数据写入:近3分钟的五条数据,显然,每条数据的温度指标都高于我们设定的阈值:50℃。

taos> insert into device0 values(now - 2m, 220, 5, 50) (now - 90s, 220, 5, 60) (now - 1m, 220, 5, 70) (now - 30s, 220, 5, 80) (now, 220, 5, 90);

在消费者端可以看到,所有的告警数据被成功消费。

TDengine3.0全方位安装体验与数据订阅进阶功能实践

Reference

  • TDengine 3.0.2.0 正式发布,性能、稳定性大幅提升
  • 数据订阅

Source Code

  • Source Code: Github

If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!文章来源地址https://www.toymoban.com/news/detail-418058.html

到了这里,关于TDengine3.0全方位安装体验与数据订阅进阶功能实践的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 全方位解析 pinia

    前言 Vue3已经推出很长时间了,它周边的生态也是越来越完善了。之前我们使用Vue2的时候,Vuex可以说是必备的,它作为一个状态管理工具,给我们带来了极大的方便。Vue3推出后,虽然相对于Vue2很多东西都变了,但是核心的东西还是没有变的,比如说状态管理、路由等等。再

    2024年04月25日
    浏览(36)
  • Kotlin全方位-简单解析

    Kotlin是一种现代化的静态类型编程语言,由JetBrains公司开发。它可以在Java虚拟机(JVM)上运行,并且可以与Java代码无缝地进行互操作。Kotlin旨在提供更简洁、更安全、更具表达力和更高效的编程语言。 Android开发:Kotlin被广泛用于Android应用程序的开发。它可以与Java代码互操

    2024年02月10日
    浏览(38)
  • Java——线程睡眠全方位解析

    在 Java 中,让线程休眠的方法有很多,这些方法大致可以分为两类,一类是设置时间,在一段时间后自动唤醒,而另一个类是提供了一对休眠和唤醒的方法,在线程休眠之后,可以在任意时间对线程进行唤醒。 线程睡眠的方法有以下 5 个: Thread.sleep TimeUnit wait Condition LockSu

    2024年02月04日
    浏览(41)
  • kafka知识点全方位讲解

    Apache Kafka是一个开源消息系统,由Scala写成。是由Apache软件基金会开发的一个开源消息系统项目。 Kafka最初是由LinkedIn开发,并于2011年初开源。2012年10月从Apache Incubator毕业。该项目的目标是为处理实时数据提供一个统一、高通量、低等待的平台。 Kafka是一个分布式消息队列:

    2023年04月25日
    浏览(30)
  • 精彩解读:短链接应用全方位探究

    1. 短链接的定义和原理 短链接是一种将长网址转换为短网址的服务,通过简化网址长度,方便用户分享和传播链接。短链接服务通过将长网址映射到短标识符的方式,实现对原始网址的压缩和简化。用户在访问短链接时,系统会将短链接还原为原始长网址,实现跳转到目标网

    2024年04月08日
    浏览(35)
  • Android Jetpack组件的全方位分析

    Jetpack是一个用于简化Android应用程序开发的工具包,包含了一系列的组件和工具。Jetpack包含了很多组件,如LiveData、ViewModel、Room、Data Binding、Navigation等。 Jetpack组件是一种更高级别的抽象,它们可以提供更简洁、更易于使用的API。支持库是Jetpack组件的底层实现。 基本概念和

    2024年02月11日
    浏览(29)
  • SQL全方位攻略:3.SQL标准

    1.数据库介绍 2.SQL介绍 【免责声明】文章仅供学习交流,观点代表个人,与任何公司无关。 编辑|SQL和数据库技术(ID:SQLplusDB) 为了确保不同厂商数据库系统之间的兼容性和互操作性,用于控制SQL查询的行为和数据存储结构等方面的统一性,由国际组织或者国家标准化组织制定

    2024年02月05日
    浏览(37)
  • ⛳前端进阶:SEO 全方位解决方案

    SEO 代表搜寻引擎最佳化/搜寻引擎优化(英文全名Search Engine Optimization,简称SEO),是指通过了解搜寻引擎的自然排名的算法逻辑,以提高目标网站在有关搜寻引擎内排名的方式。 网站的 SEO 至关重要,它可以让你的网站获得更好的排名和流量,从而提高网站知名度。对于一些盈

    2024年02月09日
    浏览(36)
  • 全方位了解VR全景展示与制作

    引言: 虚拟现实(VR)技术正在以惊人的速度改变我们的生活方式和体验方式。其中,VR全景展示与制作作为虚拟现实的重要应用之一,为用户提供了身临其境的视听体验。  一、了解VR全景展示与制作 1.VR全景展示 VR全景展示是一种通过虚拟现实技术,将用户带入真实或虚构

    2024年02月13日
    浏览(28)
  • 如何对 Spark 进行全方位性能调优?

    日志收集 如果作业执行报错或者速度异常,通常需要查看 Spark 作业日志,Spark 日志通常是排错的唯一根据,更是作业调优的好帮手。查看日志的时候,需要注意的是 Spark 作业是一个分布式执行的过程,所以日志也是分布式的,联想到 Spark 的架构,Spark 的日志也分为两个级别

    2024年02月21日
    浏览(37)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包