forked from openGemini/opengemini-client-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: PennyYoon <525296438@qq.com> Co-authored-by: ZhangJian He <shoothzj@gmail.com>
- Loading branch information
1 parent
b343cec
commit f2d04e4
Showing
2 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
# opengemini-client-go | ||
opengemini-client-go 是一个基于go语言的 OpenGemini 客户端开源库 | ||
|
||
中文简体 | [English](README.md) | ||
|
||
OpenGemini 是华为云开源的一款云原生分布式时序数据库,获取更多关于 OpenGemini 的信息可点击 https://github.com/openGemini/openGemini | ||
|
||
## 要求 | ||
|
||
- Go 1.19 | ||
|
||
## 用法 | ||
|
||
引入客户端库: | ||
|
||
<i><font color=gray>示例使用点引用法,用户可结合具体需要选择适合的引用方式。</font></i> | ||
|
||
```go | ||
import . "github.com/openGemini/opengemini-client-go/opengemini" | ||
``` | ||
|
||
创建客户端: | ||
|
||
```go | ||
config := &Config{ | ||
Addresses: []*Address{ | ||
{ | ||
Host: "127.0.0.1", | ||
Port: 8086, | ||
}, | ||
}, | ||
} | ||
client, err := NewClient(config) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
``` | ||
|
||
创建数据库: | ||
|
||
```go | ||
exampleDatabase := "ExampleDatabase" | ||
err = client.CreateDatabase(exampleDatabase) | ||
if err != nil { | ||
fmt.Println(err) | ||
return | ||
} | ||
``` | ||
|
||
写入单个点: | ||
|
||
```go | ||
exampleMeasurement := "ExampleMeasurement" | ||
point := &Point{} | ||
point.SetMeasurement(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) | ||
} | ||
``` | ||
|
||
批量写入点位: | ||
|
||
```go | ||
exampleMeasurement := "ExampleMeasurement" | ||
bp := &BatchPoints{} | ||
var tagList []string | ||
tagList = append(tagList, "sunny", "rainy", "windy") | ||
for i := 0; i < 10; i++ { | ||
p := &Point{} | ||
p.SetMeasurement(exampleMeasurement) | ||
p.AddTag("Weather", tagList[rand.Int31n(3)]) | ||
p.AddField("Humidity", rand.Int31n(100)) | ||
p.AddField("Temperature", rand.Int31n(40)) | ||
p.SetTime(time.Now()) | ||
bp.AddPoint(p) | ||
time.Sleep(time.Nanosecond) | ||
} | ||
err = client.WriteBatchPoints(exampleDatabase, bp) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
``` | ||
|
||
执行查询: | ||
|
||
```go | ||
q := 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() | ||
} | ||
} | ||
} | ||
``` |