Go操作influxDB
<p> </p><div class="container"> </div>
<div id="body">
<div class="container">
<div class="col-group">
<div id="main" class="col-8">
<div class="res-cons">
<h1 class="post-title">influxDB</h1>
<div class="post-content">
<h2 id="安装">安装</h2>
<h3 id="下载">下载</h3>
<p>https://portal.influxdata.com/downloads/</p>
<p>这里需要注意因为这个网站引用了google的api所以国内点页面的按钮是没反应的,怎么办呢?</p>
<p>按照下图所示,按<code>F12</code>打开浏览器的控制台,然后点击<code>Elements</code>,按下<code>Ctrl/Command+F</code>搜索<code>releases/influxdb</code>,按回车查找自己所需版本的下载地址。 </p>
<p><img src="https://www.liwenzhou.com/images/Go/influxDB/influxdb_01.png"></p>
<p>Mac和Linux用户可以点击https://v2.docs.influxdata.com/v2.0/get-started/下载。</p>
<h3 id="安装-1">安装</h3>
<p>将上一步的压缩包,解压到本地。</p>
<h2 id="influxdb介绍">influxDB介绍</h2>
<h3 id="名词介绍">名词介绍</h3>
<table>
<thead>
<tr><th>influxDB名词</th><th>传统数据库概念</th></tr>
</thead>
<tbody>
<tr>
<td>database</td>
<td>数据库</td>
</tr>
<tr>
<td>measurement</td>
<td>数据表</td>
</tr>
<tr>
<td>point</td>
<td>数据行</td>
</tr>
</tbody>
</table>
<h3 id="point">point</h3>
<p>influxDB中的point相当于传统数据库里的一行数据,由时间戳(time)、数据(field)、标签(tag)组成。</p>
<table>
<thead>
<tr><th>Point属性</th><th>传统数据库概念</th></tr>
</thead>
<tbody>
<tr>
<td>time</td>
<td>每个数据记录时间,是数据库中的主索引</td>
</tr>
<tr>
<td>field</td>
<td>各种记录值(没有索引的属性),例如温度、湿度</td>
</tr>
<tr>
<td>tags</td>
<td>各种有索引的属性,例如地区、海拔</td>
</tr>
</tbody>
</table>
<h3 id="series">Series</h3>
<p><code>Series</code>相当于是 InfluxDB 中一些数据的集合,在同一个 database 中,retention policy、measurement、tag sets 完全相同的数据同属于一个 series,同一个 series 的数据在物理上会按照时间顺序排列存储在一起。</p>
<p>想要了解更多</p>
<h2 id="go操作influxdb">Go操作influxDB</h2>
<h3 id="安装-2">安装</h3>
<h4 id="influxdb-1-x版本">influxDB 1.x版本</h4>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">go get github.com/influxdata/influxdb1-client/v2
</pre>
</div>
<p> </p>
<h4 id="influxdb-2-x版本">influxDB 2.x版本</h4>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">go get github.com/influxdata/influxdb-client-go
</pre>
</div>
<p> </p>
<h3 id="基本使用">基本使用</h3>
<div class="cnblogs_Highlighter">
<pre class="brush:go;gutter:true;">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 := mapstring{"cpu": "ih-cpu"}
fields := mapinterface{}{
"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.Series.Values {
for j, value := range row {
log.Printf("j:%d value:%v\n", j, value)
}
}
}
</pre>
</div>
<p> </p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="MySignature" role="contentinfo">
Songzhibin<br><br>
来源:https://www.cnblogs.com/binHome/p/12345373.html
頁:
[1]