Skip to content

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
func Insert() {
	series := greptime.Series{}
	series.AddStringTag("host", "localhost")
	series.AddFloatField("cpu", 0.90)
	series.AddIntField("memory", 1024)
	series.SetTimestamp(time.Now())

	metric := greptime.Metric{}
	metric.AddSeries(series)

	monitorReq := greptime.InsertRequest{}
	monitorReq.WithTable("monitor").WithMetric(metric)

	insertsRequest := greptime.InsertsRequest{}
	insertsRequest.Append(monitorReq)

	res, err := client.Insert(context.Background(), insertsRequest)
	if err != nil {
		fmt.Printf("fail to insert, err: %+v\n", err)
		return
	}
	fmt.Printf("AffectedRows: %d\n", res.GetAffectedRows().Value)
}
func Insert() {
	series := greptime.Series{}
	series.AddStringTag("host", "localhost")
	series.AddFloatField("cpu", 0.90)
	series.AddIntField("memory", 1024)
	series.SetTimestamp(time.Now())

	metric := greptime.Metric{}
	metric.AddSeries(series)

	monitorReq := greptime.InsertRequest{}
	monitorReq.WithTable("monitor").WithMetric(metric)

	insertsRequest := greptime.InsertsRequest{}
	insertsRequest.Append(monitorReq)

	res, err := client.Insert(context.Background(), insertsRequest)
	if err != nil {
		fmt.Printf("fail to insert, err: %+v\n", err)
		return
	}
	fmt.Printf("AffectedRows: %d\n", res.GetAffectedRows().Value)
}