func queryDB(clnt client.Client, command string) (res []client.Result, err error) {
	q := client.Query{
		Command:  command,
		Database: DB,
	}
	if response, err := clnt.Query(q); err == nil {
		if response.Error() != nil {
			return res, response.Error()
		}
		res = response.Results
	} else {
		return res, err
	}
	return res, nil
}
queryDB query the database
package main
import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"time"
	client "github.com/influxdata/influxdb/client/v2"
)
const (
	DB       = "metricsdb"
	username = "opsadmin"
	password = "pass123"
)
func main() {
	c := influxDBClient()
	createMetrics(c)
	readWithLimit(c, 10)
	meanCPUUsage(c, "us-west")
	countRegion(c, "us-west")
}
func influxDBClient() client.Client {
	c, err := client.NewHTTPClient(client.HTTPConfig{
		Addr:     "http://localhost:8086",
		Username: username,
		Password: password,
	})
	if err != nil {
		log.Fatalln("Error: ", err)
	}
	return c
}
func createMetrics(clnt client.Client) {
	batchCount := 100
	rand.Seed(42)
	bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
		Database:  DB,
		Precision: "s",
	})
	for i := 0; i < batchCount; i++ {
		regions := []string{"us-west", "us-central", "us-north", "us-east"}
		tags := map[string]string{
			"host":   fmt.Sprintf("192.168.%d.%d", rand.Intn(100), rand.Intn(100)),
			"region": regions[rand.Intn(len(regions))],
		}
		value := rand.Float64() * 100.0
		fields := map[string]interface{}{
			"cpu_usage": value,
		}
		pt, err := client.NewPoint("cpu", tags, fields, time.Now())
		if err != nil {
			log.Fatalln("Error: ", err)
		}
		bp.AddPoint(pt)
	}
	err := clnt.Write(bp)
	if err != nil {
		log.Fatalln("Error: ", err)
	}
}