官网中的高效写入部分文档提供了实例代码,但是关于批处理相关的部分有一些疑问:
在配置驱动参数的时候,需要配置一个TSDBDriver.PROPERTY_KEY_BATCH_SIZE_BY_ROW参数,我理解这个参数是每次客户端驱动向涛思数据库写入数据的条数,按照文档中的说明原理,即客户端程序写入的数据会被驱动底层的数据库连接通过批处理的方式写入到数据库中。但是例子中还有如下部分代码,代码中也使用了PreparedStatement的批处理方法进行数据的存储,这么做的意义何在?因为批处理其实是驱动底层实现的,为什么还要使用PreparedStatement的批处理?
for (int i = 1; i <= NUM_OF_SUB_TABLE; i++) {
pstmt.setString(1, "d_" + i);
pstmt.setInt(2, i);
pstmt.setString(3, "location_" + i);
pstmt.setTimestamp(4, new Timestamp(current + j));
pstmt.setFloat(5, random.nextFloat() * 30);
pstmt.setInt(6, random.nextInt(300));
pstmt.setFloat(7, random.nextFloat());
// when the queue of backend cached data reaches the maximum size, this method
// will be blocked
pstmt.addBatch();
rows++;
}
pstmt.executeBatch();
if (rows % 50000 == 0) {
// The semantics of executeUpdate in efficient writing mode is to synchronously
// retrieve the number of rows written between the previous call and the current
// one.
int affectedRows = pstmt.executeUpdate();
Assert.equals(50000, affectedRows);
}