Skip to main content
Version: 0.8

Go

{template ingester-lib-introduction%

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

%}

{template ingester-lib-installation%

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

go get -u github.com/GreptimeTeam/greptimedb-ingester-go@<%go-sdk-version%>

Import the library in your code:

import (
greptime "github.com/GreptimeTeam/greptimedb-ingester-go"
"github.com/GreptimeTeam/greptimedb-ingester-go/table"
"github.com/GreptimeTeam/greptimedb-ingester-go/table/types"
)

%}

{template ingester-lib-connect%

cfg := greptime.NewConfig("127.0.0.1").
// change the database name to your database name
WithDatabase("public").
// Default port 4001
// WithPort(4001).
// Enable secure connection if your server is secured by TLS
// WithInsecure(false).
// set authentication information
WithAuth("username", "password")

cli, _ := greptime.NewClient(cfg)

%}

{template low-level-object%

// Construct the table schema for CPU metrics
cpuMetric, err := table.New("cpu_metric")
if err != nil {
// Handle error appropriately
}

// Add a 'Tag' column for host identifiers
cpuMetric.AddTagColumn("host", types.STRING)
// Add a 'Timestamp' column for recording the time of data collection
cpuMetric.AddTimestampColumn("ts", types.TIMESTAMP_MILLISECOND)
// Add 'Field' columns for user and system CPU usage measurements
cpuMetric.AddFieldColumn("cpu_user", types.FLOAT)
cpuMetric.AddFieldColumn("cpu_sys", types.FLOAT)

// Insert example data
// NOTE: The arguments must be in the same order as the columns in the defined schema: host, ts, cpu_user, cpu_sys
err = cpuMetric.AddRow("127.0.0.1", time.Now(), 0.1, 0.12)
err = cpuMetric.AddRow("127.0.0.1", time.Now(), 0.11, 0.13)
if err != nil {
// Handle error appropriately
}

%}

{template create-rows%

cpuMetric, err := table.New("cpu_metric")
if err != nil {
// Handle error appropriately
}
cpuMetric.AddTagColumn("host", types.STRING)
cpuMetric.AddTimestampColumn("ts", types.TIMESTAMP_MILLISECOND)
cpuMetric.AddFieldColumn("cpu_user", types.FLOAT)
cpuMetric.AddFieldColumn("cpu_sys", types.FLOAT)
err = cpuMetric.AddRow("127.0.0.1", time.Now(), 0.1, 0.12)
if err != nil {
// Handle error appropriately
}

memMetric, err := table.New("mem_metric")
if err != nil {
// Handle error appropriately
}
memMetric.AddTagColumn("host", types.STRING)
memMetric.AddTimestampColumn("ts", types.TIMESTAMP_MILLISECOND)
memMetric.AddFieldColumn("mem_usage", types.FLOAT)
err = memMetric.AddRow("127.0.0.1", time.Now(), 112)
if err != nil {
// Handle error appropriately
}

%}

{template insert-rows%

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

%}

{template streaming-insert%

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

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.

affected, err := cli.CloseStream(ctx)

%}

{template high-level-style-object%

type CpuMetric struct {
Host string `greptime:"tag;column:host;type:string"`
CpuUser float64 `greptime:"field;column:cpu_user;type:float64"`
CpuSys float64 `greptime:"field;column:cpu_sys;type:float64"`
Ts time.Time `greptime:"timestamp;column:ts;type:timestamp;precision:millisecond"`
}

func (CpuMetric) TableName() string {
return "cpu_metric"
}

cpuMetrics := []CpuMetric{
{
Host: "127.0.0.1",
CpuUser: 0.10,
CpuSys: 0.12,
Ts: time.Now(),
}
}

%}

{template high-level-style-insert-data%

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

%}

{template high-level-style-streaming-insert%

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.

affected, err := cli.CloseStream(ctx)

%}

{template more-ingestion-examples%

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

%}

{template ingester-lib-reference%

%}

{template recommended-query-library%

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

%}

{template query-library-installation%

Use the following command to install the GORM library:

go get -u gorm.io/gorm

and install the MySQL driver as the example:

go get -u gorm.io/driver/mysql

Then import the libraries in your code:

import (
"gorm.io/gorm"
"gorm.io/driver/mysql"
)

%}

{template query-library-connect%

type Mysql struct {
Host string
Port string
User string
Password string
Database string

DB *gorm.DB
}

m := &Mysql{
Host: "127.0.0.1",
Port: "4002", // default port for MySQL
User: "username",
Password: "password",
Database: "public",
}

dsn := fmt.Sprintf("tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local",
m.Host, m.Port, m.Database)
dsn = fmt.Sprintf("%s:%s@%s", m.User, m.Password, dsn)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
//error handling
}
m.DB = db

%}

{template query-library-raw-sql%

The following code declares a GORM object model:

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

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

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

Declare the table name as follows:

func (CpuMetric) TableName() string {
return "cpu_metric"
}

Use raw SQL to query data:

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

%}

{template query-lib-doc-link%

GORM

%}