openGemini / opengemini-client-go

CNCF openGemini Go Client Library
https://opengemini.org/
Apache License 2.0
24 stars 17 forks source link
go opengemini

opengemini-client-go

English | 简体中文

License Language version Go report Godoc

opengemini-client-go is a Golang client for OpenGemini

Design Doc

OpenGemini Client Design Doc

About OpenGemini

OpenGemini is a cloud-native distributed time series database, find more information here

Requirements

Usage

Install the client library:

go get github.com/openGemini/opengemini-client-go

Import the Client Library:

import "github.com/openGemini/opengemini-client-go/opengemini"

Create a Client:

config := &opengemini.Config{
    Addresses: []*opengemini.Address{
        {
            Host: "127.0.0.1",
            Port: 8086,
        },
    },
}
client, err := opengemini.NewClient(config)
if err != nil {
    fmt.Println(err)
}

Create a Database:

exampleDatabase := "ExampleDatabase"
err = client.CreateDatabase(exampleDatabase)
if err != nil {
    fmt.Println(err)
    return
}

Write single point:

exampleMeasurement := "ExampleMeasurement"
point := &opengemini.Point{}
point.Measurement = exampleMeasurement
point.AddTag("Weather", "foggy")
point.AddField("Humidity", 87)
point.AddField("Temperature", 25)
err = client.WritePoint(exampleDatabase, point, func(err error) {
    if err != nil {
        fmt.Printf("write point failed for %s", err)
    }
})
if err != nil {
    fmt.Println(err)
}

Write batch points:

exampleMeasurement := "ExampleMeasurement"
var pointList []*opengemini.Point
var tagList []string
tagList = append(tagList, "sunny", "rainy", "windy")
for i := 0; i < 10; i++ {
    p := &opengemini.Point{}
    p.Measurement=exampleMeasurement
    p.AddTag("Weather", tagList[rand.Int31n(3)])
    p.AddField("Humidity", rand.Int31n(100))
    p.AddField("Temperature", rand.Int31n(40))
    p.Time = time.Now()
        pointList = append(pointList,p)
    time.Sleep(time.Nanosecond)
}
err = client.WriteBatchPoints(context.Background(), exampleDatabase, pointList)
if err != nil {
    fmt.Println(err)
}

Do a query:

q := opengemini.Query{
    Database: exampleDatabase,
    Command:  "select * from " + exampleMeasurement,
}
res, err := client.Query(q)
if err != nil {
    fmt.Println(err)
}
for _, r := range res.Results {
    for _, s := range r.Series {
        for _, v := range s.Values {
            for _, i := range v {
                fmt.Print(i)
                fmt.Print(" | ")
            }
            fmt.Println()
        }
    }
}