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("完成数据库超级表创建");

}

相关推荐
金銀銅鐵4 小时前
为什么枚举类中可以定义抽象方法
java·后端·ai编程
文丑颜不良啊6 小时前
LeetCode1941~1960题目解析
java·leetcode
txxzjmzlh6 小时前
SpringBoot 统⼀功能处理
java·spring boot·后端
nice_lcj5208 小时前
Java Lambda 表达式入门:怎么写、怎么用、常用方法有哪些
java·开发语言·后端·面试
码农小河668 小时前
基于SSM+JSP+MySQL的在线医疗服务系统的设计与实现
java·开发语言·mysql
SL_staff9 小时前
从配置失效到逻辑复用:JVS-Logic 如何构建可治理的业务编排中枢
java·spring·产品
特立独行的猫A9 小时前
一份 Markdown 发十个平台:cspub 一稿多投
java·后端·python
文丑颜不良啊10 小时前
LeetCode1961~1980题目解析
java·leetcode
文丑颜不良啊11 小时前
LeetCode2001~2020题目解析
java·leetcode
用户37215742613511 小时前
Java 批量提取 Word 文档中的图片并保存到本地
java