Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Installation and use of influxdb 2.0

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "influxdb 2.0 installation and use method". In daily operation, I believe many people have doubts about influxdb 2.0 installation and use method. Xiaobian consulted various materials and sorted out simple and easy operation methods. I hope to help you answer the doubts of "influxdb 2.0 installation and use method"! Next, please follow the small series to learn together!

influxdb is an open source temporal database. The open source version only supports stand-alone + high availability, and the commercial version only supports clustering (horizontal expansion/fragmentation).

Installation Reference https://docs.influxdata.com/influxdb/v2.0/get-started/? t=Docker

cd ~mkdir influxdb-datasudo docker run -d \--name influxdb \--restart always \-p 8086:8086 \--volume /home/your_name/influxdb-data:/var/lib/influxdb2 \influxdb:2.0.4

Code Reference (golang)

package mainimport ( "context" "fmt" "log" "math/rand" "time" influxdb2 "github.com/influxdata/influxdb-client-go/v2")func main() { // You can generate a Token from the "Tokens Tab" in the UI const token = "Uok6jfbNdqUWSIZyaH0aqxrSvSC1vg5WS5UyJHWciqY_FvkfOGxqHox_EbbvJQwRXQ770gUQqD1ZcXWvTjPFrA==" const bucket = "test" const org = "you_org" client := influxdb2.NewClient("http://192.168.1.11:8086", token) // always close client at the end defer client.Close() write(client, bucket, org)}func write(client influxdb2.Client, bucket, org string) { // get non-blocking write client writeAPI := client.WriteAPI(org, bucket) now := time.Now() times := 0 // 10 devices (60*60*24*365)*10=315360000. About 310 million pieces of data for i := 0; i

< 10; i++ { // 过去一年 start := now.Add(-8760 * time.Hour) for now.After(start) { // write line protocol p := influxdb2.NewPointWithMeasurement("设备信息"). AddTag("设备id", fmt.Sprint(i)). AddField("压力", rand.Float32()*100). AddField("温度", rand.Float32()*30).SetTime(start) start = start.Add(1 * time.Second) writeAPI.WritePoint(p) if times == 10000 { log.Printf("当前设备id: %s, 本次写入 %d 条, 最后一条时间: %s", fmt.Sprint(i), times, start.Format("2006-01-02 15:04:05")) times = 0 // Flush writes writeAPI.Flush() } else { times++ } } // Flush writes writeAPI.Flush() }}func query(client influxdb2.Client, bucket, org string) { query := fmt.Sprintf("from(bucket:\"%v\")|>

range(start: -1h) |> filter(fn: (r) => r._ measurement == \"stat\")", bucket) // Get query client queryAPI := client.QueryAPI(org) // get QueryTableResult result, err := queryAPI.Query(context.Background(), query) if err == nil { // Iterate over query response for result.Next() { // Notice when group key has changed if result.TableChanged() { fmt.Printf("table: %s\n", result.TableMetadata().String()) } // Access data fmt.Printf("%s value: %v\n", result.Record().Field(), result.Record().Value()) } // check for an error if result.Err() != nil { fmt.Printf("query parsing error: %s\n", result.Err().Error()) } } else { panic(err) }}

dashboard

At this point, the study of "influxdb 2.0 installation and use method" is over, I hope to solve everyone's doubts. Theory and practice can better match to help you learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report