本文介绍通过Lindorm JDBC Driver连接并访问时序引擎的具体操作,以及JDBC Driver访问时序引擎时支持的API接口和方法。
背景信息
Lindorm时序引擎的JDBC Driver遵循JDBC 4.1规范定义的接口。由于时序引擎自身是一个时序数据库,所以JDBC Driver中提供的实际上是JDBC 1.0定义的基础功能集中的一个子集。
前提条件
操作步骤
通过以下两种方式安装Lindorm JDBC Driver依赖。
手动安装。
在本地自行下载JAR包集成JDBC Driver,下载链接为:Lindorm-all-client。选择需要安装的版本,以2.1.5为例,下载lindorm-all-client-2.1.5.jar包。
通过Maven下载。
如果在Maven项目中集成JDBC Driver,创建Project并在pom.xml中添加以下依赖配置,具体内容如下:
<dependency> <groupId>com.aliyun.lindorm</groupId> <artifactId>lindorm-all-client</artifactId> <version>2.2.1.3</version> </dependency>
说明lindorm-all-client的版本号根据需求填写。
访问Lindorm时序引擎。完整的代码示例请参考访问示例。
建立Lindorm时序引擎和数据之间的连接。
String url = "jdbc:lindorm:tsdb:url=http://<host:port>"; Connection conn = DriverManager.getConnection(url);
说明host:port
:Lindorm时序引擎的连接地址和端口,例如:ld-bp17j28j2y7pm****-proxy-tsdb.lindorm.rds.aliyuncs.com:8242
。jdbc:lindorm:tsdb:url=http://<host:port>
:表示JDBC的连接地址,具体语法和连接参数说明请参见查看连接地址。
创建Lindorm时序数据库和表。
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))");
在表sensor中插入数据。
//PreparedStatement 批量写入数据(推荐使用) 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++) { //按INSERT语句中的列参数顺序依次绑定参数 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(); //批量写入数据 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(); //单条写入数据(不推荐使用) Statement stmt = conn.createStatement(); stmt.execute("INSERT INTO sensor(device_id, region, time, temperature, humidity) values('F07A1260','north-cn',1619076780000,12.1,45)");
查询表sensor中的数据。
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); }
文档内容是否对您有帮助?