测试准备
需要将InfluxDB的源码放入 go/src/github.com/influxdata 目录
单写测试代码(write1.go):
package main import ( "log" "time" "fmt" "math/rand" "github.com/influxdata/influxdb/client/v2" ) const ( MyDB = "testInfluxdb" username = "root" password = "" ) func queryDB(clnt client.Client, cmd string) (res []client.Result, err error) { q := client.Query{ Command: cmd, Database: MyDB, } if response, err := clnt.Query(q); err == nil { if response.Error() != nil { return res, response.Error() } res = response.Results } else { return res, err } return res, nil } func writePoints(clnt client.Client,num int) { sampleSize := 1 * 10000 rand.Seed(42) t := num bp, _ := client.NewBatchPoints(client.BatchPointsConfig{ Database: MyDB, Precision: "us", }) for i := 0; i < sampleSize; i++ { t += 1 tags := map[string]string{ "system_name": fmt.Sprintf("sys_%d",i%10), "site_name":fmt.Sprintf("s_%d", (t+i) % 10), "equipment_name":fmt.Sprintf("e_%d",t % 10), } fields := map[string]interface{}{ "value" : fmt.Sprintf("%d",rand.Int()), } pt, err := client.NewPoint("monitorStatus", tags, fields,time.Now()) if err != nil { log.Fatalln("Error: ", err) } bp.AddPoint(pt) } err := clnt.Write(bp) if err != nil { log.Fatal(err) } //fmt.Printf("%d task done\n",num) } func main() { // Make client c, err := client.NewHTTPClient(client.HTTPConfig{ Addr: "http://localhost:8086", Username: username, Password: password, }) if err != nil { log.Fatalln("Error: ", err) } _, err = queryDB(c, fmt.Sprintf("CREATE DATABASE %s", MyDB)) if err != nil { log.Fatal(err) } i := 1 for i <= 10000 { defer writePoints(c,i) //fmt.Printf("i=%d\n",i) i += 1 } //fmt.Printf("task done : i=%d \n",i) }
单机读:
package main import ( "log" //"time" "fmt" //"math/rand" "github.com/influxdata/influxdb/client/v2" ) const ( MyDB = "testInfluxdb" username = "root" password = "" ) func queryDB(clnt client.Client, cmd string) (res []client.Result, err error) { q := client.Query{ Command: cmd, Database: MyDB, } if response, err := clnt.Query(q); err == nil { if response.Error() != nil { return res, response.Error() } res = response.Results } else { return res, err } return res, nil } func main() { // Make client c, err := client.NewHTTPClient(client.HTTPConfig{ Addr: "http://localhost:8086", Username: username, Password: password, }) if err != nil { log.Fatalln("Error: ", err) } q := fmt.Sprintf("select * from monitorStatus where system_name='sys_5' and site_name='s_1' and equipment_name='e_6' order by time desc limit 10000 ;") res, err2 := queryDB(c, q) if err2 != nil { log.Fatal(err) } count := len(res[0].Series[0].Values) log.Printf("Found a total of %v records\n", count) }
代码摘自:http://www.cnblogs.com/MikeZhang/p/InfluxDBTest20170212.html
本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6813989.html,如需转载请自行联系原作者