TDengine GO 连接器使用示例

TDengine GO连接器支持window和linux系统,由于TDengine GO连接器采用cgo方法,所以在windows环境下使用时,需要有gcc支持,因此,对windows系统,请先准备好如下的编译环境。

一、安装工具

安装msys2

1、下载和安装链接(https://mirror.tuna.tsinghua.edu.cn/help/msys2/),选择一个x86_64的稳定版本完成安装(假如安装目录为c:\msys64);

2、在c:\msys64\msys2_shell.cmd上点右键打开,然后在窗口上点右键, 选择 Options ,更改字符集:Locale选择zh_CN, Character set选择GBK。点击Apply后,save。

3、修改 pacman 配置

编辑 c:\msys64\etc\pacman.d\mirrorlist.mingw32,在文件开头添加:

Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/i686

编辑 c:\msys64\etc\pacman.d\mirrorlist.mingw64,在文件开头添加:

Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/mingw/x86_64

编辑 c:\msys64\etc\pacman.d\mirrorlist.msys,在文件开头添加:

Server = https://mirrors.tuna.tsinghua.edu.cn/msys2/msys/$arch

然后执行 pacman -Sy 刷新软件包数据。如果有秘钥未知的问题,请参考如下博文:https://blog.csdn.net/xfijun/article/details/107810097?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-5-107810097.nonecase&utm_term=pacman%20%E6%97%A0%E6%95%88%E6%88%96%E5%B7%B2%E6%8D%9F%E5%9D%8F%E7%9A%84%E6%95%B0%E6%8D%AE%E5%BA%93

4、安装gcc和make

pacman -S mingw-w64-x86_64-gcc 
pacman -S make

5、配置环境变量

C:\msys64\mingw64\bin C:\msys64\usr\bin 按以上顺序添加到系统变量 path中。

二、安装TDengine

GO连接器依赖于TDengine的动态库,所以,先要安装TDengine的客户端或服务端,都会安装TDengine的动态库。

具体安装指南,请参见官方博客:

https://tdengine.com/2019/08/09/566.html

三、使用示例

在TDengine安装后,在example/go目录下有一个taosdemo.go的样例程序,下面以此为例描述编译、运行步骤。

1、建议安装go 1.13及以上版本;

2、提前设置好go环境变量:

jason@VirtualBox ~$  go env -w GO111MODULE=on
jason@VirtualBox ~$  go env -w GOPROXY=https://goproxy.io,direct

3、进入example/go目录,先后执行如下命令:

jason@VirtualBox ~/TDengine/tests/examples/go$ go mod init demo
jason@VirtualBox ~/TDengine/tests/examples/go$ go build
jason@VirtualBox ~/TDengine/tests/examples/go$ ./demo
============= args parse result: =============
 dbName:               127.0.0.1
 serverPort:           6030
 usr:                  root
 password:             taosdata
 dbName:               test
 tablePrefix:          d
 numOftables:          2
 numOfRecordsPerTable: 10
 numOfRecordsPerReq:   3
 numOfThreads:         1
 startTimestamp:       2020-10-01 08:00:00[1601510400000]
 Please press enter key to continue….
 ======== create database success! ========
 create tables spent duration: 0.035178s
 ======== create super table and child tables success! ========
 insert data spent duration: 0.006006s
 ======== insert data into child tables success! ========
 query sql: select * from test.meters limit 3 offset 0
 ts:2020-10-01 08:00:01.000     current:200.999390  voltage:286     phase:0.162647  location:Beijing    groupid:0
 ts:2020-10-01 08:00:02.000     current:200.415726  voltage:966     phase:0.785816  location:Beijing    groupid:0
 ts:2020-10-01 08:00:03.000     current:200.334427  voltage:587     phase:0.123541  location:Beijing    groupid:0
 query sql: select avg(voltage), min(voltage), max(voltage) from test.d0
 avg(voltage):-107374184.000000     min(voltage):244    max(voltage):966
 query sql: select last(*) from test.meters
 last(ts):2020-10-01 08:00:20.000     last(current):200.497757    last(voltage):170   last(phase):0.141704
 ======== select data success!  ========
 ======== end demo ========