This topic describes how to write data to LindormTSDB based on Java Database Connectivity (JDBC).
Develop applications to perform efficient data write operations based on JDBC
If you use JDBC to develop applications, we recommend that you use a PreparedStatement
object in JDBC. This way, you can insert multiple records into a database at a time. Perform the following operations:
Use JDBC to create a
connection
object.For information about how to use JDBC to connect to a database, see Use Lindorm JDBC Driver to connect to and use Lindorm TSDB (recommended).
Create a
PreparedStatement
object and specify an INSERT statement that contains the required parameters.The following sample code provides an example on how to create a
PreparedStatement
object based on the air test data provided in the Design a time series table topic:StringBuilder builder = new StringBuilder(); builder.append("INSERT INTO aqm (city, district, id, time, pm2_5, pm10, so2, no2) "); builder.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?)"); PreparedStatement preparedStmt = connection.prepareStatement(builder.toString());
Bind input parameters to the
PreparedStatement
object and execute the INSERT statement to write data specified by the input parameters to the database.The following sample code provides an example on how to bind input parameters in the INSERT statement in the previous step to the PreparedStatement object:
for (int i = 0; i < batchSize; i++) { // Bind the parameters to the object in the order in which the column parameters are specified in the INSERT statement. // The parameters such as city, district, id, ts, and pm25 in the following code are local variables that are used to bind parameters: preparedStmt.setString(1, city); preparedStmt.setString(2, district); preparedStmt.setString(3, id); preparedStmt.setTimestamp(4, ts); preparedStmt.setDouble(5, pm25); preparedStmt.setDouble(6, pm10); preparedStmt.setDouble(7, so2); preparedStmt.setDouble(8, no2); // Add the parameters to a batch. preparedStmt.addBatch(); } // Write data specified in the batch to the database. int[] results = preparedStmt.executeBatch(); // Query the number of written data records from results. The remaining code is skipped. }
When you use JDBC in LindormTSDB, you can use a
PreparedStatement
object to configure parameters only by using the question mark (?) placeholder. When you call the API operations that are provided byPreparedStatement
to bind parameters, make sure that a value that corresponds to a parameter index specified forsetXXX()
is consistent with a column parameter that corresponds to a placeholder.If the input parameters are bound in batches before the
executeBatch()
method is called for thePreparedStatement
object, a larger number of batches does not indicate better database performance. The database performance varies based on the definition of the table into which data is inserted. If the table contains only one field column and up to five tag columns and thebatchSize
variable in thePreparedStatement
object is set to 5000, the database provides optimal write performance.