0、环境
TDengine服务器版本:Version:2.6.0.14
[root@dev-sthx ~]# taos
Welcome to the TDengine shell from Linux, Client Version:2.6.0.14
Copyright (c) 2022 by TAOS Data, Inc. All rights reserved.
********************* How to Use TAB in TAOS Shell ******************************
* Taos shell supports pressing TAB key to complete word. You can try it. *
* Press TAB key anywhere, You'll get surprise. *
* KEYBOARD SHORTCUT: *
* [ TAB ] ...... Complete the word or show help if no input *
* [ Ctrl + A ] ...... move cursor to [A]head of line *
* [ Ctrl + E ] ...... move cursor to [E]nd of line *
* [ Ctrl + W ] ...... move cursor to line of middle *
* [ Ctrl + L ] ...... clean screen *
* [ Ctrl + K ] ...... clean after cursor *
* [ Ctrl + U ] ...... clean before cursor *
* *
**********************************************************************************
taos>
官方相关文档:
1、介绍
连接器建立连接的方式,TDengine 提供两种:
- 通过 taosAdapter 组件提供的 REST API 建立与 taosd 的连接,这种连接方式下文中简称“REST 连接”
- 通过客户端驱动程序 taosc 直接与服务端程序 taosd 建立连接,这种连接方式下文中简称“原生连接”。
无论使用何种方式建立连接,连接器都提供了相同或相似的 API 操作数据库,都可以执行 SQL 语句,只是初始化连接的方式稍有不同,用户在使用上不会感到什么差别。
关键不同点在于:
2、REST API 连接TDengine
无需安装客户端,直接连接使用即可。
2.1、导入依赖
如果使用 maven 管理项目,只需在 pom.xml 中加入以下依赖。
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>2.0.38</version>
</dependency>
2.2、样例
package com.taos.example;
import java.sql.*;
import java.util.Properties;
import com.taosdata.jdbc.TSDBDriver;
public class RestConnectExample {
public static void main(String[] args) throws SQLException {
// 这里用的是TAOS-RS,端口为6041
String jdbcUrl = "jdbc:TAOS-RS://192.168.xx.xx:6041/test_data?&timezone=UTC-8&charset=UTF-8&locale=en_US.UTF-8&druid.stat.mergeSql=false";
Properties connProps = new Properties();
connProps.setProperty(TSDBDriver.PROPERTY_KEY_USER, "root");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_PASSWORD, "taosdata");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD, "false");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
String sql = "SELECT tbname,value,TIMEDIFF(ts,0) as ts_long FROM test_data.meters WHERE tbname IN ('73654') order by ts limit 5";
Connection restConn =null;
Statement stmt = null;
try {
// 驱动
Class.forName("com.taosdata.jdbc.rs.RestfulDriver");
restConn = DriverManager.getConnection(jdbcUrl, connProps);
stmt = restConn.createStatement();
// query data
ResultSet resultSet = stmt.executeQuery(sql);
while(resultSet.next()){
System.out.println("tbname:"+resultSet.getString(1)+",value:"+resultSet.getString(2)+",ts:"+resultSet.getString(3));
}
}catch (Exception e){
e.printStackTrace();
} finally {
if (stmt != null){
stmt.close();
}
if (restConn != null) {
restConn.close();
}
}
}
}
运行
[admin@kingjw33 module]$ java -jar demo-1.0-SNAPSHOT-jar-with-dependencies.jar com.taos.example.RestConnectExample
tbname:73654,value:0.0,ts:1672502400000
tbname:73654,value:0.0,ts:1672502403000
tbname:73654,value:0.0,ts:1672506001000
tbname:73654,value:0.0,ts:1672509601000
tbname:73654,value:0.0,ts:1672513202000
3、原生JDBC连接TDengine
这里以Linux为例
3.1、下载客户端安装包
这里要按照需要连接的TDengine的服务器的版本进行客户端的选择,我这里的TDengine版本为2.6.0.14,所以选择如下
全部的Linux版本的TDengine客户端下载地址:
3.2、上传解压安装
3.2.1、上传到Linux服务器
[admin@kingjw33 software]$ ll
-rw-rw-r--. 1 admin admin 16465198 12月 12 13:25 TDengine-server-2.6.0.14-Linux-x64.tar.gz
3.2.2、解压
[admin@kingjw33 software]$ tar -zxvf TDengine-server-2.6.0.14-Linux-x64.tar.gz -C /opt/module/
解压软件包之后,会在解压目录下看到以下文件(目录):
- install_client.sh:安装脚本,用于应用驱动程序
- taos.tar.gz:应用驱动安装包
- driver:TDengine 应用驱动 driver
- examples: 各种编程语言的示例程序(c/C#/go/JDBC/MATLAB/python/R) 运行 install_client.sh 进行安装。
3.2.3、安装TDengine客户端
[admin@kingjw33 TDengine-server-2.6.0.14]$ /opt/module/TDengine-server-2.6.0.14/install.sh
Start to install TDengine...
Created symlink from /etc/systemd/system/multi-user.target.wants/taosd.service to /etc/systemd/system/taosd.service.
System hostname is: kingjw33
Enter FQDN:port (like h1.taosdata.com:6030) of an existing TDengine cluster node to join
OR leave it blank to build one:
Enter your email address for priority support or enter empty to skip:
To configure TDengine : edit /etc/taos/taos.cfg
To configure taosadapter (if has) : edit /etc/taos/taosadapter.toml
To start TDengine : sudo systemctl start taosd
To access TDengine : taos -h kingjw33 to login into TDengine server
TDengine is installed successfully!
3.3、配置 taos.cfg
编辑 taos.cfg
文件(默认路径/etc/taos/taos.cfg),将 firstEp
修改为 TDengine 服务器的 End Point,例如:h1.tdengine.com:6030
[admin@kingjw33 TDengine-server-2.6.0.14]$ sudo vim /etc/taos/taos.cfg
修改如下内容:
# first fully qualified domain name (FQDN) for TDengine system
# firstEp hostname:6030
firstEp dev-sthx:6030
# local fully qualified domain name (FQDN)
# fqdn hostname
fqdn dev-sthx
注:其中的 dev-sthx 为TDengine服务器的名称
添加服务器信息到hosts文件
[admin@kingjw33 TDengine-server-2.6.0.14]$ sudo vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
# 本机
192.168.xx.33 kingjw33
# TDengine服务器
192.168.xx.xx dev-sthx
3.4、安装验证
以上安装和配置完成后,并确认 TDengine 服务已经正常启动运行,此时可以执行安装包里带有的 TDengine 命令行程序 taos 进行登录。
3.4.1、进入shell客户端
在 Linux shell 下直接执行 taos
连接到 TDengine 服务,进入到 TDengine CLI 界面,示例如下:
[admin@kingjw33 TDengine-server-2.6.0.14]$ taos
Welcome to the TDengine shell from Linux, Client Version:2.6.0.14
Copyright (c) 2022 by TAOS Data, Inc. All rights reserved.
********************* How to Use TAB in TAOS Shell ******************************
* Taos shell supports pressing TAB key to complete word. You can try it. *
* Press TAB key anywhere, You'll get surprise. *
* KEYBOARD SHORTCUT: *
* [ TAB ] ...... Complete the word or show help if no input *
* [ Ctrl + A ] ...... move cursor to [A]head of line *
* [ Ctrl + E ] ...... move cursor to [E]nd of line *
* [ Ctrl + W ] ...... move cursor to line of middle *
* [ Ctrl + L ] ...... clean screen *
* [ Ctrl + K ] ...... clean after cursor *
* [ Ctrl + U ] ...... clean before cursor *
* *
**********************************************************************************
taos>
3.4.2、查询测试
查询数据库
taos> show databases;
name | created_time | ntables | vgroups | replica | quorum | days | keep | cache(MB) | blocks | minrows | maxrows | wallevel | fsync | comp | cachelast | precision | update | status |
====================================================================================================================================================================================================================================================================================
test | 2022-09-20 11:23:40.842 | 125 | 1 | 1 | 1 | 10 | 3650 | 16 | 6 | 100 | 4096 | 1 | 3000 | 2 | 0 | ms | 0 | ready |
log | 2022-09-19 10:46:08.149 | 552 | 1 | 1 | 1 | 10 | 30 | 1 | 3 | 100 | 4096 | 1 | 3000 | 2 | 0 | us | 0 | ready |
test_data | 2023-02-27 19:40:58.600 | 408827 | 4 | 1 | 1 | 10 | 3650 | 16 | 6 | 100 | 4096 | 1 | 3000 | 2 | 0 | ms | 1 | ready |
Query OK, 3 row(s) in set (0.016741s)
taos>
查表数据
taos> use test_data;
Database changed.
taos> select * from `73654` limit 5;
ts | value |
=================================================
2023-01-01 00:00:00.000 | 0.00000 |
2023-01-01 00:00:03.000 | 0.00000 |
2023-01-01 01:00:01.000 | 0.00000 |
2023-01-01 02:00:01.000 | 0.00000 |
2023-01-01 03:00:02.000 | 0.00000 |
Query OK, 5 row(s) in set (0.124132s)
taos>
注意:这里验证是否成功,一定要到成功查询到数据,有时候客户端版本和服务器的版本不对的时候,前面的安装到连接show databases; 之前都是正常的,但是一查询数据就会有如下的报错:
报错信息:TDengine ERROR (80000216): syntax error near 'unrecognized token: ") order by ts asc ;"'
3.5、样例
导入pom依赖
<dependency>
<groupId>com.taosdata.jdbc</groupId>
<artifactId>taos-jdbcdriver</artifactId>
<version>2.0.38</version>
</dependency>
java代码
package com.taos.example;
import com.taosdata.jdbc.TSDBDriver;
import java.sql.*;
import java.util.Properties;
public class JDBCConnectExample {
public static void main(String[] args) throws SQLException {
// 这里用的是TAOS,端口为6030
String jdbcUrl = "jdbc:TAOS://192.168.xx.xx:6030/test_data?&timezone=UTC-8&charset=UTF-8&locale=en_US.UTF-8&druid.stat.mergeSql=false";
Properties connProps = new Properties();
connProps.setProperty(TSDBDriver.PROPERTY_KEY_USER, "root");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_PASSWORD, "taosdata");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_BATCH_LOAD, "false");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_CHARSET, "UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_LOCALE, "en_US.UTF-8");
connProps.setProperty(TSDBDriver.PROPERTY_KEY_TIME_ZONE, "UTC-8");
String sql = "SELECT tbname,value,TIMEDIFF(ts,0) as ts_long FROM test_data.meters WHERE tbname IN ('73654') order by ts limit 5";
Connection restConn =null;
Statement stmt = null;
try {
// 驱动
Class.forName("com.taosdata.jdbc.TSDBDriver");
restConn = DriverManager.getConnection(jdbcUrl, connProps);
stmt = restConn.createStatement();
// query data
ResultSet resultSet = stmt.executeQuery(sql);
while(resultSet.next()){
System.out.println("tbname:"+resultSet.getString(1)+",value:"+resultSet.getString(2)+",ts:"+resultSet.getString(3));
}
}catch (Exception e){
e.printStackTrace();
} finally {
if (stmt != null){
stmt.close();
}
if (restConn != null) {
restConn.close();
}
}
}
}
打包到linux上测试
[admin@kingjw33 module]$ java -jar demo-1.0-SNAPSHOT-jar-with-dependencies.jar com.taos.example.JDBCConnectExample
tbname:73654,value:0.0,ts:1672502400000
tbname:73654,value:0.0,ts:1672502403000
tbname:73654,value:0.0,ts:1672506001000
tbname:73654,value:0.0,ts:1672509601000
tbname:73654,value:0.0,ts:1672513202000
4、总结
一般的TDengine客户端测试的时候使用REST API就可以了,毕竟安装TDengine的客户端还是挺麻烦的。
还有就是在Linux上安装TDengine客户端的时候要慎重点,装错了版本不好卸载。
有帮助的话帮忙点个赞吧!