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