构建influx包

This commit is contained in:
wyh 2021-08-15 09:29:54 +08:00
parent 71ad327942
commit 5fa497043d
3 changed files with 107 additions and 69 deletions

83
influx/influx.go Normal file
View File

@ -0,0 +1,83 @@
package influx
import (
"log"
"time"
client "github.com/influxdata/influxdb1-client/v2"
"myschools.me/wyh/influx-demo.git/model"
)
const (
//MyDB 数据库名
MyDB = "wyh"
username = ""
password = ""
)
//ConnInflux 连接influxDB
func ConnInflux() client.Client {
cli, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://127.0.0.1:8086",
Username: username,
Password: password,
})
if err != nil {
log.Fatal(err)
}
return cli
}
//WritesPoints 写入数据
func WritesPoints(points *model.Influx, cli client.Client) {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: MyDB,
Precision: "s",
})
if err != nil {
log.Fatal(err)
}
log.Println(points.TableName)
log.Println(points.Tags)
log.Println(points.Fields)
// tags := map[string]string{"cpu": "ih-cpu"}
// fields := map[string]interface{}{
// "idle": 20.1,
// "system": 43.3,
// "user": 86.6,
// }
pt, err := client.NewPoint(
// "cpu_usage",
points.TableName,
points.Tags,
points.Fields,
time.Now(),
)
if err != nil {
log.Fatal(err)
}
bp.AddPoint(pt)
if err := cli.Write(bp); err != nil {
log.Fatal(err)
}
}
//QueryDB 查询数据库
func QueryDB(cli client.Client, cmd string) (res []client.Result, err error) {
q := client.Query{
Command: cmd,
Database: MyDB,
}
if response, err := cli.Query(q); err == nil {
if response.Error() != nil {
return res, response.Error()
}
res = response.Results
} else {
return res, err
}
return res, nil
}

85
main.go
View File

@ -6,28 +6,37 @@ import (
"log"
"time"
client "github.com/influxdata/influxdb1-client/v2"
"myschools.me/wyh/influx-demo.git/influx"
"myschools.me/wyh/influx-demo.git/model"
)
const (
// MyDB wyh
MyDB = "wyh"
username = "admin"
password = ""
MyDB = "wyh"
// MyMeasurement cpu_usgae
MyMeasurement = "cpu_usage"
)
func main() {
conn := connInflux()
conn := influx.ConnInflux()
fmt.Println(conn)
//insert
WritesPoints(conn)
var points = &model.Influx{
TableName: "cpu_usage",
Tags: map[string]string{"cpu": "ih-cpu"},
Fields: map[string]interface{}{
"idle": 20.1,
"system": 43.3,
"user": 86.6,
},
}
influx.WritesPoints(points, conn)
//获取10条数据并展示
qs := fmt.Sprintf("SELECT * FROM %s LIMIT %d", MyMeasurement, 10)
res, err := QueryDB(conn, qs)
res, err := influx.QueryDB(conn, qs)
if err != nil {
log.Fatal(err)
}
@ -42,65 +51,3 @@ func main() {
log.Printf("[%2d] %s: %s %s\n", k, t.Format(time.Stamp), value, valu)
}
}
func connInflux() client.Client {
cli, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://127.0.0.1:8086",
Username: username,
Password: password,
})
if err != nil {
log.Fatal(err)
}
return cli
}
//QueryDB 查询数据库
func QueryDB(cli client.Client, cmd string) (res []client.Result, err error) {
q := client.Query{
Command: cmd,
Database: MyDB,
}
if response, err := cli.Query(q); err == nil {
if response.Error() != nil {
return res, response.Error()
}
res = response.Results
} else {
return res, err
}
return res, nil
}
//WritesPoints 写入数据
func WritesPoints(cli client.Client) {
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
Database: MyDB,
Precision: "s",
})
if err != nil {
log.Fatal(err)
}
tags := map[string]string{"cpu": "ih-cpu"}
fields := map[string]interface{}{
"idle": 20.1,
"system": 43.3,
"user": 86.6,
}
pt, err := client.NewPoint(
"cpu_usage",
tags,
fields,
time.Now(),
)
if err != nil {
log.Fatal(err)
}
bp.AddPoint(pt)
if err := cli.Write(bp); err != nil {
log.Fatal(err)
}
}

8
model/influx.go Normal file
View File

@ -0,0 +1,8 @@
package model
// Influx Influx测试表
type Influx struct {
TableName string
Tags map[string]string
Fields map[string]interface{}
}