1.安装
请参考http://www.cnblogs.com/zhja/p/5996191.html,
安装完毕运行influxd,http://域名:8083为控制台界面;http://域名:8086为api地址;运行influx进入命令行模式。
2.go操作influxdb
1)利用influxdb的client,github地址:github.com/influxdata/influxdb/client/v2
2)创建用户和授权
create user zhja with password 'zhjazhja'
grant all privileges to zhja
3)创建数据库
create database nfdump
4)链接influxdb
package main import (
"fmt"
"log" "github.com/influxdata/influxdb/client/v2"
) const (
MyDB = "nfdump" //数据库名
username = "zhja" //用户名
password = "zhjazhja" //密码
) func main(){
//链接数据库
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://192.168.210.130:8086",
Username: username,
Password: password,
}) if err != nil {
log.Fatalln("Error: ", err)
} fmt.Println(c)
}
执行结果:
5)influxdb Measurements(查看详细介绍)
6)批量插入数据
package main import (
"fmt"
"log"
"time"
"math/rand" "github.com/influxdata/influxdb/client/v2"
) const (
MyDB = "nfdump" //数据库名
username = "zhja" //用户名
password = "zhjazhja" //密码
) func main(){
//链接数据库
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://192.168.210.130:8086",
Username: username,
Password: password,
}) if err != nil {
log.Fatalln("Error: ", err)
} writePoints(c, MyDB)
} func writePoints(c client.Client, MyDB string) {
sampleSize := 1000
rand.Seed(42) bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
Database: MyDB,
Precision: "us",
}) for i := 0; i < sampleSize; i++ {
regions := []string{"us-west1", "us-west2", "us-west3", "us-east1"}
tags := map[string]string{
"cpu": "cpu-total",
"host": fmt.Sprintf("host%d", rand.Intn(1000)),
"region": regions[rand.Intn(len(regions))],
} idle := rand.Float64() * 100.0
fields := map[string]interface{}{
"idle": idle,
"busy": 100.0 - idle,
} pt, err := client.NewPoint("cpu_usage", tags, fields, time.Now())
if err != nil {
log.Fatalln("Error: ", err)
} bp.AddPoint(pt)
} err := c.Write(bp)
if err != nil {
log.Fatal(err)
}
}
执行结果:
7)利用sql查询数据,利用上面的数据
package main import (
"fmt"
"log"
"time"
"encoding/json" "github.com/influxdata/influxdb/client/v2"
) const (
MyDB = "nfdump" //数据库名
username = "zhja" //用户名
password = "zhjazhja" //密码
MyMeasurement = "cpu_usage"
) func main(){
//链接数据库
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://192.168.210.130: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)
} //获取总数
q := fmt.Sprintf("SELECT count(%s) FROM %s", "*", MyMeasurement)
res, err := queryDB(c, q)
if err != nil {
log.Fatal(err)
}
count := res[0].Series[0].Values[0][1]
log.Printf("Found a total of %v records\n", count) //获取10条数据并展示
qs := fmt.Sprintf("SELECT * FROM %s LIMIT %d", MyMeasurement, 20)
res, err = queryDB(c, qs)
if err != nil {
log.Fatal(err)
} for i, row := range res[0].Series[0].Values {
t, err := time.Parse(time.RFC3339, row[0].(string))
if err != nil {
log.Fatal(err)
}
//fmt.Println(reflect.TypeOf(row[1]))
val := row[1].(json.Number)
log.Printf("[%2d] %s: %s\n", i, t.Format(time.Stamp), val)
}
} func queryDB(c client.Client, cmd string) (res []client.Result, err error) {
q := client.Query{
Command: cmd,
Database: MyDB,
}
if response, err := c.Query(q); err == nil {
if response.Error() != nil {
return res, response.Error()
}
res = response.Results
} else {
return res, err
}
return res, nil
}
执行结果: