-
Notifications
You must be signed in to change notification settings - Fork 47
docs: update onboarding steps for Go v3 client #6987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,13 +61,14 @@ export const WriteDataSqlComponent = (props: OwnProps) => { | |
}, [bucket, onSelectBucket]) | ||
|
||
const initializeCodeSnippet = `package main | ||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
"os" | ||
"github.com/InfluxCommunity/influxdb3-go/influxdb3" | ||
"github.com/InfluxCommunity/influxdb3-go/v2/influxdb3" | ||
) | ||
func main() { | ||
|
@@ -95,55 +96,61 @@ func main() { | |
database := "${bucket.name}" | ||
}` | ||
|
||
const writeCodeSnippet = `data := map[string]map[string]interface{}{ | ||
"point1": { | ||
"location": "Klamath", | ||
"species": "bees", | ||
"count": 23, | ||
}, | ||
"point2": { | ||
"location": "Portland", | ||
"species": "ants", | ||
"count": 30, | ||
}, | ||
"point3": { | ||
"location": "Klamath", | ||
"species": "bees", | ||
"count": 28, | ||
}, | ||
"point4": { | ||
"location": "Portland", | ||
"species": "ants", | ||
"count": 32, | ||
}, | ||
"point5": { | ||
"location": "Klamath", | ||
"species": "bees", | ||
"count": 29, | ||
}, | ||
"point6": { | ||
"location": "Portland", | ||
"species": "ants", | ||
"count": 40, | ||
}, | ||
} | ||
const writeCodeSnippet = ` data := map[string]map[string]interface{}{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious to see what effect this whitespace has on the snippet There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The instruction is to add this snippet at the end of the func main() {
// Create client
url := "http://localhost:8080"
token := os.Getenv("INFLUXDB_TOKEN")
// Create a new client using an InfluxDB server base URL and an authentication token
client, err := influxdb3.New(influxdb3.ClientConfig{
Host: url,
Token: token,
})
if err != nil {
panic(err)
}
// Close client at the end and escalate error if present
defer func (client *influxdb3.Client) {
err := client.Close()
if err != nil {
panic(err)
}
}(client)
database := "my-bucket"
data := map[string]map[string]interface{}{
"point1": {
"location": "Klamath",
"species": "bees",
"count": 23,
},
"point2": {
"location": "Portland",
"species": "ants",
"count": 30,
},
"point3": {
"location": "Klamath",
"species": "bees",
"count": 28,
},
"point4": {
"location": "Portland",
"species": "ants",
"count": 32,
},
"point5": {
"location": "Klamath",
"species": "bees",
"count": 29,
},
"point6": {
"location": "Portland",
"species": "ants",
"count": 40,
},
}
// convert data to Points
points := []*influxdb3.Point{}
// start time stamp
stamp := time.Now().Add(time.Duration(len(data)) * time.Second * -1)
for key := range data {
points = append(points,
influxdb3.NewPointWithMeasurement("census").
SetTag("location", data[key]["location"].(string)).
SetIntegerField(data[key]["species"].(string),
int64(data[key]["count"].(int))).
SetTimestamp(stamp))
stamp = stamp.Add(time.Second) // Add a second to the stamp
}
// Write data
fmt.Printf("Writing %d points\n", len(points))
if err := client.WritePoints(context.Background(),
points,
influxdb3.WithDatabase(database)); err != nil {
panic(err)
}
} |
||
"point1": { | ||
"location": "Klamath", | ||
"species": "bees", | ||
"count": 23, | ||
}, | ||
"point2": { | ||
"location": "Portland", | ||
"species": "ants", | ||
"count": 30, | ||
}, | ||
"point3": { | ||
"location": "Klamath", | ||
"species": "bees", | ||
"count": 28, | ||
}, | ||
"point4": { | ||
"location": "Portland", | ||
"species": "ants", | ||
"count": 32, | ||
}, | ||
"point5": { | ||
"location": "Klamath", | ||
"species": "bees", | ||
"count": 29, | ||
}, | ||
"point6": { | ||
"location": "Portland", | ||
"species": "ants", | ||
"count": 40, | ||
}, | ||
} | ||
// Write data | ||
options := influxdb3.WriteOptions{ | ||
Database: database, | ||
} | ||
for key := range data { | ||
point := influxdb3.NewPointWithMeasurement("census"). | ||
AddTag("location", data[key]["location"].(string)). | ||
AddField(data[key]["species"].(string), data[key]["count"]) | ||
// convert data to Points | ||
points := []*influxdb3.Point{} | ||
// start time stamp | ||
stamp := time.Now().Add(time.Duration(len(data)) * time.Second * -1) | ||
if err := client.WritePointsWithOptions(context.Background(), &options, point); err != nil { | ||
panic(err) | ||
for key := range data { | ||
points = append(points, | ||
influxdb3.NewPointWithMeasurement("census"). | ||
SetTag("location", data[key]["location"].(string)). | ||
SetIntegerField(data[key]["species"].(string), | ||
int64(data[key]["count"].(int))). | ||
SetTimestamp(stamp)) | ||
stamp = stamp.Add(time.Second) // Add a second to the stamp | ||
} | ||
time.Sleep(1 * time.Second) // separate points by 1 second | ||
} | ||
// Write data | ||
fmt.Printf("Writing %d points\\n", len(points)) | ||
if err := client.WritePoints(context.Background(), | ||
points, | ||
influxdb3.WithDatabase(database)); err != nil { | ||
panic(err) | ||
} | ||
` | ||
|
||
return ( | ||
|
@@ -328,7 +335,7 @@ for key := range data { | |
<CodeSnippet | ||
language="properties" | ||
onCopy={logCopyRunCodeSnippet} | ||
text="go run ./main.go" | ||
text="go mod tidy && go run ./main.go" | ||
/> | ||
<p> | ||
The program should write data once you run it. After the data is | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also curious about the whitespace here, looks like the number of spaces in a tab has changed, is that maintained across all snippets in the go onboarding?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is the
main()
method with all copy-pasted snippets.