Connect with Java
Add Dependency
- Maven
- Spring
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>3.2.9</version>
</dependency>
In the "pom.xml" file, please add the Spring Boot and TDengine Java connector dependencies:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.boot.version>2.5.12</spring.boot.version>
<taos.jdbc.version>3.2.7</taos.jdbc.version>
<druid.version>1.2.8</druid.version>
<mybatis.version>2.2.2</mybatis.version>
</properties>
<dependencies>
<!--spring-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis.version}</version>
</dependency>
<!--connection pool-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>${druid.version}</version>
</dependency>
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>${taos.jdbc.version}</version>
</dependency>
Config
Run this command in your terminal to save the JDBC URL as variable, or if it is a Spring application, you can use the Spring configuration:
- Bash
- CMD
- Powershell
- Spring
export TDENGINE_JDBC_URL="<jdbcURL>"
set TDENGINE_JDBC_URL=<jdbcURL>
$env:TDENGINE_JDBC_URL='<jdbcURL>'
# spring server
server:
port: 8088
spring:
datasource:
driver-class-name: com.taosdata.jdbc.rs.RestfulDriver
url: jdbc:TAOS-RS://example.com?useSSL=true&token=xxxx
# using connection pools
type: com.alibaba.druid.pool.DruidDataSource
druid:
initial-size: 5
min-idle: 5
max-active: 20
max-wait: 60000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
# mybatis
mybatis:
mapper-locations: classpath:mapper/*.xml
Replace <jdbcURL> with real JDBC URL, it will seems like: jdbc:TAOS-RS://example.com?useSSL=true&token=xxxx
.
To obtain the value of JDBC URL, please log in TDengine Cloud and click "Programming" on the left menu, then select "Java".
Connect
- Java
- Spring
Code bellow get JDBC URL from environment variables first and then create a Connection
object, which is a standard JDBC Connection object.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class ConnectCloudExample {
public static void main(String[] args) throws SQLException {
String jdbcUrl = System.getenv("TDENGINE_JDBC_URL");
System.out.println(jdbcUrl);
try(Connection conn = DriverManager.getConnection(jdbcUrl)) {
try(Statement stmt = conn.createStatement()) {
stmt.executeQuery("select server_version()");
}
}
}
}
- Define an interface called "meterMapper", which uses the MyBatis framework to map from TDengine database super table to Java object:
@Mapper
public interface MeterMapper {
@Select("select * from meters limit 10")
List<Meter> find();
int create(@Param("meter")Meter meter, @Param("tableName")String tableName);
int save(@Param("meter")Meter meter, @Param("tableName")String tableName);
Meter lastRow(@Param("tableName")String tableName);
}
- Create a 'meterMapper.xml' file under 'src/main/resources/mapper', and add the following SQL mapping:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.taos.example.dao.MeterMapper">
<resultMap id="Meter" type="com.taos.example.dao.Meter">
<result column="ts" property="ts"/>
<result column="current" property="current"/>
<result column="voltage" property="voltage"/>
<result column="phase" property="phase"/>
<result column="groupId" property="groupid"/>
<result column="location" property="location"/>
</resultMap>
<insert id="create">
CREATE TABLE IF NOT EXISTS #{tableName, jdbcType=VARCHAR} USING meters TAGS
(#{meter.groupId, jdbcType=INTEGER}, #{meter.location, jdbcType=VARCHAR})
</insert>
<insert id="save">
INSERT INTO #{tableName} VALUES (#{meter.ts}, #{meter.current}, #{meter.voltage}, #{meter.phase}) ;
</insert>
<select id="lastRow" resultMap="Meter">
SELECT last_row(*) FROM meters;
</select>
</mapper>
- For more details about how to write or query data from TDngine Cloud instance through Spring, please refer to Spring Example
The client connection is then established. For how to write data and query data, please refer to Insert and Query.
For more details about how to write or query data via REST API, please check REST API.