batch insert into supertable according to documentation fails
Open
@yu285 is already working on this.
Since Jul 8, 2025.
bug
- Dominant language
- C
- Stars
- 25.1k
- Forks
- 5k
- Avg merge
- 4d 59m
- Merged PRs (30d)
- 7
Description
我的需求是批量给超表插入子表以及数据。数据源是用ws链接。设置异步写入:
@Bean(name = "tdengineDataSource")
public DataSource tdengineDataSource() {
Properties props = new Properties();
props.setProperty("user", jdbcUsername);
props.setProperty("password", jdbcPassword);
props.setProperty("charset", "UTF-8");
props.setProperty("timeZone", "UTC-8");
props.setProperty("enableAutoReconnect", "true");
//异步写入
props.setProperty("asyncWrite", "stmt");
props.setProperty("batchSize", String.valueOf(BATCH_SIZE));
props.setProperty("cacheSize", String.valueOf(CACHE_SIZE));
props.setProperty("backendWriteThreads", String.valueOf(BACKEND_WRITE_THREADS));
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl(jdbcUrl);
dataSource.setDriverClassName("com.taosdata.jdbc.ws.WebSocketDriver");
dataSource.setConnectionProperties(props);
return dataSource;
}
批量写入代码:
public int addPointValue(List<PointDTO> list, String measurement) {
// 插入数据到TDengine表的逻辑
if(ObjectUtil.isEmpty(list)){
log.error("PointDTO list is empty");
return 0;
}
int totalRows = 0;
int batchCount = 0;
String sql = "INSERT INTO ? USING point_model TAGS (?) VALUES (?, ?)";
//开始时间戳
StopWatch stopWatch = new StopWatch();
stopWatch.start();
try (Connection conn = tdengineDataSource.getConnection();
TSWSPreparedStatement ps = conn.prepareStatement(sql).unwrap(TSWSPreparedStatement .class))
{
for (PointDTO p : list) {
//设置table
ps.setTableName("point_model_"+p.getPointId());
//设置tag
ps.setTagString(0, p.getPointId());
//设置columns
ps.setTimestamp(1, new java.sql.Timestamp(p.getTime().getTime()));
ps.setLong(2, p.getValue().longValue());
ps.addBatch();
totalRows++;
batchCount++;
if (totalRows % 1000 == 0) {
ps.executeBatch();
// 设置异步,确保数据落盘
ps.executeUpdate();
batchCount=0;
}
}
if (batchCount > 0) {
ps.executeBatch();
// 设置异步,确保数据落盘
ps.executeUpdate();
}
stopWatch.stop();
log.info("Insert {} rows to TDengine, time elapsed: {} s.", totalRows, stopWatch.getTotalTimeMillis()/1000);
}
return totalRows; // 返回批量插入的条数
}
TSWSPreparedStatement ps = conn.prepareStatement(sql).unwrap(TSWSPreparedStatement .class))这个就报错了。java.sql.SQLException: Unable to unwrap to class com.taosdata.jdbc.ws.TSWSPreparedStatement。
要改成WSEWPreparedStatement,端点看到是:WSEWPreparedStatement。我改成WSEWPreparedStatement又不支持参数绑定了。
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.