LindormTSDB allows you to use the following six methods to write data to LindormTSDB: LindormTSDB SDK for Java, the InfluxDB® line protocol, standard SQL statements, API operations provided by Time Series Database (TSDB), the remote write interface of Prometheus, and Apache Flink. This topic describes how to use these methods to write data to LindormTSDB.
Use LindormTSDB SDK for Java to write data
LindormTSDB SDK for Java is applicable to developers who use Java to develop their applications. For more information about how to use LindormTSDB SDK for Java to write data to LindormTSDB, see Tutorial: Use LindormTSDB SDK for Java to connect to and use LindormTSDB.
LindormTSDB SDK for Java has the following benefits:
You can asynchronously write data to LindormTSDB without the need to wait for data synchronization.
Data is automatically written in batches, which simplifies the development process and improves data write efficiency.
Connections in the pool can be reused. This prevents the risk that a large number of connections need to be created.
Failed requests are automatically retried. You do not need to develop the retry logic in your application.
LindormTSDB SDK for Java uses the custom encoding protocol for data transmission to efficiently encode and decode data.
LindormTSDB SDK for Java provides methods to ensure the security of threads. You do not need to worry about the security of threads when you develop your applications.
Java Native SDK supports three schema constraint policies: strong constraint, weak constraint, and no constraint.
Example
// Specify the endpoint of LindormTSDB.
String url = "http://ld-bp17j28j2y7pm****-proxy-tsdb-pub.lindorm.rds.aliyuncs.com:8242";
// Create a LindormTSDB client. A LindormTSDB client is thread-safe and can be reused. You do not need to create a client for each thread and then shutdown the client after operations are complete.
ClientOptions options = ClientOptions.newBuilder(url).build();
LindormTSDBClient lindormTSDBClient = LindormTSDBFactory.connect(options);
// Create a database and a table.
lindormTSDBClient.execute("CREATE DATABASE demo");
lindormTSDBClient.execute("demo","CREATE TABLE sensor (device_id VARCHAR TAG,region VARCHAR TAG,time BIGINT,temperature DOUBLE,humidity DOUBLE,PRIMARY KEY(device_id))");
// Write data to LindormTSDB.
int numRecords = 10;
List<Record> records = new ArrayList<>(numRecords);
long currentTime = System.currentTimeMillis();
for (int i = 0; i < numRecords; i++) {
Record record = Record
.table("sensor")
.time(currentTime + i * 1000)
.tag("device_id", "F07A1260")
.tag("region", "north-cn")
.addField("temperature", 12.1 + i)
.addField("humidity", 45.0 + i)
.build();
records.add(record);
}
CompletableFuture<WriteResult> future = lindormTSDBClient.write("demo", records);
// Process the data that is asynchronously written.
future.whenComplete((r, ex) -> {
// Process write failures.
if (ex != null) {
System.out.println("Failed to write.");
if (ex instanceof LindormTSDBException) {
LindormTSDBException e = (LindormTSDBException) ex;
System.out.println("Caught an LindormTSDBException, which means your request made it to Lindorm TSDB, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Code: " + e.getCode());
System.out.println("SQL State: " + e.getSqlstate());
System.out.println("Error Message: " + e.getMessage());
} else {
ex.printStackTrace();
}
} else {
System.out.println("Write successfully.");
}
});
// In this example, a simple synchronization operation is performed to process the data that is written to the table.
System.out.println(future.join());For more information about how to obtain the endpoint of LindormTSDB, see View endpoints.
Use the InfluxDB® line protocol to write data
LindormTSDB allows you to use the InfluxDB® line protocol to write data. If your application is not developed by using Java, we recommend that you use the line protocol to write data to LindormTSDB. You can configure the schema constraint policy when you use the line protocol to write data. By default, the schema constraint policy is weak constraint. For more information, see Write data by using the InfluxDB line protocol.
When you use the line protocol to write data to LindormTSDB, the time precision of the written data is converted to millisecond. Therefore, precision loss occurs when data with a time precision higher than millisecond is written to LindormTSDB by using the line protocol.
Example
curl -X POST \
'http://ld-bp1489gr5t*****-proxy-tsdb.lindorm.rds.aliyuncs.com:8242/api/v2/write?precision=ms&db=default' \
-d '
sensor7,device_id=F07A1260,region=north-cn temperature=12.1,humidity=45 1619076780000
sensor7,device_id=F07A1260,region=north-cn temperature=13.2,humidity=47 1619076790000
sensor7,device_id=F07A1260,region=north-cn temperature=10.6,humidity=46 1619076800000
sensor7,device_id=F07A1260,region=north-cn temperature=18.1,humidity=44 1619076780000
sensor7,device_id=F07A1260,region=north-cn temperature=19.7,humidity=44 1619076790000
'Use standard SQL statements to write data
The following table describes the two methods that can be used when you use SQL statements to write data to LindormTSDB.
Method | Scenario | Description | References |
JDBC Driver | Applications developed by using Java | When you use JDBC to write data to LindormTSDB, you must develop code in your application or use Druid to manage the connection pool. | |
HTTP SQL API | Applications developed by using non-Java programming languages | LindormTSDB allows you to use SQL statements to write data by calling HTTP-based API operations. |
Use the API operations provided by TSDB to write data
LindormTSDB is compatible with the API operations of TSDB. If your application uses TSDB to store data, you can use the API operations provided by TSDB to write data to LindormTSDB. The following table describes the two methods that can be used when you use API operations provided by TSDB to write data.
Method | Description | References |
Write multi-value data. | The /api/mput operation is used to write multi-value data. In this case, the default schema constraint policy is no constraint, which indicates that time series tables are not created and SQL statements cannot be used to query data. If you want to use SQL statements to query data, you can manually create time series tables before or after you write data. You can also set the schema constraint policy to weak constraint to allow the automatic creation of time series tables. Note For more information about how to change the schema constraint policy, see Select a schema constraint policy based on the data writing method. |
|
Write single-value data. | The /api/put operation is used to write single-value data. This method can be used to write data to OpenTSDB. In this case, the default schema constraint policy must be no constraint, which indicates that time series tables are not created and SQL statements cannot be directly used to query data. We recommend that you do not use this method to write data to LindormTSDB. |
|
Use the remote write interface of Prometheus to write data
LindormTSDB can be used as the remote storage of Prometheus. After you write data to LindormTSDB by using the remote write interface, you can use PromQL statements to query the data. For more information, see Use Prometheus to connect to and use LindormTSDB.
Use Apache Flink to write data
You can use the Sink connector that integrates LindormTSDB SDK for Java to write data to LindormTSDB. For more information, see Use Flink to write data to LindormTSDB.