NutzCN Logo
精华 TDengine 时序数据库的 NutzBoot 开发实例
发布于 1368天前 作者 大鲨鱼 2829 次浏览 复制 上一个帖子 下一个帖子
标签:

nutz 及 nutzboot 已支持 TDengine

  • nutz 版本:1.r.68-SNAPSHOT +

  • nutzboot 版本:2.4.2-SNAPSHOT +

  • TDengine 支持版本:2.0.18.0 +

  • nutz 官网:http://nutzam.com/

  • TDengine 官网:https://www.taosdata.com/cn/

基于 nutzboot 开发 TDengine 实例

开发环境

  • 服务端:CentOS 8.2 64 位

  • 客户端:Windows 10 64 位

TDengine 安装及配置

  • 官网下载 rpm 安装包

  • 执行安装 rpm -ivh TDengine-server-2.0.18.0-Linux-x64.rpm

  • 修改配置文件 vi /etc/taos/taos.cfg 加上当前服务器 hostname 主机名

# first fully qualified domain name (FQDN) for TDengine system
firstEp                   wizzer-test:6030
# local fully qualified domain name (FQDN)
fqdn                      wizzer-test
  • 若为默认密码则直接输入 taostaos -h 127.0.0.1 执行数据库创建命令

## Windows 10 hosts 配置 * 修改 ```C:\Windows\System32\drivers\etc\hosts``` * 添加 ```ip wizzer-test``` ## 创建 nutzboot Maven 项目 * pom.xml 文件加入 nutzboot 及 TDengine JDBC 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
4.0.0

<groupId>com.budwk</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <nutzboot.version>2.4.2-SNAPSHOT</nutzboot.version>
    <jaxb-api.version>2.3.1</jaxb-api.version>
    <slf4j.version>1.7.25</slf4j.version>
    <logback.version>1.2.3</logback.version>
    <taos-jdbcdriver.version>2.0.23</taos-jdbcdriver.version>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
</properties>

<dependencies>
    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutzboot-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutzboot-starter-nutz-dao</artifactId>
    </dependency>
    <dependency>
        <groupId>org.nutz</groupId>
        <artifactId>nutzboot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.taosdata.jdbc</groupId>
        <artifactId>taos-jdbcdriver</artifactId>
        <version>${taos-jdbcdriver.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>${logback.version}</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>${logback.version}</version>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.nutz</groupId>
            <artifactId>nutzboot-parent</artifactId>
            <version>${nutzboot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<repositories>
    <repository>
        <id>nutz</id>
        <url>http://jfrog.nutz.cn/artifactory/libs-release</url>
    </repository>
    <repository>
        <id>nutz-snapshots</id>
        <url>http://jfrog.nutz.cn/artifactory/snapshots</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>nutz-snapshots</id>
        <url>http://jfrog.nutz.cn/artifactory/snapshots</url>
        <snapshots>
            <enabled>true</enabled>
            <updatePolicy>always</updatePolicy>
        </snapshots>
        <releases>
            <enabled>false</enabled>
        </releases>
    </pluginRepository>
</pluginRepositories>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
                <compilerArgs>
                    <arg>-parameters</arg>
                </compilerArgs>
                <useIncrementalCompilation>false</useIncrementalCompilation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.nutz.boot</groupId>
            <artifactId>nutzboot-maven-plugin</artifactId>
            <version>${nutzboot.version}</version>
        </plugin>
    </plugins>
</build>


```

  • 创建实体类
/**
 * 注意 TDengine 表及字段名都为小写字母
 */
@Table("iot_dev")
public class Iot_dev implements Serializable {
    private static final long serialVersionUID = 1L;

    @Column
    @Comment("ID")
    @ColDefine(type = ColType.TIMESTAMP)
    private Date ts;

    @Column("devid") //字段名都为小写字母
    @Comment("设备 ID")
    @ColDefine(type = ColType.VARCHAR, width = 32)
    private String devId;

    @Column("devtype") //字段名都为小写字母
    @Comment("设备类型")
    @ColDefine(type = ColType.BINARY, width = 32)
    private String devType;

    @Column
    @Comment("状态")
    @ColDefine(type = ColType.BOOLEAN)
    private Boolean status;

    @Column
    @Comment("读数 1")
    @ColDefine(type = ColType.DOUBLE)
    private Double val1;

    @Column
    @Comment("读数 2")
    @ColDefine(type = ColType.INT)
    private Integer val2;

    @Column
    @Comment("读数 3")
    @ColDefine(type = ColType.INT,width = 3)
    private Integer val3;

    @Column
    @Comment("读数 4")
    @ColDefine(type = ColType.INT,width = 2)
    private Integer val4;

}

完整代码见

https://gitee.com/wizzer/demo/tree/master/nutzboot-tdengine-demo

7 回复

大佬,有nutz mvc的demo吗

| 09-16 14:46:05 [main] INFO  com.alibaba.druid.pool.DruidDataSource -{dataSource-1} inited
| 09-16 14:46:05 [main] DEBUG org.nutz.ioc.aop.impl.DefaultMirrorFactory -Load class org.nutz.dao.impl.NutDao without AOP
| 09-16 14:46:05 [main] DEBUG org.nutz.ioc.impl.NutIoc -Get 'tddataSource'<>
| 09-16 14:46:05 [main] DEBUG org.nutz.dao.jdbc.Jdbcs -Jdbcs init complete
| 09-16 14:46:05 [main] INFO  org.nutz.dao.jdbc.Jdbcs -Get Connection from DataSource for JdbcExpert, if you lock at here, check your database server and configure
java.library.path:C:\Program Files\Java\jdk1.8.0_181\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;E:\python\Scripts\;E:\python\;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;E:\Git\cmd;C:\Program Files\Java\jdk1.8.0_181\bin;E:\mysql-5.7.27-winx64\mysql-5.7.27-winx64\bin;E:\nodejs\;E:\putty\;E:\zabbix\;C:\Users\92140\AppData\Local\Microsoft\WindowsApps;E:\Bandizip\;E:\IntelliJ IDEA 2018.3.6\bin;;C:\Users\92140\AppData\Roaming\npm;E:\IntelliJ IDEA 2018.3.6\plugins\maven\lib\maven3\bin;;.
| 09-16 14:46:05 [Druid-ConnectionPool-Create-883521348] ERROR com.alibaba.druid.pool.DruidDataSource -create connection Error
java.lang.NoSuchMethodError: setTimestamp
	at com.taosdata.jdbc.TSDBJNIConnector.initImp(Native Method)
	at com.taosdata.jdbc.TSDBJNIConnector.init(TSDBJNIConnector.java:74)
	at com.taosdata.jdbc.TSDBDriver.connect(TSDBDriver.java:121)
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1468)
	at com.alibaba.druid.pool.DruidAbstractDataSource.createPhysicalConnection(DruidAbstractDataSource.java:1533)

连接报错了,不知道哪里配的有问题

把demo跑起来,仔细看文章,有demo地址。

不是用的nutzboot,普通的nutz项目集成demo没看到诶

nutzboot的demo跑起来也是报了上面那个错

找到原因了,jdbcdriver 版本和TDengine 服务的版本不匹配导致的

@qq_c047d3c0 TDengine目前版本服务端和客户端需要保持版本号一致,预计3.0以后版本客户端会向下兼容

添加回复
请先登陆
回到顶部