All Products
Search
Document Center

JDBC-based TSQL queries

Last Updated: Sep 16, 2020

The SQL query engine of TSDB supports the Java Database Connectivity (JDBC) protocol. You can access TSDB from common clients that support JDBC or use TSQL in your Java application to query time series data in TSDB through the JDBC protocol.

In certain TSDB versions, you can view the URL required to access TSDB through JDBC on the Instance Details page of a TSDB instance.

This topic describes how to use TSQL in your Java application to query time series data of TSDB through the JDBC protocol.

I. JDBC connection example

1. Add dependencies for the JDBC driver

Make sure the following requirements are met:

  • Java runtime environment 1.8 is used.
  • You have configured JDBC-based access to the TSDB instance and have obtained the URL for the access.
  • You have configured a blacklist or a whitelist for the TSDB instance to ensure that the application client can access the TSDB instance.

JDBC driver dependencies of TSQL have been published to a Maven repository. The following example uses the Maven repository to manage the tsql_jdbc_app project. You need to add the following content to the pom.xml file.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.alibaba.tsdb.tsql</groupId>
  7. <artifactId>tsql_jdbc_app</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!-- https://mvnrepository.com/artifact/org.apache.drill.exec/drill-jdbc -->
  11. <dependency>
  12. <groupId>org.apache.drill.exec</groupId>
  13. <artifactId>drill-jdbc-all</artifactId>
  14. <version>1.15.0</version>
  15. <exclusions>
  16. <exclusion>
  17. <groupId>org.slf4j</groupId>
  18. <artifactId>log4j-over-slf4j</artifactId>
  19. </exclusion>
  20. </exclusions>
  21. </dependency>
  22. </dependencies>
  23. <build>
  24. <plugins>
  25. <plugin>
  26. <artifactId>maven-assembly-plugin</artifactId>
  27. <configuration>
  28. <archive>
  29. <manifest>
  30. <mainClass>com.alibaba.tsdb.tsql.TsqlJdbcSampleApp</mainClass>
  31. </manifest>
  32. </archive>
  33. <descriptorRefs>
  34. <descriptorRef>jar-with-dependencies</descriptorRef>
  35. </descriptorRefs>
  36. </configuration>
  37. <executions>
  38. <execution>
  39. <id>make-assembly</id> <!-- this is used for inheritance merges -->
  40. <phase>package</phase> <!-- bind to the packaging phase -->
  41. <goals>
  42. <goal>single</goal>
  43. </goals>
  44. </execution>
  45. </executions>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>

After you add the preceding content, you can obtain a .jar file that includes all .jar dependency files and the .class file of your application.

2. Sample code for a JDBC connection

The following provides an example for your reference. You must change the following parameter settings:

  • host: Specify the host name or IP address of your TSDB instance.
  • port: Specify the JDBC port. In Alibaba Cloud TSDB, the default JDBC port is 3306.
  • sql: Specify the TSQL query statement to execute.

In the Java application project, create the com.alibaba.tsdb.tsql package and the TsqlJdbcSampleApp Java source file.Code demo

The example code is as follows:

  1. package com.alibaba.tsdb.tsql;
  2. import java.sql.Connection;
  3. import java.sql.DriverManager;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. public class TsqlJdbcSampleApp {
  8. public static void main(String[] args) throws Exception {
  9. Connection connection = null;
  10. Statement stmt = null;
  11. try {
  12. // step 1: Register JDBC driver
  13. Class.forName("org.apache.drill.jdbc.Driver");
  14. // hostname or address of TSDB instance.
  15. String host = "ts-uf64t3199j58j8251.tsql.hitsdb.rds.aliyuncs.com";
  16. // port for TSQL JDBC service
  17. int port = 3306;
  18. String jdbcUrl = String.format("jdbc:drill:drillbit=%s:%s", host, port);
  19. // step 2: Open connection
  20. System.out.println("Connecting to database @ " + jdbcUrl + " ...");
  21. connection = DriverManager.getConnection(jdbcUrl);
  22. // step 3: Create a statement
  23. System.out.println("Creating statement ...");
  24. stmt = connection.createStatement();
  25. // step 4: Execute a query using the statement.
  26. String sql = "select hostname, `timestamp`, `value` " +
  27. "from tsdb.`cpu.usage_system` " +
  28. "where `timestamp` between '2019-03-01' and '2019-03-01 00:05:00'";
  29. ResultSet rs = stmt.executeQuery(sql);
  30. // step 5: Extract data from ResultSet.
  31. int row = 0;
  32. System.out.println("hostname\ttimestamp\tvalue");
  33. System.out.println("-----------------------------------------------------");
  34. while (rs.next()) {
  35. row++;
  36. System.out.println(rs.getString("hostname") + "\t" + rs.getTimestamp("timestamp") + "\t" +rs.getDouble("value"));
  37. }
  38. System.out.println("-----------------------------------------------------");
  39. System.out.println( row + "rows returned");
  40. } catch(SQLException se){
  41. //Handle errors for JDBC
  42. se.printStackTrace();
  43. }catch(Exception e){
  44. //Handle errors for Class.forName
  45. e.printStackTrace();
  46. }finally{
  47. //finally block used to close resources
  48. try{
  49. if(stmt!=null)
  50. stmt.close();
  51. }catch(SQLException se2){
  52. }// nothing we can do
  53. try{
  54. if(connection!=null)
  55. connection.close();
  56. }catch(SQLException se){
  57. se.printStackTrace();
  58. }//end finally try
  59. }//end try
  60. System.out.println("Goodbye!");
  61. }
  62. }

3. Compilation and execution

In the root directory of the project, run the following Maven command: maven clean install

After the command is executed, you can obtain an executable file named tsql_jdbc_app-1.0-SNAPSHOT-jar-with-dependencies.jar in the /targets directory of the project.

Run the JAR file.

java -jar target/tsql_jdbc_app-1.0-SNAPSHOT-jar-with-dependencies.jar

The following figure shows a part of the query results.TSQL query results based on the JDBC application

Summary: You can perform the preceding steps to compile Java applications to query time series data by using the JDBC protocol.

II. Limits on the use of JDBC

Before you use the JDBC protocol to access a TSDB instance, make sure that you are aware of the following limits:

  1. TSQL supports queries of time series data and metadata, but does not allow you to write, modify, or delete data.
  2. TSDB does not support transactions.

The following table describes the limits on calling JDBC APIs.

API Method TSDB limits
Connection setAutoCommit(boolean) Only allows the value true as the input parameter.
Connection getAutoCommit() Returns the value true.
Connection commit() The following error message appears when you call this method: SQLFeatureNotSupportedException.
Connection rollback() The following error message appears when you call this method: SQLFeatureNotSupportedException.
Connection setTransactionIsolation(int level) Only allows TRANSACTION_NONE as the input parameter.
Connection getTransactionIsolation() Only allows TRANSACTION_NONE as the input parameter.
Connection setSavePoint() The following error message appears when you call this method: SQLFeatureNotSupportedException.
Connection setSavePoint(String name) The following error message appears when you call this method: SQLFeatureNotSupportedException.
Connection rollback(Savepoint savepoint) The following error message appears when you call this method: SQLFeatureNotSupportedException.
Connection releaseSavePoint(Savepoint savepoint) The following error message appears when you call this method: SQLFeatureNotSupportedException.
Connection setNetworkTimeout() The following error message appears when you call this method: SQLFeatureNotSupportedException.
Connection getNetworkTimeout() The following error message appears when you call this method: SQLFeatureNotSupportedException.