JDBC
注意: 目前的JDBC实现仅是为与第三方工具连接使用的。使用JDBC(尤其是执行插入语句时)无法提供高性能吞吐。 对于Java应用,我们推荐使用JAVA NATIVE API
open 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;
4. public class JDBCExample {
5. /**
6. * Before executing a SQL statement with a Statement object, you need to create a Statement object using the createStatement() method of the Connection object.
7. * After creating a Statement object, you can use its execute() method to execute a SQL statement
8. * Finally, remember to close the 'statement' and 'connection' objects by using their close() method
9. * For statements with query results, we can use the getResultSet() method of the Statement object to get the result set.
10. */
11. public static void main(String[] args) throws SQLException {
12. Connection connection = getConnection();
13. if (connection == null) {
14. System.out.println("get connection defeat");
15. return;
16. }
17. Statement statement = connection.createStatement();
18. //Create database
19. try {
20. statement.execute("CREATE DATABASE root.demo");
21. }catch (IoTDBSQLException e){
22. System.out.println(e.getMessage());
23. }
25. //SHOW DATABASES
26. statement.execute("SHOW DATABASES");
27. outputResult(statement.getResultSet());
29. //Create time series
30. //Different data type has different encoding methods. Here use INT32 as an example
31. try {
32. statement.execute("CREATE TIMESERIES root.demo.s0 WITH DATATYPE=INT32,ENCODING=RLE;");
33. }catch (IoTDBSQLException e){
34. System.out.println(e.getMessage());
35. }
36. //Show time series
37. statement.execute("SHOW TIMESERIES root.demo");
38. outputResult(statement.getResultSet());
39. //Show devices
40. statement.execute("SHOW DEVICES");
41. outputResult(statement.getResultSet());
42. //Count time series
43. statement.execute("COUNT TIMESERIES root");
44. outputResult(statement.getResultSet());
45. //Count nodes at the given level
46. statement.execute("COUNT NODES root LEVEL=3");
47. outputResult(statement.getResultSet());
48. //Count timeseries group by each node at the given level
49. statement.execute("COUNT TIMESERIES root GROUP BY LEVEL=3");
50. outputResult(statement.getResultSet());
53. //Execute insert statements in batch
54. statement.addBatch("insert into root.demo(timestamp,s0) values(1,1);");
55. statement.addBatch("insert into root.demo(timestamp,s0) values(2,15);");
56. statement.addBatch("insert into root.demo(timestamp,s0) values(2,17);");
57. statement.addBatch("insert into root.demo(timestamp,s0) values(4,12);");
58. statement.executeBatch();
59. statement.clearBatch();
61. //Full query statement
62. String sql = "select * from root.demo";
63. ResultSet resultSet = statement.executeQuery(sql);
64. System.out.println("sql: " + sql);
65. outputResult(resultSet);
67. //Exact query statement
68. sql = "select s0 from root.demo where time = 4;";
69. resultSet= statement.executeQuery(sql);
70. System.out.println("sql: " + sql);
71. outputResult(resultSet);
73. //Time range query
74. sql = "select s0 from root.demo where time >= 2 and time < 5;";
75. resultSet = statement.executeQuery(sql);
76. System.out.println("sql: " + sql);
77. outputResult(resultSet);
79. //Aggregate query
80. sql = "select count(s0) from root.demo;";
81. resultSet = statement.executeQuery(sql);
82. System.out.println("sql: " + sql);
83. outputResult(resultSet);
85. //Delete time series
86. statement.execute("delete timeseries root.demo.s0");
88. //close connection
89. statement.close();
90. connection.close();
91. }
93. public static Connection getConnection() {
94. // JDBC driver name and database URL
95. String driver = "org.apache.iotdb.jdbc.IoTDBDriver";
96. String url = "jdbc:iotdb://127.0.0.1:6667/";
97. // set rpc compress mode
98. // String url = "jdbc:iotdb://127.0.0.1:6667?rpc_compress=true";
100. // Database credentials
101. String username = "root";
102. String password = "root";
104. Connection connection = null;
105. try {
106. Class.forName(driver);
107. connection = DriverManager.getConnection(url, username, password);
108. } catch (ClassNotFoundException e) {
109. e.printStackTrace();
110. } catch (SQLException e) {
111. e.printStackTrace();
112. }
113. return connection;
114. }
116. /**
117. * This is an example of outputting the results in the ResultSet
118. */
119. private static void outputResult(ResultSet resultSet) throws SQLException {
120. if (resultSet != null) {
121. System.out.println("--------------------------");
122. final ResultSetMetaData metaData = resultSet.getMetaData();
123. final int columnCount = metaData.getColumnCount();
124. for (int i = 0; i < columnCount; i++) {
125. System.out.print(metaData.getColumnLabel(i + 1) + " ");
126. }
127. System.out.println();
128. while (resultSet.next()) {
129. for (int i = 1; ; i++) {
130. System.out.print(resultSet.getString(i));
131. if (i < columnCount) {
132. System.out.print(", ");
133. } else {
134. System.out.println();
135. break;
136. }
137. }
138. }
139. System.out.println("--------------------------\n");
140. }
141. }
142. }
可以在 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_12、V_0_13、V_1_0。
