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
| package main
import ( "context" "fmt" influxdb2 "github.com/influxdata/influxdb-client-go/v2" "time" )
func main() { client := influxdb2.NewClient("http://localhost:8086", "Ki-3lIwR1A8WIJuXIa1od_nH2B164qs0i4_yS7H1mJ_NqiAs_6AIJ8ZUYAtT69eWvx9RIVSGFVtzZyPQ7DgdgA==") writeAPI := client.WriteAPIBlocking("initOrgName", "initBucketName") p := influxdb2.NewPoint("stat", map[string]string{"unit": "temperature"}, map[string]interface{}{"avg": 24.5, "max": 45.0}, time.Now()) writeAPI.WritePoint(context.Background(), p) p = influxdb2.NewPointWithMeasurement("stat"). AddTag("unit", "temperature"). AddField("avg", 23.2). AddField("max", 45.0). SetTime(time.Now()) err := writeAPI.WritePoint(context.Background(), p) if err != nil { panic(err) } line := fmt.Sprintf("stat,unit=temperature avg=%f,max=%f", 23.5, 45.0) err = writeAPI.WriteRecord(context.Background(), line) if err != nil { panic(err) }
queryAPI := client.QueryAPI("initOrgName") result, err := queryAPI.Query(context.Background(), `from(bucket:"initBucketName")|> range(start: -1h) |> filter(fn: (r) => r._measurement == "stat")`) if err == nil { for result.Next() { if result.TableChanged() { fmt.Printf("table: %s\n", result.TableMetadata().String()) } fmt.Printf("row: %s\n", result.Record().String()) } if result.Err() != nil { fmt.Printf("Query error: %s\n", result.Err().Error()) } } else { panic(err) } defer client.Close() }
|