TDengine时序数据库java连接代码

yml配置文件

spring:

datasource:

tdengine:

lazy: true

driverClassName: com.taosdata.jdbc.rs.RestfulDriver

url: jdbc:TAOS-RS://127.0.0.1:6041/zjrecharge?timezone=UTC-8&charset=utf-8

username: root

password: taosdata

tdengine:

enabled: true

dbName: zjrecharge

stableCmdLog: cmd_log

时序数据库配类

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;

import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(name = "spring.tdengine.enabled", havingValue = "true")

@Slf4j

public class TdengineConfig {

/**

* TDengine 用户名

*/

@Value("${spring.datasource.tdengine.username}")

private String username;

/**

* TDengine 密码

*/

@Value("${spring.datasource.tdengine.password}")

private String password;

/**

* url

*/

@Value("${spring.datasource.tdengine.url}")

private String jdbcUrl;

/**

* driverClassName

*/

@Value("${spring.datasource.tdengine.driverClassName}")

private String driverClassName;

/**

* TDengine 库名

*/

@Value("${spring.tdengine.dbName}")

private String dbName;

/**

* stable 超级表名

*/

@Value("${spring.tdengine.stableCmdLog}")

private String stableCmdLog;

public String getUsername() {

return username;

}

public String getPassword() {

return password;

}

public String getDbName() {

return dbName;

}

public String getJdbcUrl() {

return jdbcUrl;

}

public String getDriverClassName() {

return driverClassName;

}

public String getStableCmdLog() {

return stableCmdLog;

}

时序数据库初始化类

import com.zzdy.recharge.tdengine.config.TdengineConfig;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.util.Properties;

@Configuration(proxyBeanMethods = false)

public class ApplicationStarted {

private static final Logger LOGGER = LoggerFactory.getLogger(ApplicationStarted.class);

@Autowired

private TdengineConfig dengineConfig;

/**

*

* @Method

* @Description 开始初始化加载系统参数, 创建数据库和超级表

* @author wxy

*/

@Bean

public void initTDengine() {

LOGGER.info("开始创建实时数据库与超级表");

try {
// 创建连接-原生链接

Connection conn = getConnection();
// 创建数据库

createDatabase(conn);
// 创建指令日志超级数据库表

createSTable(conn);

LOGGER.info("完成超级表的创建");

} catch (Exception e) {

LOGGER.error("错误", e.getMessage());

e.printStackTrace();

}

}

/**
* 连接数据库
* @return
* @throws SQLException
*/

public Connection getConnection() throws SQLException {

String jdbcUrl = dengineConfig.getJdbcUrl();

String username = dengineConfig.getUsername();

String password = dengineConfig.getPassword();

String newJdbcUrl = jdbcUrl.substring(0, jdbcUrl.indexOf("/zjrecharge"));

newJdbcUrl += ("?&user=" + username);

newJdbcUrl += ("&password=" + password);

Properties connProps = new Properties();

return DriverManager.getConnection(newJdbcUrl, connProps);

}

/**
* @return
* @Method
* @Description 根据数据库连接自动创建数据库
*/

private void createDatabase(Connection conn) throws SQLException {

// 默认360天

conn.createStatement().execute(String.format(" create database if not exists %s keep 360d;", dengineConfig.getDbName()));

LOGGER.info("完成数据库创建");

}

/**
* 创建超级表
* 指令日志
*
* @throws SQLException
*/

public void createSTable(Connection conn) throws SQLException {

conn.createStatement().execute(

String.format("CREATE STABLE if not exists %s (ts timestamp, remark BINARY(500), gen_power_total float,data BINARY(1000),device_id BINARY(500), " +

"sn BINARY(500),gen_power float,gen_power_to_day float,station_id BINARY(500)) " +

"TAGS (location binary(64));",

dengineConfig.getDbName() + "." + dengineConfig.getStableCmdLog()));

LOGGER.info("完成数据库超级表创建");

}

相关推荐
杨杨杨大侠11 小时前
都能接模型、RAG 和 MCP,Java Agent 框架到底差在哪?
java
Thinker QAQ12 小时前
并发编程(九):BlockingQueue 与 Channel——语言层的原子性、可见性与有序性
java·python·go·并发编程·channel·blockingqueue·message passing
霸道流氓气质13 小时前
RAG 可观测性与生产监控实战:四层遥测模型、五大仪表盘与 OpenTelemetry 全链路追踪的 Java 生产级实现
java·人工智能·python
AOI小白新手上路13 小时前
autodl常用的tar 打包下载(数据集)命令手册:逐段拆解与套用模板
java·大数据·数据库
dishugj15 小时前
系统架构风险点、敏感点和权衡点的概念和区分
java·开发语言
苜蓿muxu15 小时前
常用代码命名规范
java
马优晨15 小时前
JAVA 中 VO 实体类是什么意思,为什么要做这个
java·开发语言·状态模式·java vo·java vo是什么
Baron X16 小时前
Eclipse Temurin 21 (JRE) Alpine 镜像获取、离线导入与推送操作
java·docker·eclipse·temurin
孙浩君16 小时前
我用 Spring Boot + xterm.js 写了个终端风格互动叙事游戏引擎:游戏即文件,上传一个 JSON 就能玩
java