|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/InfluxCommunity/influxdb3-go/v2/influxdb3" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + // Retrieve credentials from environment variables. |
| 16 | + url := os.Getenv("INFLUX_URL") |
| 17 | + token := os.Getenv("INFLUX_TOKEN") |
| 18 | + database := os.Getenv("INFLUX_DATABASE") |
| 19 | + |
| 20 | + timeout := 10 * time.Second |
| 21 | + maxIdleConnections := 10 |
| 22 | + |
| 23 | + transport := http.DefaultTransport.(*http.Transport).Clone() |
| 24 | + transport.IdleConnTimeout = 90 * time.Second |
| 25 | + transport.MaxIdleConns = maxIdleConnections |
| 26 | + transport.MaxIdleConnsPerHost = maxIdleConnections |
| 27 | + |
| 28 | + dialer := &net.Dialer{ |
| 29 | + Timeout: timeout, |
| 30 | + KeepAlive: 30 * time.Second, |
| 31 | + } |
| 32 | + transport.DialContext = dialer.DialContext |
| 33 | + |
| 34 | + httpClient := &http.Client{ |
| 35 | + Timeout: timeout, |
| 36 | + Transport: transport, |
| 37 | + } |
| 38 | + |
| 39 | + /* |
| 40 | + Creating an Influxdb client with a pre-configured Http client. |
| 41 | + Warning: If you also set ClientConfig.MaxIdleConnections or ClientConfig.IdleConnectionTimeout,... |
| 42 | + It will override these properties in your pre-configured Http client |
| 43 | + */ |
| 44 | + config := influxdb3.ClientConfig{ |
| 45 | + Host: url, |
| 46 | + Token: token, |
| 47 | + Database: database, |
| 48 | + HTTPClient: httpClient, |
| 49 | + } |
| 50 | + client, err := influxdb3.New(config) |
| 51 | + if err != nil { |
| 52 | + panic(err) |
| 53 | + } |
| 54 | + |
| 55 | + // Close the client when finished and raise any errors. |
| 56 | + defer func(client *influxdb3.Client) { |
| 57 | + err := client.Close() |
| 58 | + if err != nil { |
| 59 | + panic(err) |
| 60 | + } |
| 61 | + }(client) |
| 62 | + |
| 63 | + // Create a Point using the full params constructor. |
| 64 | + p := influxdb3.NewPoint("stat", |
| 65 | + map[string]string{"location": "Paris"}, |
| 66 | + map[string]any{ |
| 67 | + "temperature": 24.5, |
| 68 | + "humidity": 40, |
| 69 | + }, |
| 70 | + time.Now()) |
| 71 | + |
| 72 | + // Write the point synchronously. |
| 73 | + err = client.WritePoints(context.Background(), []*influxdb3.Point{p}) |
| 74 | + if err != nil { |
| 75 | + panic(err) |
| 76 | + } |
| 77 | + |
| 78 | + // Create a Point using the fluent interface (method chaining). |
| 79 | + p = influxdb3.NewPointWithMeasurement("stat"). |
| 80 | + SetTag("location", "London"). |
| 81 | + SetField("temperature", 17.1). |
| 82 | + SetField("humidity", 65). |
| 83 | + SetTimestamp(time.Now()) |
| 84 | + |
| 85 | + // Write the point synchronously. |
| 86 | + err = client.WritePoints(context.Background(), []*influxdb3.Point{p}) |
| 87 | + if err != nil { |
| 88 | + panic(err) |
| 89 | + } |
| 90 | + |
| 91 | + // Construct data as a custom type. |
| 92 | + sensorData := struct { |
| 93 | + Table string `lp:"measurement"` |
| 94 | + Unit string `lp:"tag,location"` |
| 95 | + Temp float64 `lp:"field,temperature"` |
| 96 | + Humid int64 `lp:"field,humidity"` |
| 97 | + Time time.Time `lp:"timestamp"` |
| 98 | + }{"stat", "Madrid", 33.8, 35, time.Now()} |
| 99 | + |
| 100 | + // Write the data. |
| 101 | + err = client.WriteData(context.Background(), []any{sensorData}) |
| 102 | + if err != nil { |
| 103 | + panic(err) |
| 104 | + } |
| 105 | + |
| 106 | + // Provide data as a line protocol string. |
| 107 | + line := fmt.Sprintf("stat,location=Berlin temperature=%f,humidity=%di", 20.1, 55) |
| 108 | + |
| 109 | + // Write the line protocol string. |
| 110 | + err = client.Write(context.Background(), []byte(line)) |
| 111 | + if err != nil { |
| 112 | + panic(err) |
| 113 | + } |
| 114 | + |
| 115 | + // Prepare an SQL query |
| 116 | + query := ` |
| 117 | + SELECT * |
| 118 | + FROM stat |
| 119 | + WHERE time >= now() - interval '5 minutes' |
| 120 | + AND location IN ('Paris', 'London', 'Madrid') |
| 121 | + ` |
| 122 | + |
| 123 | + // Run the query (with client timeout) |
| 124 | + ctx, cancel := context.WithTimeout(context.Background(), timeout) |
| 125 | + defer cancel() |
| 126 | + iterator, err := client.Query(ctx, query) |
| 127 | + if err != nil { |
| 128 | + panic(err) |
| 129 | + } |
| 130 | + for iterator.Next() { |
| 131 | + // The query iterator returns each row as a map[string]any. |
| 132 | + // The keys are the column names, allowing you to access the values by column name. |
| 133 | + value := iterator.Value() |
| 134 | + fmt.Printf("%s at %v:\n", value["location"], |
| 135 | + (value["time"].(time.Time)).Format(time.RFC822)) |
| 136 | + fmt.Printf(" temperature: %f\n", value["temperature"]) |
| 137 | + fmt.Printf(" humidity : %d%%\n", value["humidity"]) |
| 138 | + } |
| 139 | +} |
0 commit comments