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;文章来源:https://www.toymoban.com/news/detail-809245.html@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("完成数据库超级表创建");
}文章来源地址https://www.toymoban.com/news/detail-809245.html
到了这里,关于TDengine时序数据库java连接代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!