Go

Insert

To begin with, we have to prepare a Series, which delegates one row data. There are three kind fields in Series you can use:

KindDescription
Tagindex column, helps to retrieve data more efficiently
Fieldvalue column, helps to analysis, aggregation, calculating, etc,.
Timestamptimestamp column, each table MUST have exactly one timestamp column

then, you can add one Series into Metric, then create an InsertRequest and call client.Insert to store the data into GreptimeDB.

Metric can change the Timestamp precision by metric.SetTimePrecision. The following is the supported options:

PrecisionDescription
time.Second
time.Milliseconddefault
time.Microsecond
time.Nanosecond

go

  1. func Insert() {
  2. series := greptime.Series{} // Create one row of data
  3. series.AddStringTag("host", "localhost") // add index column, for query efficiency
  4. series.AddFloatField("cpu", 0.90) // add value column
  5. series.AddIntField("memory", 1024) // add value column
  6. series.SetTimestamp(time.Now()) // requird
  7. metric := greptime.Metric{} // Create a Metric and add the Series
  8. metric.AddSeries(series)
  9. // metric.SetTimePrecision(time.Second) // default is Millisecond
  10. // metric.SetTimestampAlias("timestamp") // default is 'ts'
  11. // Create an InsertRequest using fluent style
  12. // the specified table will be created automatically if it's not exist
  13. insertRequest := greptime.InsertRequest{}
  14. // if you want to specify another database, you can specify it via: `WithDatabase(database)`
  15. insertRequest.WithTable("monitor").WithMetric(metric) // .WithDatabase(database)
  16. // Fire the real Insert request and Get the affected number of rows
  17. n, err := client.Insert(context.Background(), insertRequest)
  18. if err != nil {
  19. fmt.Printf("fail to insert, err: %+v\n", err)
  20. return
  21. }
  22. fmt.Printf("AffectedRows: %d\n", n)
  23. }