Skip to content

Commit

Permalink
feat: write/query (#6)
Browse files Browse the repository at this point in the history
* feat: write/query

* refactor: removing external dependencies

* refactor: types.gen pruned

* refactor: returned dependency on lineprotocol

* refactor: replaced codegen

* fix: removed broken code

* docs: README Usage updated

* lint: fixed according to super-linter

* docs: changelog

* chore: update go for checking CVEs

* test: write, client url

* fix: close client after query

* fix: retry strategy minDelay oveflow

* test: unit tests

* style: lint

* chore: use direct

* test: e2e tests

* feat: removed retry

* chore: rerun CI

* fix: Apply suggestions from code review

Co-authored-by: Jakub Bednář <[email protected]>

* fix: renaming refactor

* feat: iterator first iteration

* test: e2e with iterator

* feat: Retain flight client

* test: fixed error message text

* feat: removed NextValue from iterator

* refactor: rename bucket to database

* feat: example client close error escalation

* feat: use insecure when http provided

* docs: iterator README

* Apply suggestions from code review

Co-authored-by: Jakub Bednář <[email protected]>

* feat: send database in context

* fix: remove duplicit check on StatusCode

* test: client increased coverage

* test: client write, client

* fix: fix url + test

* test: TestWritePointsAndBytes fixed

* fix: use queryParams

* fix: compilation error

---------

Co-authored-by: Jakub Bednar <[email protected]>
  • Loading branch information
Sciator and bednar authored May 19, 2023
1 parent dac26c7 commit 87bb27c
Show file tree
Hide file tree
Showing 27 changed files with 2,789 additions and 50 deletions.
7 changes: 6 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ jobs:
name: Create a temp directory for artifacts
command: |
mkdir -p /tmp/artifacts
- run:
name: Lint go code
command: |
go vet ./...
go install golang.org/x/lint/golint@latest && golint ./...
- run:
name: Run tests
command: go test -v -cover -coverprofile=coverage.out ./...
command: go test -v -cover -coverprofile=coverage.out --tags e2e ./...
- run:
name: Coverage Report
command: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/go-vuln-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Scan for Vulnerabilities in Code
uses: Templum/[email protected]
with:
go-version: 1.20.0
go-version: 1.20.4
vulncheck-version: v0.0.0-20230320232729-bfc1eaef17a4
package: ./...
fail-on-vuln: true
3 changes: 2 additions & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ jobs:
LINTER_RULES_PATH: '.'
MARKDOWN_CONFIG_FILE: .markdownlint.yml
VALIDATE_MARKDOWN: true
VALIDATE_BASH: true
VALIDATE_BASH: true
VALIDATE_GITHUB_ACTIONS: true
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/.idea/
coverage.*
coverage.*
/.vscode/
2 changes: 1 addition & 1 deletion .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"MD013": false,
"MD033": {
"allowed_elements": [ "a", "img", "p" ]
"allowed_elements": [ "a", "img", "p", "details", "summary" ]
},
"MD041": false,
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
## 1.0.0 [unreleased]

- initial release of new client version
- write using v2 api
- query using flightSQL
99 changes: 84 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,37 +41,105 @@ go get github.com/bonitoo-io/influxdb3-go

## Usage

To start with the client, import the `influx` package and create a `influx.Client` by the `NewClient` function:
set environment variables:

- `INFLUXDB_URL` region of your influxdb cloud e.g. *`https://us-east-1-1.aws.cloud2.influxdata.com/`*
- `INFLUXDB_TOKEN` read/write token generated in cloud
- `INFLUXDB_DATABASE` name of database e.g .*`my-database`*

<details>
<summary>linux/macos</summary>

```sh
export INFLUXDB_URL="<url>"
export INFLUXDB_DATABASE="<database>"
export INFLUXDB_TOKEN="<token>"
```

</details>

<details>
<summary>windows</summary>

```powershell
setx INFLUXDB_URL "<url>"
setx INFLUXDB_DATABASE "<database>"
setx INFLUXDB_TOKEN "<token>"
```

</details>

To get started with influxdb client import `influxdb3-go` package.

```go
import (
"github.com/bonitoo-io/influxdb3-go/influx"
"github.com/bonitoo-io/influxdb3-go/influx/configs"
"context"
"encoding/json"
"fmt"
"os"

"github.com/bonitoo-io/influxdb3-go/influx"
)
```

host := "https://eu-central-1-1.aws.cloud2.influxdata.com/"
database := "my-database"
token := "my-token"
Create `influx.Client` with `New` function. Make sure to `Close` client after with `defer` keyword.

client, err := NewClient(configs.ClientConfigs{
Host: &host,
Database: &database,
Token: &token},
)
```go
url := os.Getenv("INFLUXDB_URL")
token := os.Getenv("INFLUXDB_TOKEN")
database := os.Getenv("INFLUXDB_DATABASE")

// Create a new client using an InfluxDB server base URL and an authentication token
client, err := influx.New(influx.Params{
ServerURL: url,
AuthToken: token,
})
// Close client at the end and escalate error if present
defer func (client *influx.Client) {
err := client.Close()
if err != nil {
panic(err)
}
}(client)
```

to insert data, you can use code like this:
The `client` can be now used to insert data using [line-protocol](https://docs.influxdata.com/influxdb/cloud-serverless/reference/syntax/line-protocol/).

```go
// TBD
line := "stat,unit=temperature avg=23.5,max=45.0"
err = client.Write(context.Background(), database, []byte(line))
```

to query your data, you can use code like this:
Fetch data using FlightSQL query and print result.

```go
// TBD
query := `
SELECT *
FROM "stat"
WHERE
time >= now() - interval '5 minute'
AND
"unit" IN ('temperature')
`;

iterator, err := client.Query(context.Background(), database, query, nil)

if err != nil {
panic(err)
}

for iterator.Next() {
value := iterator.Value()

fmt.Printf("avg is %f\n", value["avg"])
fmt.Printf("max is %f\n", value["max"])
}
```

## Example

Prepare environment like in [Usage](#usage) and run `go run ./example/main.go`.

## Feedback

If you need help, please use our [Community Slack](https://app.slack.com/huddle/TH8RGQX5Z/C02UDUPLQKA)
Expand All @@ -87,3 +155,4 @@ the `main` branch.
## License

The InfluxDB 3 Go Client is released under the [MIT License](https://opensource.org/licenses/MIT).
which allows you to execute SQL queries on InfluxDB IOx.
99 changes: 99 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"context"
"fmt"
"os"
"time"

"github.com/bonitoo-io/influxdb3-go/influx"
)

func main() {
// Use env variables to initialize client
url := os.Getenv("INFLUXDB_URL")
token := os.Getenv("INFLUXDB_TOKEN")
database := os.Getenv("INFLUXDB_DATABASE")

// Create a new client using an InfluxDB server base URL and an authentication token
client, err := influx.New(influx.Configs{
HostURL: url,
AuthToken: token,
})

if err != nil {
panic(err)
}
// Close client at the end and escalate error if present
defer func (client *influx.Client) {
err := client.Close()
if err != nil {
panic(err)
}
}(client)

// Create point using full params constructor
p := influx.NewPoint("stat",
map[string]string{"unit": "temperature"},
map[string]interface{}{"avg": 24.5, "max": 45.0},
time.Now())
// write point synchronously
err = client.WritePoints(context.Background(), database, p)
if err != nil {
panic(err)
}
// Create point using fluent style
p = influx.NewPointWithMeasurement("stat").
AddTag("unit", "temperature").
AddField("avg", 23.2).
AddField("max", 45.0).
SetTimestamp(time.Now())
// write point synchronously
err = client.WritePoints(context.Background(), database, p)
if err != nil {
panic(err)
}
// Prepare custom type
sensorData := struct {
Table string `lp:"measurement"`
Unit string `lp:"tag,unit"`
Avg float64 `lp:"field,avg"`
Max float64 `lp:"field,max"`
Time time.Time `lp:"timestamp"`
}{"stat", "temperature", 22.3, 40.3, time.Now()}
// Write point
err = client.WriteData(context.Background(), database, sensorData)
if err != nil {
panic(err)
}
// Or write directly line protocol
line := fmt.Sprintf("stat,unit=temperature avg=%f,max=%f", 23.5, 45.0)
err = client.Write(context.Background(), database, []byte(line))
if err != nil {
panic(err)
}

// Prepare FlightSQL query
query := `
SELECT *
FROM "stat"
WHERE
time >= now() - interval '5 minute'
AND
"unit" IN ('temperature')
`

iterator, err := client.Query(context.Background(), database, query)

if err != nil {
panic(err)
}

for iterator.Next() {
value := iterator.Value()

fmt.Printf("avg is %f\n", value["avg"])
fmt.Printf("max is %f\n", value["max"])
}

}
28 changes: 27 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,36 @@ module github.com/bonitoo-io/influxdb3-go

go 1.20

require github.com/stretchr/testify v1.8.2
require (
github.com/apache/arrow/go/v12 v12.0.0-20230427070130-07d02d6ccaf2
github.com/influxdata/line-protocol/v2 v2.2.1
github.com/stretchr/testify v1.8.0
google.golang.org/grpc v1.54.0
)

require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/apache/thrift v0.16.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v2.0.8+incompatible // indirect
github.com/klauspost/asmfmt v1.3.2 // indirect
github.com/klauspost/compress v1.15.9 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/minio/asm2plan9s v0.0.0-20200509001527-cdd76441f9d8 // indirect
github.com/minio/c2goasm v0.0.0-20190812172519-36a3d3bbc4f3 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/zeebo/xxh3 v1.0.2 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.8.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/tools v0.6.0 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 87bb27c

Please sign in to comment.