-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinsert.md
116 lines (97 loc) · 2.05 KB
/
insert.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
Inserting
==
```go
package insert
import (
"context"
"fmt"
"strconv"
"time"
gc "github.com/GreptimeTeam/greptimedb-client-go"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
type Monitor struct {
ID int64
Host string
Memory uint64
Cpu float64
Temperature int64
Ts time.Time
}
type Greptime struct {
Host string
Port string
User string
Password string
Database string
Client gc.Client
}
func (g *Greptime) Setup() error {
options := []grpc.DialOption{
grpc.WithTransportCredentials(insecure.NewCredentials()),
}
cfg := gc.NewCfg(g.Host).
WithDatabase(g.Database).
WithAuth(g.User, g.Password).
WithDialOptions(options...)
if len(g.Port) > 0 {
port, err := strconv.Atoi(g.Port)
if err != nil {
return err
}
cfg.WithPort(port)
}
cli, err := gc.NewClient(cfg)
if err != nil {
return err
}
g.Client = *cli
return nil
}
func (g *Greptime) Insert() error {
table := "monitor"
monitor := Monitor{
ID: time.Now().UnixMicro(),
Host: "127.0.0.1",
Ts: time.Now(),
Memory: 21,
Cpu: 0.81,
Temperature: 21,
}
series := gc.Series{}
series.AddTag("id", monitor.ID)
series.AddField("host", monitor.Host)
series.AddField("memory", monitor.Memory)
series.AddField("cpu", monitor.Cpu)
series.AddField("temperature", monitor.Temperature)
series.SetTimestamp(monitor.Ts)
metric := gc.Metric{}
metric.SetTimePrecision(time.Microsecond)
metric.SetTimestampAlias("ts")
metric.AddSeries(series)
req := gc.InsertRequest{}
req.WithTable(table).WithMetric(metric)
reqs := gc.InsertsRequest{}
reqs.Append(req)
resp, err := g.Client.Insert(context.Background(), reqs)
fmt.Println(resp)
return err
}
func main() {
greptimedb := &Greptime{
Host: "127.0.0.1",
Port: "4001",
User: "",
Password: "",
Database: "public",
}
if err := greptimedb.Setup(); err != nil {
panic(err)
}
if err := greptimedb.Insert(); err != nil {
panic(err)
}
fmt.Println("insert success via greptimedb-client")
}
```