Home > Backend Development > Golang > Using InfluxDB in Go: A Complete Guide

Using InfluxDB in Go: A Complete Guide

王林
Release: 2023-06-17 11:55:00
Original
3225 people have browsed it

I believe many program developers have heard of InfluxDB, which is an open source, distributed time series data storage, mainly used to store operational metrics (OMI) and event data. InfluxDB's core features include high performance, scalability, and a powerful query language. In addition, InfluxDB also provides client SDKs in multiple languages, including Go.

Go language is a very powerful programming language. It is efficient and concurrency, and is also suitable for writing microservices. In this article, we will introduce how to use InfluxDB to manipulate time series data in the Go language. We will cover the following:

  • How to install InfluxDB and start InfluxDB
  • How to install the InfluxDB client SDK in Go
  • How to create an InfluxDB database
  • How to perform basic InfluxDB query operations
  • How to write and read the InfluxDB database through the Go language

Installing InfluxDB

Before you start using InfluxDB, We need to install and start InfluxDB first. InfluxDB's official website provides detailed installation guides for various operating systems. Please select the appropriate installation guide based on your current operating system version.

After the installation is complete, you can start InfluxDB with the following command:

$ influxd
Copy after login

Install InfluxDB Go SDK

The Go language has an official InfluxDB client SDK for us to use. Use the InfluxDB Client SDK in your application to easily connect to an InfluxDB database to write and read data from the database. We can install the InfluxDB SDK in the Go language project using the following command:

go get github.com/influxdata/influxdb1-client/v2
Copy after login

Create an InfluxDB database

Before we start writing data, we need to create an InfluxDB database. You can use InfluxDB's web interface, or you can use the CLI tool influx:

$ influx
Connected to http://localhost:8086 version 1.8.0
InfluxDB shell version: 1.8.0

> create database mydb
Copy after login

Once completed, we can use the following command to list all databases:

> show databases
name: databases
-----------
name
_internal
mydb
Copy after login

Execute InfluxDB query

In addition to connecting to InfluxDB in Go language, we can also use influx to manually execute InfluxDB queries. Here is a simple example:

$ influx
Connected to http://localhost:8086 version 1.8.0
InfluxDB shell version: 1.8.0

> use mydb
Using database mydb

> insert temperature value=25.5
> insert temperature value=28.0

> select * from temperature
name: temperature
time                value
----                -----
1623102590352798368 25.5
1623102590981854175 28
Copy after login

In this example, we have inserted two temperature values ​​in a database named mydb. We then executed a simple query to get all temperature data.

Using InfluxDB in Go language

The following are some basic usage examples of InfluxDB Go SDK:

package main

import (
    "fmt"
    "time"
    "github.com/influxdata/influxdb1-client/v2"
)

func main() {
    // 初始化 InfluxDB 客户端
    c, err := client.NewHTTPClient(client.HTTPConfig{
        Addr: "http://localhost:8086",
    })
    if err != nil {
        fmt.Println("Error creating InfluxDB Client: ", err.Error())
    }
    defer c.Close()

    // 创建新的 InfluxDB 数据点
    tags := map[string]string{"test": "test123"}
    fields := map[string]interface{}{
        "value": 35.6,
    }
    pt, err := client.NewPoint("temperature", tags, fields, time.Now())
    if err != nil {
        fmt.Println("Error: ", err.Error())
    }

    // 写入数据点到 InfluxDB
    bp, err := client.NewBatchPoints(client.BatchPointsConfig{
        Database:  "mydb",
        Precision: "s",
    })
    if err != nil {
        fmt.Println("Error: ", err.Error())
    }
    bp.AddPoint(pt)
    err = c.Write(bp)
    if err != nil {
        fmt.Println("Error: ", err.Error())
    }

    // 查询 InfluxDB 数据点
    q := client.Query{
        Command:  `SELECT "value" FROM "temperature"`,
        Database: "mydb",
    }
    res, err := c.Query(q)
    if err != nil {
        fmt.Println("Error: ", err.Error())
    }

    var value float64
    for _, row := range res.Results[0].Series[0].Values {
        value = row[1].(float64)
    }
    fmt.Printf("%+v
", value)
}
Copy after login

In this example, we first initialize an InfluxDB Go SDK client. Next, we manually created a new InfluxDB data point and added it to a batch of data. Finally, we use the InfluxDB Go SDK to query the data points and print their return values ​​to the console.

Conclusion

In this article, we introduced how to use InfluxDB to operate time series data in the Go language and demonstrated how to use the InfluxDB Go SDK. We learned how to install and start InfluxDB, and create an InfluxDB database. We also learned how to perform basic InfluxDB queries and demonstrated how to write and read from an InfluxDB database via the Go language. We hope this article is helpful to Go developers who want to use InfluxDB.

The above is the detailed content of Using InfluxDB in Go: A Complete Guide. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template