Skip to content

Instantly share code, notes, and snippets.

@axl89
Last active March 29, 2020 11:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axl89/5b466fac4f3b8cf05e73f155f6327184 to your computer and use it in GitHub Desktop.
Save axl89/5b466fac4f3b8cf05e73f155f6327184 to your computer and use it in GitHub Desktop.
InfluxDB batch writting in Golang
package main
import (
"log"
"os"
"strconv"
"time"
_ "github.com/influxdata/influxdb1-client" // this is important because of the bug in go mod
client "github.com/influxdata/influxdb1-client/v2"
)
// Insert inserta cosas
/*
func ExampleClient_query() {
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
})
if err != nil {
fmt.Println("Error creating InfluxDB Client: ", err.Error())
}
defer c.Close()
q := client.NewQuery("SELECT * FROM website", "pingass", "")
if response, err := c.Query(q); err == nil && response.Error() == nil {
fmt.Println(response.Results)
}
}
*/
/*
func ExampleClient_insert() {
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
})
if err != nil {
fmt.Println("Error creating InfluxDB Client: ", err.Error())
}
defer c.Close()
//q := client.NewQuery("INSERT cpu,host=serverA,region=us_west value=0.64", "mydb", "")
q := client.NewQuery("INSERT cpu,host=serverA,region=us_west value=0.64", "mydb", "")
if response, err := c.Query(q); err == nil && response.Error() == nil {
fmt.Println(response.Results)
}
}
*/
/*
func ExampleClient_create() {
c, err := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
})
if err != nil {
fmt.Println("Error creating InfluxDB Client: ", err.Error())
}
defer c.Close()
q := client.NewQuery("CREATE DATABASE mydb", "mydb", "")
if response, err := c.Query(q); err == nil && response.Error() == nil {
fmt.Println(response.Results)
}
}
*/
func writeMazoPoints() {
// Previously, launch the InfluxDB with docker as follows:
// docker run -d --restart unless-stopped -p 8086:8086 -v influxdb_data_dir:/var/lib/influxdb influxdb:1.5.4-alpine
httpClient, _ := client.NewHTTPClient(client.HTTPConfig{
Addr: "http://localhost:8086",
//Username: "username",
//Password: "password",
})
defer httpClient.Close()
// Use influx DB client and create the database
// docker exec -it <container_id> influx
// CREATE DATABASE pingass
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
Database: "pingass",
Precision: "us",
})
// Point 1 data
p1Timestamp := "1585475270"
p1TimestampInt, _ := strconv.ParseInt(p1Timestamp, 10, 64)
tm1 := time.Unix(p1TimestampInt, 0)
tags1 := map[string]string{"site": "sqex.com"}
fields1 := map[string]interface{}{
"status": 0,
}
// Point 2 data
p2Timestamp := "1585475438"
p2TimestampInt, _ := strconv.ParseInt(p2Timestamp, 10, 64)
tm2 := time.Unix(p2TimestampInt, 0)
tags2 := map[string]string{"site": "pepe.com"}
fields2 := map[string]interface{}{
"status": 1,
}
// Point 1 creation
p1, _ := client.NewPoint(
"websites", // This is the "measurement" https://docs.influxdata.com/influxdb/v1.7/concepts/key_concepts/#measurement
tags1,
fields1,
tm1,
)
// Point 2 creation
p2, _ := client.NewPoint(
"websites", // This is the "measurement" https://docs.influxdata.com/influxdb/v1.7/concepts/key_concepts/#measurement
tags2,
fields2,
tm2,
)
//Add point 1 to the list of points to be written
bp.AddPoint(p1)
//Add point 2 to the list of points to be written
bp.AddPoint(p2)
err := httpClient.Write(bp)
if err != nil {
log.Fatal(err)
os.Exit(1)
}
}
func main() {
writeMazoPoints()
}
@axl89
Copy link
Author

axl89 commented Mar 29, 2020

Select all data

select * from websites
name: websites
time                site     status
----                ----     ------
1585475270000000000 gato.com 0
1585475438000000000 pepe.com 1

Select data with using WHERE clause

> SELECT * from websites WHERE "site" = 'gato.com'
name: websites
time                site     status
----                ----     ------
1585475270000000000 gato.com 0

Select data in a time range

> SELECT * from websites WHERE time > '2020-03-29T08:00:00Z'
name: websites
time                site     status
----                ----     ------
1585475270000000000 gato.com 0
1585475438000000000 pepe.com 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment