JDBC

注意: 目前的JDBC实现仅是为与第三方工具连接使用的。使用JDBC(尤其是执行插入语句时)无法提供高性能吞吐。 对于Java应用,我们推荐使用JAVA NATIVE APIJDBC (不推荐) - 图1open in new window

依赖

  • JDK >= 1.8
  • Maven >= 3.6

安装方法

在根目录下执行下面的命令:

  1. mvn clean install -pl jdbc -am -Dmaven.test.skip=true

在 MAVEN 中使用 IoTDB JDBC

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.apache.iotdb</groupId>
  4. <artifactId>iotdb-jdbc</artifactId>
  5. <version>1.0.0</version>
  6. </dependency>
  7. </dependencies>

示例代码

本章提供了如何建立数据库连接、执行 SQL 和显示查询结果的示例。

要求您已经在工程中包含了数据库编程所需引入的包和 JDBC class.

注意:为了更快地插入,建议使用 executeBatch()

  1. import java.sql.*;
  2. import org.apache.iotdb.jdbc.IoTDBSQLException;
  3. public class JDBCExample {
  4. /**
  5. * Before executing a SQL statement with a Statement object, you need to create a Statement object using the createStatement() method of the Connection object.
  6. * After creating a Statement object, you can use its execute() method to execute a SQL statement
  7. * Finally, remember to close the 'statement' and 'connection' objects by using their close() method
  8. * For statements with query results, we can use the getResultSet() method of the Statement object to get the result set.
  9. */
  10. public static void main(String[] args) throws SQLException {
  11. Connection connection = getConnection();
  12. if (connection == null) {
  13. System.out.println("get connection defeat");
  14. return;
  15. }
  16. Statement statement = connection.createStatement();
  17. //Create database
  18. try {
  19. statement.execute("CREATE DATABASE root.demo");
  20. }catch (IoTDBSQLException e){
  21. System.out.println(e.getMessage());
  22. }
  23. //SHOW DATABASES
  24. statement.execute("SHOW DATABASES");
  25. outputResult(statement.getResultSet());
  26. //Create time series
  27. //Different data type has different encoding methods. Here use INT32 as an example
  28. try {
  29. statement.execute("CREATE TIMESERIES root.demo.s0 WITH DATATYPE=INT32,ENCODING=RLE;");
  30. }catch (IoTDBSQLException e){
  31. System.out.println(e.getMessage());
  32. }
  33. //Show time series
  34. statement.execute("SHOW TIMESERIES root.demo");
  35. outputResult(statement.getResultSet());
  36. //Show devices
  37. statement.execute("SHOW DEVICES");
  38. outputResult(statement.getResultSet());
  39. //Count time series
  40. statement.execute("COUNT TIMESERIES root");
  41. outputResult(statement.getResultSet());
  42. //Count nodes at the given level
  43. statement.execute("COUNT NODES root LEVEL=3");
  44. outputResult(statement.getResultSet());
  45. //Count timeseries group by each node at the given level
  46. statement.execute("COUNT TIMESERIES root GROUP BY LEVEL=3");
  47. outputResult(statement.getResultSet());
  48. //Execute insert statements in batch
  49. statement.addBatch("insert into root.demo(timestamp,s0) values(1,1);");
  50. statement.addBatch("insert into root.demo(timestamp,s0) values(2,15);");
  51. statement.addBatch("insert into root.demo(timestamp,s0) values(2,17);");
  52. statement.addBatch("insert into root.demo(timestamp,s0) values(4,12);");
  53. statement.executeBatch();
  54. statement.clearBatch();
  55. //Full query statement
  56. String sql = "select * from root.demo";
  57. ResultSet resultSet = statement.executeQuery(sql);
  58. System.out.println("sql: " + sql);
  59. outputResult(resultSet);
  60. //Exact query statement
  61. sql = "select s0 from root.demo where time = 4;";
  62. resultSet= statement.executeQuery(sql);
  63. System.out.println("sql: " + sql);
  64. outputResult(resultSet);
  65. //Time range query
  66. sql = "select s0 from root.demo where time >= 2 and time < 5;";
  67. resultSet = statement.executeQuery(sql);
  68. System.out.println("sql: " + sql);
  69. outputResult(resultSet);
  70. //Aggregate query
  71. sql = "select count(s0) from root.demo;";
  72. resultSet = statement.executeQuery(sql);
  73. System.out.println("sql: " + sql);
  74. outputResult(resultSet);
  75. //Delete time series
  76. statement.execute("delete timeseries root.demo.s0");
  77. //close connection
  78. statement.close();
  79. connection.close();
  80. }
  81. public static Connection getConnection() {
  82. // JDBC driver name and database URL
  83. String driver = "org.apache.iotdb.jdbc.IoTDBDriver";
  84. String url = "jdbc:iotdb://127.0.0.1:6667/";
  85. // set rpc compress mode
  86. // String url = "jdbc:iotdb://127.0.0.1:6667?rpc_compress=true";
  87. // Database credentials
  88. String username = "root";
  89. String password = "root";
  90. Connection connection = null;
  91. try {
  92. Class.forName(driver);
  93. connection = DriverManager.getConnection(url, username, password);
  94. } catch (ClassNotFoundException e) {
  95. e.printStackTrace();
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. }
  99. return connection;
  100. }
  101. /**
  102. * This is an example of outputting the results in the ResultSet
  103. */
  104. private static void outputResult(ResultSet resultSet) throws SQLException {
  105. if (resultSet != null) {
  106. System.out.println("--------------------------");
  107. final ResultSetMetaData metaData = resultSet.getMetaData();
  108. final int columnCount = metaData.getColumnCount();
  109. for (int i = 0; i < columnCount; i++) {
  110. System.out.print(metaData.getColumnLabel(i + 1) + " ");
  111. }
  112. System.out.println();
  113. while (resultSet.next()) {
  114. for (int i = 1; ; i++) {
  115. System.out.print(resultSet.getString(i));
  116. if (i < columnCount) {
  117. System.out.print(", ");
  118. } else {
  119. System.out.println();
  120. break;
  121. }
  122. }
  123. }
  124. System.out.println("--------------------------\n");
  125. }
  126. }
  127. }

可以在 url 中指定 version 参数:

  1. String url = "jdbc:iotdb://127.0.0.1:6667?version=V_1_0";

version 表示客户端使用的 SQL 语义版本,用于升级 0.13 时兼容 0.12 的 SQL 语义,可能取值有:V_0_12V_0_13V_1_0