-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbasic.go
116 lines (101 loc) · 2.97 KB
/
basic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/InfluxCommunity/influxdb3-go/v2/influxdb3"
)
func main() {
// Retrieve credentials from environment variables.
url := os.Getenv("INFLUX_URL")
token := os.Getenv("INFLUX_TOKEN")
database := os.Getenv("INFLUX_DATABASE")
// (optional) Custom SSL root certificates file path
sslRootsFilePath := os.Getenv("INFLUX_SSL_ROOTS_FILE_PATH")
// (optional) Proxy URL
proxyURL := os.Getenv("INFLUX_PROXY_URL")
// Instantiate a client using your credentials.
client, err := influxdb3.New(influxdb3.ClientConfig{
Host: url,
Token: token,
Database: database,
SSLRootsFilePath: sslRootsFilePath,
Proxy: proxyURL,
})
if err != nil {
panic(err)
}
// Close the client when finished and raise any errors.
defer func(client *influxdb3.Client) {
err := client.Close()
if err != nil {
panic(err)
}
}(client)
// Create a Point using the full params constructor.
p := influxdb3.NewPoint("stat",
map[string]string{"location": "Paris"},
map[string]any{
"temperature": 24.5,
"humidity": 40,
},
time.Now())
// Write the point synchronously.
err = client.WritePoints(context.Background(), []*influxdb3.Point{p})
if err != nil {
panic(err)
}
// Create a Point using the fluent interface (method chaining).
p = influxdb3.NewPointWithMeasurement("stat").
SetTag("location", "London").
SetField("temperature", 17.1).
SetField("humidity", 65).
SetTimestamp(time.Now())
// Write the point synchronously.
err = client.WritePoints(context.Background(), []*influxdb3.Point{p})
if err != nil {
panic(err)
}
// Construct data as a custom type.
sensorData := struct {
Table string `lp:"measurement"`
Unit string `lp:"tag,location"`
Temp float64 `lp:"field,temperature"`
Humid int64 `lp:"field,humidity"`
Time time.Time `lp:"timestamp"`
}{"stat", "Madrid", 33.8, 35, time.Now()}
// Write the data.
err = client.WriteData(context.Background(), []any{sensorData})
if err != nil {
panic(err)
}
// Provide data as a line protocol string.
line := fmt.Sprintf("stat,location=Berlin temperature=%f,humidity=%di", 20.1, 55)
// Write the line protocol string.
err = client.Write(context.Background(), []byte(line))
if err != nil {
panic(err)
}
// Prepare an SQL query
query := `
SELECT *
FROM stat
WHERE time >= now() - interval '5 minutes'
AND location IN ('Paris', 'London', 'Madrid')
`
// Run the query
iterator, err := client.Query(context.Background(), query)
if err != nil {
panic(err)
}
for iterator.Next() {
// The query iterator returns each row as a map[string]interface{}.
// The keys are the column names, allowing you to access the values by column name.
value := iterator.Value()
fmt.Printf("%s at %v:\n", value["location"],
(value["time"].(time.Time)).Format(time.RFC822))
fmt.Printf(" temperature: %f\n", value["temperature"])
fmt.Printf(" humidity : %d%%\n", value["humidity"])
}
}