增加influx标准代码
This commit is contained in:
parent
1ceea92959
commit
ea9b6384af
15
go.mod
15
go.mod
|
|
@ -6,6 +6,7 @@ require (
|
||||||
github.com/gin-gonic/gin v1.7.4
|
github.com/gin-gonic/gin v1.7.4
|
||||||
github.com/gomodule/redigo v1.8.5
|
github.com/gomodule/redigo v1.8.5
|
||||||
github.com/hashicorp/consul/api v1.10.1
|
github.com/hashicorp/consul/api v1.10.1
|
||||||
|
github.com/influxdata/influxdb v1.9.5
|
||||||
go.mongodb.org/mongo-driver v1.7.4
|
go.mongodb.org/mongo-driver v1.7.4
|
||||||
google.golang.org/grpc v1.40.0
|
google.golang.org/grpc v1.40.0
|
||||||
gorm.io/driver/mysql v1.1.2
|
gorm.io/driver/mysql v1.1.2
|
||||||
|
|
@ -16,7 +17,7 @@ require (
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da // indirect
|
github.com/armon/go-metrics v0.3.3 // indirect
|
||||||
github.com/fatih/color v1.9.0 // indirect
|
github.com/fatih/color v1.9.0 // indirect
|
||||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||||
github.com/go-playground/locales v0.13.0 // indirect
|
github.com/go-playground/locales v0.13.0 // indirect
|
||||||
|
|
@ -28,27 +29,24 @@ require (
|
||||||
github.com/golang/snappy v0.0.1 // indirect
|
github.com/golang/snappy v0.0.1 // indirect
|
||||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0 // indirect
|
github.com/grpc-ecosystem/grpc-gateway/v2 v2.6.0 // indirect
|
||||||
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
|
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
|
||||||
github.com/hashicorp/go-hclog v0.12.0 // indirect
|
github.com/hashicorp/go-hclog v0.12.2 // indirect
|
||||||
github.com/hashicorp/go-immutable-radix v1.0.0 // indirect
|
github.com/hashicorp/go-immutable-radix v1.2.0 // indirect
|
||||||
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
|
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
|
||||||
github.com/hashicorp/golang-lru v0.5.1 // indirect
|
github.com/hashicorp/golang-lru v0.5.4 // indirect
|
||||||
github.com/hashicorp/serf v0.9.5 // indirect
|
github.com/hashicorp/serf v0.9.5 // indirect
|
||||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||||
github.com/jinzhu/now v1.1.2 // indirect
|
github.com/jinzhu/now v1.1.2 // indirect
|
||||||
github.com/json-iterator/go v1.1.11 // indirect
|
github.com/json-iterator/go v1.1.11 // indirect
|
||||||
github.com/klauspost/compress v1.13.6 // indirect
|
github.com/klauspost/compress v1.13.6 // indirect
|
||||||
github.com/kr/text v0.2.0 // indirect
|
|
||||||
github.com/leodido/go-urn v1.2.0 // indirect
|
github.com/leodido/go-urn v1.2.0 // indirect
|
||||||
github.com/mattn/go-colorable v0.1.6 // indirect
|
github.com/mattn/go-colorable v0.1.8 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.12 // indirect
|
github.com/mattn/go-isatty v0.0.12 // indirect
|
||||||
github.com/mattn/go-sqlite3 v1.14.5 // indirect
|
github.com/mattn/go-sqlite3 v1.14.5 // indirect
|
||||||
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
github.com/mitchellh/go-homedir v1.1.0 // indirect
|
||||||
github.com/mitchellh/mapstructure v1.4.1 // indirect
|
github.com/mitchellh/mapstructure v1.4.1 // indirect
|
||||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
|
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/stretchr/testify v1.7.0 // indirect
|
|
||||||
github.com/ugorji/go/codec v1.1.13 // indirect
|
github.com/ugorji/go/codec v1.1.13 // indirect
|
||||||
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
|
||||||
github.com/xdg-go/scram v1.0.2 // indirect
|
github.com/xdg-go/scram v1.0.2 // indirect
|
||||||
|
|
@ -61,7 +59,6 @@ require (
|
||||||
golang.org/x/text v0.3.7 // indirect
|
golang.org/x/text v0.3.7 // indirect
|
||||||
google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83 // indirect
|
google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83 // indirect
|
||||||
google.golang.org/protobuf v1.27.1 // indirect
|
google.golang.org/protobuf v1.27.1 // indirect
|
||||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
|
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package influx
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Address string
|
||||||
|
DBName string
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
Timeout int
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,103 @@
|
||||||
|
package influx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/influxdata/influxdb/client/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var conf *Config
|
||||||
|
|
||||||
|
//配置初始化
|
||||||
|
func Init(_conf *Config) {
|
||||||
|
conf = _conf
|
||||||
|
if conf == nil {
|
||||||
|
conf = &Config{
|
||||||
|
Address: "127.0.0.1",
|
||||||
|
Timeout: 3,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//实例
|
||||||
|
func New() (client.Client, error) {
|
||||||
|
cli, err := client.NewHTTPClient(client.HTTPConfig{
|
||||||
|
Addr: conf.Address,
|
||||||
|
Username: conf.Username,
|
||||||
|
Password: conf.Password,
|
||||||
|
Timeout: time.Duration(conf.Timeout) * time.Second,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return cli, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 写入例子
|
||||||
|
func WriteSample() error {
|
||||||
|
cli, err := New()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer cli.Close()
|
||||||
|
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
|
||||||
|
Database: func() string {
|
||||||
|
if conf.DBName == "" {
|
||||||
|
return "sample"
|
||||||
|
}
|
||||||
|
return conf.DBName
|
||||||
|
}(),
|
||||||
|
Precision: "s",
|
||||||
|
})
|
||||||
|
|
||||||
|
fields := make(map[string]interface{})
|
||||||
|
fields["f1"] = "F1"
|
||||||
|
fields["f2"] = 1
|
||||||
|
|
||||||
|
pt, _ := client.NewPoint(
|
||||||
|
"heartrate",
|
||||||
|
map[string]string{"platform": "ws", "orgid": "a001"},
|
||||||
|
fields,
|
||||||
|
time.Now(),
|
||||||
|
)
|
||||||
|
|
||||||
|
bp.AddPoint(pt)
|
||||||
|
if err := cli.Write(bp); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func ReadSample() error {
|
||||||
|
query, err := New()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer query.Close()
|
||||||
|
|
||||||
|
resp, err := query.QueryCtx(context.Background(), client.Query{
|
||||||
|
Command: `select * from heartrate where time>'` + time.Now().Add(-12*time.Hour).Format("2006-01-02T15:04:05") + `' group by time(10m) tz('Asia/Shanghai')`,
|
||||||
|
Database: func() string {
|
||||||
|
if conf.DBName == "" {
|
||||||
|
return "sample"
|
||||||
|
}
|
||||||
|
return conf.DBName
|
||||||
|
}(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, s := range resp.Results[0].Series[0].Values {
|
||||||
|
//fmt.Println(reflect.TypeOf(s[1]))
|
||||||
|
f1 := s[1].(json.Number).String()
|
||||||
|
f2, _ := s[2].(json.Number).Int64()
|
||||||
|
platform := s[3].(json.Number).String()
|
||||||
|
fmt.Println(f1, f2, platform)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue