English | 简体中文
opengemini-client-go
is a Golang client for OpenGemini
OpenGemini is a cloud-native distributed time series database, find more information here
- Go 1.20+
Install the client library:
go get github.com/openGemini/opengemini-client-go
Import the Client Library:
import "github.com/libgox/addr"
import "github.com/openGemini/opengemini-client-go/opengemini"
Create a Client:
config := &opengemini.Config{
Addresses: []*addr.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()
}
}
}