TDengine Go Connector

driver-go is the official Go language connector for TDengine. It implements the database/sql package, the generic Go language interface to SQL databases. Go developers can use it to develop applications that access TDengine cluster data.

driver-go provides two ways to establish connections. One is native connection, which connects to TDengine instances natively through the TDengine client driver (taosc), supporting data writing, querying, subscriptions, schemaless writing, and bind interface. The other is the REST connection, which connects to TDengine instances via the REST interface provided by taosAdapter. The set of features implemented by the REST connection differs slightly from those implemented by the native connection.

This article describes how to install driver-go and connect to TDengine clusters and perform basic operations such as data query and data writing through driver-go.

The source code of driver-go is hosted on GitHub.

Supported Platforms

Native connections are supported on the same platforms as the TDengine client driver. REST connections are supported on all platforms that can run Go.

Version support

Please refer to version support list

Supported features

Native connections

A “native connection” is established by the connector directly to the TDengine instance via the TDengine client driver (taosc). The supported functional features are:

  • Normal queries
  • Continuous queries
  • Subscriptions
  • schemaless interface
  • parameter binding interface

REST connection

A “REST connection” is a connection between the application and the TDengine instance via the REST API provided by the taosAdapter component. The following features are supported:

  • General queries
  • Continuous queries

Installation steps

Pre-installation

  • Install Go development environment (Go 1.14 and above, GCC 4.8.5 and above)
  • If you use the native connector, please install the TDengine client driver. Please refer to Install Client Driver for specific steps

Configure the environment variables and check the command.

  • go env
  • gcc -v

Use go get to install

  1. go get -u github.com/taosdata/driver-go/v2@latest

Manage with go mod

  1. Initialize the project with the go mod command.
  1. go mod init taos-demo
  1. Introduce taosSql
  1. import (
  2. "database/sql"
  3. _ "github.com/taosdata/driver-go/v2/taosSql"
  4. )
  1. Update the dependency packages with go mod tidy.
  1. go mod tidy
  1. Run the program with go run taos-demo or compile the binary with the go build command.
  1. go run taos-demo
  2. go build

Create a connection

Data source name (DSN)

Data source names have a standard format, e.g. PEAR DB, but no type prefix (square brackets indicate optionally):

  1. [username[:password]@][protocol[(address)]]/[dbname][?param1=value1&... &paramN=valueN]

DSN in full form.

  1. username:password@protocol(address)/dbname?param=value

Connecting via connector

  • native connection
  • REST connection

taosSql implements Go’s database/sql/driver interface via cgo. You can use the database/sql interface by simply introducing the driver.

Use taosSql as driverName and use a correct DSN as dataSourceName, DSN supports the following parameters.

  • configPath specifies the taos.cfg directory

Example.

  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/taosdata/driver-go/v2/taosSql"
  6. )
  7. func main() {
  8. var taosUri = "root:taosdata@tcp(localhost:6030)/"
  9. taos, err := sql.Open("taosSql", taosUri)
  10. if err ! = nil {
  11. fmt.Println("failed to connect TDengine, err:", err)
  12. return
  13. }
  14. }

taosRestful implements Go’s database/sql/driver interface via http client. You can use the database/sql interface by simply introducing the driver.

Use taosRestful as driverName and use a correct DSN as dataSourceName with the following parameters supported by the DSN.

  • disableCompression whether to accept compressed data, default is true do not accept compressed data, set to false if transferring data using gzip compression.
  • readBufferSize The default size of the buffer for reading data is 4K (4096), which can be adjusted upwards when the query result has a lot of data.

Example.

  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/taosdata/driver-go/v2/taosRestful"
  6. )
  7. func main() {
  8. var taosUri = "root:taosdata@http(localhost:6041)/"
  9. taos, err := sql.Open("taosRestful", taosUri)
  10. if err ! = nil {
  11. fmt.Println("failed to connect TDengine, err:", err)
  12. return
  13. }
  14. }

Usage examples

Write data

SQL Write

  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/taosdata/driver-go/v2/taosRestful"
  6. )
  7. func createStable(taos *sql.DB) {
  8. _, err := taos.Exec("CREATE DATABASE power")
  9. if err != nil {
  10. fmt.Println("failed to create database, err:", err)
  11. }
  12. _, err = taos.Exec("CREATE STABLE power.meters (ts TIMESTAMP, current FLOAT, voltage INT, phase FLOAT) TAGS (location BINARY(64), groupId INT)")
  13. if err != nil {
  14. fmt.Println("failed to create stable, err:", err)
  15. }
  16. }
  17. func insertData(taos *sql.DB) {
  18. sql := `INSERT INTO power.d1001 USING power.meters TAGS(California.SanFrancisco, 2) VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000)
  19. power.d1002 USING power.meters TAGS(California.SanFrancisco, 3) VALUES ('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000)
  20. power.d1003 USING power.meters TAGS(California.LosAngeles, 2) VALUES ('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000) ('2018-10-03 14:38:16.600', 13.40000, 223, 0.29000)
  21. power.d1004 USING power.meters TAGS(California.LosAngeles, 3) VALUES ('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000) ('2018-10-03 14:38:06.500', 11.50000, 221, 0.35000)`
  22. result, err := taos.Exec(sql)
  23. if err != nil {
  24. fmt.Println("failed to insert, err:", err)
  25. return
  26. }
  27. rowsAffected, err := result.RowsAffected()
  28. if err != nil {
  29. fmt.Println("failed to get affected rows, err:", err)
  30. return
  31. }
  32. fmt.Println("RowsAffected", rowsAffected)
  33. }
  34. func main() {
  35. var taosDSN = "root:taosdata@http(localhost:6041)/"
  36. taos, err := sql.Open("taosRestful", taosDSN)
  37. if err != nil {
  38. fmt.Println("failed to connect TDengine, err:", err)
  39. return
  40. }
  41. defer taos.Close()
  42. createStable(taos)
  43. insertData(taos)
  44. }

view source code

InfluxDB line protocol write

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/taosdata/driver-go/v2/af"
  5. )
  6. func prepareDatabase(conn *af.Connector) {
  7. _, err := conn.Exec("CREATE DATABASE test")
  8. if err != nil {
  9. panic(err)
  10. }
  11. _, err = conn.Exec("USE test")
  12. if err != nil {
  13. panic(err)
  14. }
  15. }
  16. func main() {
  17. conn, err := af.Open("localhost", "root", "taosdata", "", 6030)
  18. if err != nil {
  19. fmt.Println("fail to connect, err:", err)
  20. }
  21. defer conn.Close()
  22. prepareDatabase(conn)
  23. var lines = []string{
  24. "meters,location=California.LosAngeles,groupid=2 current=11.8,voltage=221,phase=0.28 1648432611249",
  25. "meters,location=California.LosAngeles,groupid=2 current=13.4,voltage=223,phase=0.29 1648432611250",
  26. "meters,location=California.LosAngeles,groupid=3 current=10.8,voltage=223,phase=0.29 1648432611249",
  27. "meters,location=California.LosAngeles,groupid=3 current=11.3,voltage=221,phase=0.35 1648432611250",
  28. }
  29. err = conn.InfluxDBInsertLines(lines, "ms")
  30. if err != nil {
  31. fmt.Println("insert error:", err)
  32. }
  33. }

view source code

OpenTSDB Telnet line protocol write

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/taosdata/driver-go/v2/af"
  5. )
  6. func prepareDatabase(conn *af.Connector) {
  7. _, err := conn.Exec("CREATE DATABASE test")
  8. if err != nil {
  9. panic(err)
  10. }
  11. _, err = conn.Exec("USE test")
  12. if err != nil {
  13. panic(err)
  14. }
  15. }
  16. func main() {
  17. conn, err := af.Open("localhost", "root", "taosdata", "", 6030)
  18. if err != nil {
  19. fmt.Println("fail to connect, err:", err)
  20. }
  21. defer conn.Close()
  22. prepareDatabase(conn)
  23. var lines = []string{
  24. "meters.current 1648432611249 10.3 location=California.SanFrancisco groupid=2",
  25. "meters.current 1648432611250 12.6 location=California.SanFrancisco groupid=2",
  26. "meters.current 1648432611249 10.8 location=California.LosAngeles groupid=3",
  27. "meters.current 1648432611250 11.3 location=California.LosAngeles groupid=3",
  28. "meters.voltage 1648432611249 219 location=California.SanFrancisco groupid=2",
  29. "meters.voltage 1648432611250 218 location=California.SanFrancisco groupid=2",
  30. "meters.voltage 1648432611249 221 location=California.LosAngeles groupid=3",
  31. "meters.voltage 1648432611250 217 location=California.LosAngeles groupid=3",
  32. }
  33. err = conn.OpenTSDBInsertTelnetLines(lines)
  34. if err != nil {
  35. fmt.Println("insert error:", err)
  36. }
  37. }

view source code

OpenTSDB JSON line protocol write

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/taosdata/driver-go/v2/af"
  5. )
  6. func prepareDatabase(conn *af.Connector) {
  7. _, err := conn.Exec("CREATE DATABASE test")
  8. if err != nil {
  9. panic(err)
  10. }
  11. _, err = conn.Exec("USE test")
  12. if err != nil {
  13. panic(err)
  14. }
  15. }
  16. func main() {
  17. conn, err := af.Open("localhost", "root", "taosdata", "", 6030)
  18. if err != nil {
  19. fmt.Println("fail to connect, err:", err)
  20. }
  21. defer conn.Close()
  22. prepareDatabase(conn)
  23. payload := `[{"metric": "meters.current", "timestamp": 1648432611249, "value": 10.3, "tags": {"location": "California.SanFrancisco", "groupid": 2}},
  24. {"metric": "meters.voltage", "timestamp": 1648432611249, "value": 219, "tags": {"location": "California.LosAngeles", "groupid": 1}},
  25. {"metric": "meters.current", "timestamp": 1648432611250, "value": 12.6, "tags": {"location": "California.SanFrancisco", "groupid": 2}},
  26. {"metric": "meters.voltage", "timestamp": 1648432611250, "value": 221, "tags": {"location": "California.LosAngeles", "groupid": 1}}]`
  27. err = conn.OpenTSDBInsertJsonPayload(payload)
  28. if err != nil {
  29. fmt.Println("insert error:", err)
  30. }
  31. }

view source code

Query data

  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "time"
  6. _ "github.com/taosdata/driver-go/v2/taosRestful"
  7. )
  8. func main() {
  9. var taosDSN = "root:taosdata@http(localhost:6041)/power"
  10. taos, err := sql.Open("taosRestful", taosDSN)
  11. if err != nil {
  12. fmt.Println("failed to connect TDengine, err:", err)
  13. return
  14. }
  15. defer taos.Close()
  16. rows, err := taos.Query("SELECT ts, current FROM meters LIMIT 2")
  17. if err != nil {
  18. fmt.Println("failed to select from table, err:", err)
  19. return
  20. }
  21. defer rows.Close()
  22. for rows.Next() {
  23. var r struct {
  24. ts time.Time
  25. current float32
  26. }
  27. err := rows.Scan(&r.ts, &r.current)
  28. if err != nil {
  29. fmt.Println("scan error:\n", err)
  30. return
  31. }
  32. fmt.Println(r.ts, r.current)
  33. }
  34. }

view source code

More sample programs

Usage limitations

Since the REST interface is stateless, the use db syntax will not work. You need to put the db name into the SQL command, e.g. create table if not exists tb1 (ts timestamp, a int) to create table if not exists test.tb1 (ts timestamp, a int) otherwise it will report the error [0x217] Database not specified or available.

You can also put the db name in the DSN by changing root:taosdata@http(localhost:6041)/ to root:taosdata@http(localhost:6041)/test. This method is supported by taosAdapter since TDengine 2.4.0.5. Executing the create database statement when the specified db does not exist will not report an error while executing other queries or writing against that db will report an error.

The complete example is as follows.

  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. "time"
  6. _ "github.com/taosdata/driver-go/v2/taosRestful"
  7. )
  8. func main() {
  9. var taosDSN = "root:taosdata@http(localhost:6041)/test"
  10. taos, err := sql.Open("taosRestful", taosDSN)
  11. if err != nil {
  12. fmt.Println("failed to connect TDengine, err:", err)
  13. return
  14. }
  15. defer taos.Close()
  16. taos.Exec("create database if not exists test")
  17. taos.Exec("create table if not exists tb1 (ts timestamp, a int)")
  18. _, err = taos.Exec("insert into tb1 values(now, 0)(now+1s,1)(now+2s,2)(now+3s,3)")
  19. if err != nil {
  20. fmt.Println("failed to insert, err:", err)
  21. return
  22. }
  23. rows, err := taos.Query("select * from tb1")
  24. if err != nil {
  25. fmt.Println("failed to select from table, err:", err)
  26. return
  27. }
  28. defer rows.Close()
  29. for rows.Next() {
  30. var r struct {
  31. ts time.Time
  32. a int
  33. }
  34. err := rows.Scan(&r.ts, &r.a)
  35. if err != nil {
  36. fmt.Println("scan error:\n", err)
  37. return
  38. }
  39. fmt.Println(r.ts, r.a)
  40. }
  41. }

Frequently Asked Questions

  1. bind interface in database/sql crashes

    REST does not support parameter binding related interface. It is recommended to use db.Exec and db.Query.

  2. error [0x217] Database not specified or available after executing other statements with use db statement

    The execution of SQL command in the REST interface is not contextual, so using use db statement will not work, see the usage restrictions section above.

  3. use taosSql without error but use taosRestful with error [0x217] Database not specified or available

    Because the REST interface is stateless, using the use db statement will not take effect. See the usage restrictions section above.

  4. Upgrade github.com/taosdata/driver-go/v2/taosRestful

    Change the github.com/taosdata/driver-go/v2 line in the go.mod file to github.com/taosdata/driver-go/v2 develop, then execute go mod tidy.

  5. readBufferSize parameter has no significant effect after being increased

    Increasing readBufferSize will reduce the number of syscall calls when fetching results. If the query result is smaller, modifying this parameter will not improve performance significantly. If you increase the parameter value too much, the bottleneck will be parsing JSON data. If you need to optimize the query speed, you must adjust the value based on the actual situation to achieve the best query performance.

  6. disableCompression parameter is set to false when the query efficiency is reduced

    When set disableCompression parameter to false, the query result will be compressed by gzip and then transmitted, so you have to decompress the data by gzip after getting it.

  7. go get command can’t get the package, or timeout to get the package

    Set Go proxy go env -w GOPROXY=https://goproxy.cn,direct.

Common APIs

database/sql API

  • sql.Open(DRIVER_NAME string, dataSourceName string) *DB

    Use This API to open a DB, returning an object of type *DB.

    Go - 图1info

    This API is created successfully without checking permissions, but only when you execute a Query or Exec, and check if user/password/host/port is legal.

  • func (db *DB) Exec(query string, args . .interface{}) (Result, error)

    sql.Open built-in method to execute non-query related SQL.

  • func (db *DB) Query(query string, args ... . interface{}) (*Rows, error)

    sql.Open Built-in method to execute query statements.

Advanced functions (af) API

The af package encapsulates TDengine advanced functions such as connection management, subscriptions, schemaless, parameter binding, etc.

Connection management

  • af.Open(host, user, pass, db string, port int) (*Connector, error)

    This API creates a connection to taosd via cgo.

  • func (conn *Connector) Close() error

    Closes the connection.

Subscribe to

  • func (conn *Connector) Subscribe(restart bool, topic string, sql string, interval time.Duration) (Subscriber, error)

    Subscribe to data.

  • func (s *taosSubscriber) Consume() (driver.Rows, error)

    Consume the subscription data, returning the Rows structure of the database/sql/driver package.

  • func (s *taosSubscriber) Unsubscribe(keepProgress bool)

    Unsubscribe from data.

schemaless

  • func (conn *Connector) InfluxDBInsertLines(lines []string, precision string) error

    Write to influxDB line protocol.

  • func (conn *Connector) OpenTSDBInsertTelnetLines(lines []string) error

    Write OpenTDSB telnet protocol data.

  • func (conn *Connector) OpenTSDBInsertJsonPayload(payload string) error

    Writes OpenTSDB JSON protocol data.

parameter binding

  • func (conn *Connector) StmtExecute(sql string, params *param.Param) (res driver.Result, err error)

    Parameter bound single row insert.

  • func (conn *Connector) StmtQuery(sql string, params *param.Param) (rows driver.Rows, err error)

    Parameter bound query that returns the Rows structure of the database/sql/driver package.

  • `func (conn Connector) InsertStmt() insertstmt.

    Initialize the parameters.

  • func (stmt *InsertStmt) Prepare(sql string) error

    Parameter binding preprocessing SQL statement.

  • func (stmt *InsertStmt) SetTableName(name string) error

    Bind the set table name parameter.

  • func (stmt *InsertStmt) SetSubTableName(name string) error

    Parameter binding to set the sub table name.

  • func (stmt *InsertStmt) BindParam(params []*param.Param, bindType *param.ColumnType) error

    Parameter bind multiple rows of data.

  • func (stmt *InsertStmt) AddBatch() error

    Add to a parameter-bound batch.

  • func (stmt *InsertStmt) Execute() error

    Execute a parameter binding.

  • func (stmt *InsertStmt) GetAffectedRows() int

    Gets the number of affected rows inserted by the parameter binding.

  • func (stmt *InsertStmt) Close() error

    Closes the parameter binding.

API Reference

Full API see driver-go documentation