Skip to content

Latest commit

 

History

History
127 lines (103 loc) · 2.83 KB

README_CN.md

File metadata and controls

127 lines (103 loc) · 2.83 KB

opengemini-client-go

License Language version Godoc

English | 简体中文

opengemini-client-go 是一个用 Go 语言编写的 OpenGemini 客户端

设计文档

OpenGemini Client 设计文档

关于 OpenGemini

OpenGemini 是一款云原生分布式时序数据库。获取更多信息,请点击这里

要求

  • Go 1.20+

用法

安装客户端库:

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

引入客户端库:

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

创建客户端:

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

创建数据库:

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

写入单个点:

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)
}

批量写入点:

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)
}

执行查询:

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()
		}
	}
}