Use the Lindorm JDBC Driver to connect to and access LindormTSDB. This guide also covers supported API operations and methods.
Background information
The JDBC Driver for LindormTSDB implements interfaces defined in the JDBC 4.1 specification. As a time series database, LindormTSDB supports a subset of the basic feature set defined in JDBC 1.0.
Prerequisites
Java is installed. Java Development Kit (JDK) 1.8 or later is required.
The client IP address is added to the Lindorm whitelist. For more information, see Set a whitelist.
The connection address for LindormTSDB has been obtained. For more information, see View endpoints.
Procedure
Install the Lindorm JDBC Driver dependency in one of the following ways.
Manual installation.
To integrate the JDBC Driver locally, download the JAR package from Lindorm-all-client. Select the version that you want to install. For example, to install version 2.1.2, download the lindorm-all-client-2.1.2.jar package.
Download using Maven.
To integrate the JDBC Driver in a Maven project, create a project and add the following dependency to the pom.xml file:
<dependency> <groupId>com.aliyun.lindorm</groupId> <artifactId>lindorm-all-client</artifactId> <version>2.2.1.3</version> </dependency>NoteSet the version number for lindorm-all-client to the version you require.
You can access LindormTSDB. For a complete code sample, see Access example.
Establish a connection to LindormTSDB.
String url = "jdbc:lindorm:tsdb:url=http://<host:port>"; Connection conn = DriverManager.getConnection(url);Notehost:port: The connection address and port of LindormTSDB. For example,ld-bp17j28j2y7pm****-proxy-tsdb.lindorm.rds.aliyuncs.com:8242.jdbc:lindorm:tsdb:url=http://<host:port>: The JDBC connection address.
Create a LindormTSDB database and table.
Statement stmt = conn.createStatement(); stmt.execute("CREATE DATABASE test"); stmt.execute("USE test"); stmt.execute("CREATE TABLE sensor (device_id VARCHAR TAG,region VARCHAR TAG,time TIMESTAMP,temperature DOUBLE,humidity DOUBLE,PRIMARY KEY(device_id))");Insert data into the sensor table.
// Bulk write data with PreparedStatement (recommended) PreparedStatement pstmt = conn.prepareStatement("INSERT INTO sensor (device_id, region, time, temperature, humidity) VALUES (?, ?, ?, ?, ?)"); int batchSize = 100; long ts = System.currentTimeMillis(); for (int i = 0; i < batchSize; i++) { // Bind parameters in the order of columns in the INSERT statement pstmt.setString(1, "F07A1260"); pstmt.setString(2, "north-cn"); pstmt.setLong(3, ts + i * 10000); pstmt.setDouble(4, 12.1); pstmt.setLong(5, 45); pstmt.addBatch(); } pstmt.executeBatch(); pstmt.clearBatch(); // Bulk write data Statement stmt = conn.createStatement(); stmt.addBatch("INSERT INTO sensor(device_id, region, time, temperature, humidity) values('F07A1260','north-cn',1619076780000,12.1,45)"); stmt.addBatch("INSERT INTO sensor(device_id, region, time, temperature, humidity) values('F07A1260','north-cn',1619076790000,13.2,47)"); stmt.addBatch("INSERT INTO sensor(device_id, region, time, temperature, humidity) values('F07A1260','north-cn',1619076800000,10.6,46)"); stmt.addBatch("INSERT INTO sensor(device_id, region, time, temperature, humidity) values('F07A1261','south-cn',1619076780000,18.1,44)"); stmt.addBatch("INSERT INTO sensor(device_id, region, time, temperature, humidity) values('F07A1261','south-cn',1619076790000,19.7,44)"); stmt.executeBatch(); stmt.clearBatch(); // Write a single row (not recommended) Statement stmt = conn.createStatement(); stmt.execute("INSERT INTO sensor(device_id, region, time, temperature, humidity) values('F07A1260','north-cn',1619076780000,12.1,45)");Query data from the sensor table.
ResultSet rs = stmt.executeQuery("select device_id, region,time,temperature,humidity from sensor where time >= 1619076780000 and time <= 1619076800000"); while (rs.next()) { String device_id = rs.getString("device_id"); String region = rs.getString("region"); Long time = rs.getLong("time"); Double temperature = rs.getDouble("temperature"); Long humidity = rs.getLong("humidity"); System.out.printf("%s %s %d %f %d\n", device_id, region, time, temperature, humidity); }