全部產品
Search
文件中心

Lindorm:通過Lindorm JDBC Driver串連並訪問時序引擎

更新時間:Aug 26, 2026

本文介紹通過Lindorm JDBC Driver串連並訪問時序引擎的具體操作,以及JDBC Driver訪問時序引擎時支援的API介面和方法。

背景資訊

Lindorm時序引擎的JDBC Driver遵循JDBC 4.1規範定義的介面。由於時序引擎自身是一個時序資料庫,所以JDBC Driver中提供的實際上是JDBC 1.0定義的基礎功能集中的一個子集。

前提條件

  • 已安裝Java環境,要求安裝JDK 1.8及以上版本。

  • 已將用戶端IP地址添加至Lindorm白名單,具體操作請參見設定白名單。

  • 已擷取Lindorm時序引擎的串連地址,具體操作請參見查看串連地址。

操作步驟

  1. 通過以下兩種方式安裝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的版本號碼根據需求填寫。

  2. 訪問Lindorm時序引擎。完整的程式碼範例請參考訪問樣本。

    1. 建立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的串連地址,具體文法和串連參數說明請參見查看串連地址。

    2. 建立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))");
    3. 在表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)");
    4. 查詢表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);
      }