TDengine优点:
专门设计用于时序数据:TDengine专门针对时序数据设计,能够高效地存储和管理大规模的时序数据,如传感器数据、监控数据、日志数据等。
高性能:TDengine具有出色的性能,能够处理海量的时序数据,支持高并发的数据写入和查询操作。它采用了多项优化技术,包括列式存储、压缩算法、并行查询等,以提高数据处理效率和吞吐量。
分布式架构:TDengine支持分布式架构,可以部署在多个节点上构建分布式集群,实现数据的水平扩展和负载均衡。这使得TDengine能够处理更大规模的数据存储和处理需求。
实时查询:TDengine支持实时查询和分析,能够快速响应实时数据的查询请求,提供实时数据分析和可视化功能,帮助用户及时发现和解决问题。
开源社区支持:TDengine是开源软件,由一个活跃的开源社区维护和支持。用户可以从GitHub上获取最新的源代码,参与社区讨论,提出问题和建议,共同推动TDengine的发展和改进。
灵活的数据模型:TDengine提供了灵活的数据模型,能够存储不同类型和结构的时序数据,包括数字、文本、标签等,适用于多种应用场景。
TDengine缺点:
场景受限:TDengine主要针对时序数据处理场景,其SQL能力受到这一特定场景的限制,可能不适用于过于复杂的查询需求。
社区成熟度:虽然TDengine有活跃的开源社区支持,但相比于一些更成熟的数据库产品,其社区可能还不够完善,用户可能需要自行解决一些较为边缘或特定的问题。
文档和资料:由于TDengine是一个相对较新的时序数据库产品,其官方文档和相关资料可能还不够丰富,这可能会增加用户学习和使用的难度。
总的来说,TDengine作为一个专门针对时序数据设计的开源时序数据库,具有高性能、分布式架构、实时查询等显著优点,但在一些特定场景下可能存在一定的限制。用户在使用时需要根据自己的需求和技术栈进行综合考虑。
配置连接池:
public static HikariDataSource dataSource;
static {
// 配置连接池
HikariConfig config = new HikariConfig();
config.setJdbcUrl("jdbc:TAOS-RS://192.168.60.155:6041/scada_data");
config.setUsername("root");
config.setPassword("taosdata");
// 设置连接池大小和其他参数
config.setMinimumIdle(10);
config.setMaximumPoolSize(60);
config.setIdleTimeout(60000);
config.setConnectionTimeout(30000);
config.setMaxLifetime(1800000);
// 初始化数据源
dataSource = new HikariDataSource(config);
}
单表造数据
public static List<TaoSEntity> createTenEntities() {
List<TaoSEntity> taoSEntities = new ArrayList<>();
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
for (int i = 0; i < 10; i++) {
String id = "id" + (i + 1);
String tagDesc = "tagDesc_" + (i + 1);
String value = "value_" + (i + 1);
String tagName = "tagName_" + (i + 1);
String replaceTime = "2023-03-02T00:00:" + (i + 1); // 假设ts是时间戳的简化形式
taoSEntities.add(
TaoSEntity.builder()
.ts(format)
.id(id)
.tagDesc(tagDesc)
.tagName(tagName)
.value(value)
.replaceTime(replaceTime)
.build());
}
return taoSEntities;
}
单表批量插入
public static void batchInserts(String eqpCode, List<TaoSEntity> taoSEntities) throws SQLException {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
// 注意:确保你的数据库表名和列名与这里的SQL匹配
String sql = String.format("INSERT INTO `scada_data`.`eqpCode_%s` USING scada_data.master_table TAGS(?) VALUES (?, ?, ?, ?, ? ,?)", eqpCode);
preparedStatement = connection.prepareStatement(sql);
// 启用批处理
connection.setAutoCommit(false);
for (TaoSEntity taoSEntity : taoSEntities) {
preparedStatement.setString(1, taoSEntity.getTagName());
preparedStatement.setString(2, taoSEntity.getTs());
preparedStatement.setString(3, taoSEntity.getId());
preparedStatement.setString(4, taoSEntity.getTagDesc());
preparedStatement.setString(5, taoSEntity.getValue());
preparedStatement.setString(6, taoSEntity.getTagName());
preparedStatement.setString(7, taoSEntity.getReplaceTime());
// 添加到批处理中
preparedStatement.addBatch();
}
// 执行批处理
int[] updateCounts = preparedStatement.executeBatch();
// 提交事务
connection.commit();
// 可以检查updateCounts以验证每条插入语句的结果
} catch (SQLException e) {
// 如果发生异常,回滚事务
if (connection != null) {
connection.rollback();
}
throw e; // 重新抛出异常
} finally {
// 关闭资源,避免泄露
if (preparedStatement != null) {
preparedStatement.close();
}
if (connection != null && !connection.isClosed()) {
connection.close();
}
}
}
运行:
public static void main(String[] args) throws SQLException {
List<TaoSEntity> tenEntities = createTenEntities();
batchInserts("test", tenEntities);
// HashMap<String, List<TaoSEntity>> tenEntitieMap = createTenEntitieMap();
// batchInsertManyTables(tenEntitieMap);
//query();
}
多变插入造数据:
public static HashMap<String, List<TaoSEntity>> createTenEntitieMap() {
HashMap<String, List<TaoSEntity>> stringListHashMap = new HashMap<>();
for (int i = 5000; i < 5002; i++) {
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
List<TaoSEntity> taoSEntities = new ArrayList<>();
for (int j = 0; j < 10; j++) {
String id = "id" + (j + 1);
String tagDesc = "tagDesc_" + (j + 1);
String value = "value_" + (j + 1);
String tagName = "tagName_" + (j + 1);
String replaceTime = "2023-03-02T00:00:" + (j + 1);
taoSEntities.add(
TaoSEntity.builder()
.ts(format)
.id(id)
.tagDesc(tagDesc)
.tagName(tagName)
.value(value)
.replaceTime(replaceTime)
.build());
}
int i1 = 10001 + i;
stringListHashMap.put(String.valueOf(i1), taoSEntities);
}
return stringListHashMap;
}
多表批量插入
public static void batchInsertManyTables(HashMap<String, List<TaoSEntity>> param) throws SQLException {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
String sql = String.format("INSERT INTO `scada_data`.`eqpCode_?` USING scada_data.master_table TAGS(?) VALUES (?, ?, ?, ?, ? ,?)");
preparedStatement = connection.prepareStatement(sql);
// 启用批处理
connection.setAutoCommit(false);
Set<Map.Entry<String, List<TaoSEntity>>> entries = param.entrySet();
for (Map.Entry<String, List<TaoSEntity>> kv : entries) {
String eqpCode = kv.getKey();
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS"));
preparedStatement.setString(1, eqpCode);
for (TaoSEntity taoSEntity : kv.getValue()) {
preparedStatement.setString(2, taoSEntity.getTagName());
preparedStatement.setString(3, format);
preparedStatement.setString(4, taoSEntity.getId());
preparedStatement.setString(5, taoSEntity.getTagDesc());
preparedStatement.setString(6, taoSEntity.getValue());
preparedStatement.setString(7, taoSEntity.getTagName());
preparedStatement.setString(8, taoSEntity.getReplaceTime());
// 添加到批处理中
preparedStatement.addBatch();
}
}
// 执行批处理
int[] updateCounts = preparedStatement.executeBatch();
// 提交事务
connection.commit();
// 可以检查updateCounts以验证每条插入语句的结果
} catch (SQLException e) {
// 如果发生异常,回滚事务
if (connection != null) {
connection.rollback();
}
throw e; // 重新抛出异常
} finally {
// 关闭资源,避免泄露
if (preparedStatement != null) {
preparedStatement.close();
}
}
}
运行:
public static void main(String[] args) throws SQLException {
// List<TaoSEntity> tenEntities = createTenEntities();
// batchInserts("test", tenEntities);
HashMap<String, List<TaoSEntity>> tenEntitieMap = createTenEntitieMap();
batchInsertManyTables(tenEntitieMap);
//query();
}