Go

GreptimeDB uses different client libraries for writing and querying data. You can choose the client library that best suits your needs.

Write data

GreptimeDB provides an ingester library to help you write data. It utilizes the gRPC protocol, which supports schemaless writing and eliminates the need to create tables before writing data. For more information, refer to Automatic Schema Generation.

The Go ingester SDK provided by GreptimeDB is a lightweight, concurrent-safe library that is easy to use with the metric struct.

Installation

Use the following command to install the GreptimeDB client library for Go:

  1. go get -u github.com/GreptimeTeam/[email protected]

Import the library in your code:

  1. import (
  2. greptime "github.com/GreptimeTeam/greptimedb-ingester-go"
  3. "github.com/GreptimeTeam/greptimedb-ingester-go/table"
  4. "github.com/GreptimeTeam/greptimedb-ingester-go/table/types"
  5. )

Connect to database

Username and password are always required to connect to GreptimeDB. For how to set authentication to GreptimeDB, see Authentication. Here we set the username and password when using the library to connect to GreptimeDB.

  1. cfg := greptime.NewConfig("127.0.0.1").
  2. // change the database name to your database name
  3. WithDatabase("public").
  4. // Default port 4001
  5. // WithPort(4001).
  6. // Enable secure connection if your server is secured by TLS
  7. // WithInsecure(false).
  8. // set authentication information
  9. WithAuth("username", "password")
  10. cli, _ := greptime.NewClient(cfg)

Data model

Each row item in a table consists of three types of columns: Tag, Timestamp, and Field. For more information, see Data Model. The types of column values could be String, Float, Int, Timestamp, etc. For more information, see Data Types.

Low-level API

The GreptimeDB low-level API provides a straightforward method to write data to GreptimeDB by adding rows to the table object with a predefined schema.

Create row objects

This following code snippet begins by constructing a table named cpu_metric, which includes columns host, cpu_user, cpu_sys, and ts. Subsequently, it inserts a single row into the table.

The table consists of three types of columns:

  • Tag: The host column, with values of type String.
  • Field: The cpu_user and cpu_sys columns, with values of type Float.
  • Timestamp: The ts column, with values of type Timestamp.
  1. // Construct the table schema for CPU metrics
  2. cpuMetric, err := table.New("cpu_metric")
  3. if err != nil {
  4. // Handle error appropriately
  5. }
  6. // Add a 'Tag' column for host identifiers
  7. cpuMetric.AddTagColumn("host", types.STRING)
  8. // Add a 'Timestamp' column for recording the time of data collection
  9. cpuMetric.AddTimestampColumn("ts", types.TIMESTAMP_MILLISECOND)
  10. // Add 'Field' columns for user and system CPU usage measurements
  11. cpuMetric.AddFieldColumn("cpu_user", types.FLOAT)
  12. cpuMetric.AddFieldColumn("cpu_sys", types.FLOAT)
  13. // Insert example data
  14. // NOTE: The arguments must be in the same order as the columns in the defined schema: host, ts, cpu_user, cpu_sys
  15. err = cpuMetric.AddRow("127.0.0.1", time.Now(), 0.1, 0.12)
  16. err = cpuMetric.AddRow("127.0.0.1", time.Now(), 0.11, 0.13)
  17. if err != nil {
  18. // Handle error appropriately
  19. }

To improve the efficiency of writing data, you can create multiple rows at once to write to GreptimeDB.

  1. cpuMetric, err := table.New("cpu_metric")
  2. if err != nil {
  3. // Handle error appropriately
  4. }
  5. cpuMetric.AddTagColumn("host", types.STRING)
  6. cpuMetric.AddTimestampColumn("ts", types.TIMESTAMP_MILLISECOND)
  7. cpuMetric.AddFieldColumn("cpu_user", types.FLOAT)
  8. cpuMetric.AddFieldColumn("cpu_sys", types.FLOAT)
  9. err = cpuMetric.AddRow("127.0.0.1", time.Now(), 0.1, 0.12)
  10. if err != nil {
  11. // Handle error appropriately
  12. }
  13. memMetric, err := table.New("mem_metric")
  14. if err != nil {
  15. // Handle error appropriately
  16. }
  17. memMetric.AddTagColumn("host", types.STRING)
  18. memMetric.AddTimestampColumn("ts", types.TIMESTAMP_MILLISECOND)
  19. memMetric.AddFieldColumn("mem_usage", types.FLOAT)
  20. err = memMetric.AddRow("127.0.0.1", time.Now(), 112)
  21. if err != nil {
  22. // Handle error appropriately
  23. }

Insert data

The following example shows how to insert rows to tables in GreptimeDB.

  1. resp, err := cli.Write(context.Background(), cpuMetric, memMetric)
  2. if err != nil {
  3. // Handle error appropriately
  4. }
  5. log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())

Streaming insert

Streaming insert is useful when you want to insert a large amount of data such as importing historical data.

  1. err := cli.StreamWrite(context.Background(), cpuMetric, memMetric)
  2. if err != nil {
  3. // Handle error appropriately
  4. }

Close the stream writing after all data has been written. In general, you do not need to close the stream writing when continuously writing data.

  1. affected, err := cli.CloseStream(ctx)

High-level API

The high-level API uses an ORM style object to write data to GreptimeDB. It allows you to create, insert, and update data in a more object-oriented way, providing developers with a friendlier experience. However, it is not as efficient as the low-level API. This is because the ORM style object may consume more resources and time when converting the objects.

Create row objects

  1. type CpuMetric struct {
  2. Host string `greptime:"tag;column:host;type:string"`
  3. CpuUser float64 `greptime:"field;column:cpu_user;type:float64"`
  4. CpuSys float64 `greptime:"field;column:cpu_sys;type:float64"`
  5. Ts time.Time `greptime:"timestamp;column:ts;type:timestamp;precision:millisecond"`
  6. }
  7. func (CpuMetric) TableName() string {
  8. return "cpu_metric"
  9. }
  10. cpuMetrics := []CpuMetric{
  11. {
  12. Host: "127.0.0.1",
  13. CpuUser: 0.10,
  14. CpuSys: 0.12,
  15. Ts: time.Now(),
  16. }
  17. }

Insert data

  1. resp, err := cli.WriteObject(context.Background(), cpuMetrics)
  2. log.Printf("affected rows: %d\n", resp.GetAffectedRows().GetValue())

Streaming insert

Streaming insert is useful when you want to insert a large amount of data such as importing historical data.

  1. err := cli.StreamWriteObject(context.Background(), cpuMetrics)

Close the stream writing after all data has been written. In general, you do not need to close the stream writing when continuously writing data.

  1. affected, err := cli.CloseStream(ctx)

More examples

For fully runnable code snippets and explanations for common methods, see the Examples.

Ingester library reference

Query data

GreptimeDB uses SQL as the main query language and is compatible with MySQL and PostgreSQL. Therefore, we recommend using mature SQL drivers to query data.

We recommend using the GORM library, which is popular and developer-friendly.

Installation

Use the following command to install the GORM library:

  1. go get -u gorm.io/gorm

and install the MySQL driver as the example:

  1. go get -u gorm.io/driver/mysql

Then import the libraries in your code:

  1. import (
  2. "gorm.io/gorm"
  3. "gorm.io/driver/mysql"
  4. )

Connect to database

The following example shows how to connect to GreptimeDB:

  1. type Mysql struct {
  2. Host string
  3. Port string
  4. User string
  5. Password string
  6. Database string
  7. DB *gorm.DB
  8. }
  9. m := &Mysql{
  10. Host: "127.0.0.1",
  11. Port: "4002", // default port for MySQL
  12. User: "username",
  13. Password: "password",
  14. Database: "public",
  15. }
  16. dsn := fmt.Sprintf("tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
  17. m.Host, m.Port, m.Database)
  18. dsn = fmt.Sprintf("%s:%s@%s", m.User, m.Password, dsn)
  19. db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
  20. if err != nil {
  21. //error handling
  22. }
  23. m.DB = db

Raw SQL

We recommend you using raw SQL to experience the full features of GreptimeDB. The following example shows how to use raw SQL to query data.

The following code declares a GORM object model:

  1. type CpuMetric struct {
  2. Host string `gorm:"column:host;primaryKey"`
  3. Ts time.Time `gorm:"column:ts;primaryKey"`
  4. CpuUser float64 `gorm:"column:cpu_user"`
  5. CpuSys float64 `gorm:"column:cpu_sys"`
  6. }

If you are using the ORM API to insert data, you can declare the model with both GORM and Greptime tags.

  1. type CpuMetric struct {
  2. Host string `gorm:"column:host;primaryKey" greptime:"tag;column:host;type:string"`
  3. Ts time.Time `gorm:"column:ts;primaryKey" greptime:"timestamp;column:ts;type:timestamp;precision:millisecond"`
  4. CpuUser float64 `gorm:"column:cpu_user" greptime:"field;column:cpu_user;type:float64"`
  5. CpuSys float64 `gorm:"column:cpu_sys" greptime:"field;column:cpu_sys;type:float64"`
  6. }

Declare the table name as follows:

  1. func (CpuMetric) TableName() string {
  2. return "cpu_metric"
  3. }

Use raw SQL to query data:

  1. var cpuMetric CpuMetric
  2. db.Raw("SELECT * FROM cpu_metric LIMIT 10").Scan(&result)

Query library reference

For more information about how to use the query library, please see the documentation of the corresponding library:

GORM