init
This commit is contained in:
parent
ed325469a5
commit
6e4facde8e
|
|
@ -0,0 +1,2 @@
|
||||||
|
go.sum
|
||||||
|
influx-demo
|
||||||
2
go.mod
2
go.mod
|
|
@ -1,3 +1,5 @@
|
||||||
module myschools.me/suguo/influx-demo
|
module myschools.me/suguo/influx-demo
|
||||||
|
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
|
require github.com/influxdata/influxdb1-client v0.0.0-20200827194710-b269163b24ab
|
||||||
|
|
|
||||||
90
main..go
90
main..go
|
|
@ -0,0 +1,90 @@
|
||||||
|
//Package 最最基本的用法
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
client "github.com/influxdata/influxdb1-client/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
// influxdb demo
|
||||||
|
|
||||||
|
func connInflux() client.Client {
|
||||||
|
cli, err := client.NewHTTPClient(client.HTTPConfig{
|
||||||
|
Addr: "http://127.0.0.1:8086",
|
||||||
|
Username: "admin",
|
||||||
|
Password: "",
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return cli
|
||||||
|
}
|
||||||
|
|
||||||
|
// query
|
||||||
|
func queryDB(cli client.Client, cmd string) (res []client.Result, err error) {
|
||||||
|
q := client.Query{
|
||||||
|
Command: cmd,
|
||||||
|
Database: "test",
|
||||||
|
}
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
// insert
|
||||||
|
func writesPoints(cli client.Client) {
|
||||||
|
bp, err := client.NewBatchPoints(client.BatchPointsConfig{
|
||||||
|
Database: "test",
|
||||||
|
Precision: "s", //精度,默认ns
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
tags := map[string]string{"cpu": "ih-cpu"}
|
||||||
|
fields := map[string]interface{}{
|
||||||
|
"idle": 201.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)
|
||||||
|
err = cli.Write(bp)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Println("insert success")
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
conn := connInflux()
|
||||||
|
fmt.Println(conn)
|
||||||
|
|
||||||
|
// insert
|
||||||
|
writesPoints(conn)
|
||||||
|
|
||||||
|
// 获取10条数据并展示
|
||||||
|
qs := fmt.Sprintf("SELECT * FROM %s LIMIT %d", "cpu_usage", 10)
|
||||||
|
res, err := queryDB(conn, qs)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, row := range res[0].Series[0].Values {
|
||||||
|
for j, value := range row {
|
||||||
|
log.Printf("j:%d value:%v\n", j, value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
readme.md
20
readme.md
|
|
@ -0,0 +1,20 @@
|
||||||
|
influx操作示例
|
||||||
|
===
|
||||||
|
|
||||||
|
### 安装
|
||||||
|
* influxDB 1.x版本
|
||||||
|
go get github.com/influxdata/influxdb1-client/v2
|
||||||
|
* influxDB 2.x版本
|
||||||
|
go get github.com/influxdata/influxdb-client-go
|
||||||
|
|
||||||
|
### 行协议介绍
|
||||||
|
https://jasper-zhang1.gitbooks.io/influxdb/content/Write_protocols/line_protocol.html
|
||||||
|
|
||||||
|
###### 语法
|
||||||
|
一行Line Protocol表示InfluxDB中的一个数据点。它向InfluxDB通知点的measurement,tag set,field set和timestamp。
|
||||||
|
|
||||||
|
### 写数据到InfluxDB
|
||||||
|
###### HTTP API
|
||||||
|
curl -i -XPOST "http://localhost:8086/write?db=science_is_cool" --data-binary 'weather,location=us-midwest temperature=82 1465839830100400200'
|
||||||
|
###### CLI
|
||||||
|
INSERT weather,location=us-midwest temperature=82 1465839830100400200
|
||||||
Loading…
Reference in New Issue