examples_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package influxdb2_test
  5. import (
  6. "context"
  7. "fmt"
  8. "github.com/influxdata/influxdb-client-go/v2"
  9. "github.com/influxdata/influxdb-client-go/v2/domain"
  10. )
  11. func ExampleClient_newClient() {
  12. // Create a new client using an InfluxDB server base URL and an authentication token
  13. client := influxdb2.NewClient("http://localhost:8086", "my-token")
  14. // Always close client at the end
  15. defer client.Close()
  16. }
  17. func ExampleClient_newClientWithOptions() {
  18. // Create a new client using an InfluxDB server base URL and an authentication token
  19. // Create client and set batch size to 20
  20. client := influxdb2.NewClientWithOptions("http://localhost:8086", "my-token",
  21. influxdb2.DefaultOptions().SetBatchSize(20))
  22. // Always close client at the end
  23. defer client.Close()
  24. }
  25. func ExampleClient_customServerAPICall() {
  26. // This example shows how to perform custom server API invocation for any endpoint
  27. // Here we will create a DBRP mapping which allows using buckets in legacy write and query (InfluxQL) endpoints
  28. // Create client. You need an admin token for creating DBRP mapping
  29. client := influxdb2.NewClient("http://localhost:8086", "my-token")
  30. // Always close client at the end
  31. defer client.Close()
  32. // Get generated client for server API calls
  33. apiClient := client.APIClient()
  34. ctx := context.Background()
  35. // Get a bucket we would like to query using InfluxQL
  36. b, err := client.BucketsAPI().FindBucketByName(ctx, "my-bucket")
  37. if err != nil {
  38. panic(err)
  39. }
  40. // Get an organization that will own the mapping
  41. o, err := client.OrganizationsAPI().FindOrganizationByName(ctx, "my-org")
  42. if err != nil {
  43. panic(err)
  44. }
  45. yes := true
  46. // Fill required fields of the DBRP struct
  47. dbrp := domain.DBRPCreate{
  48. BucketID: *b.Id,
  49. Database: "my-bucket",
  50. Default: &yes,
  51. OrgID: o.Id,
  52. RetentionPolicy: "autogen",
  53. }
  54. params := &domain.PostDBRPAllParams{
  55. Body: domain.PostDBRPJSONRequestBody(dbrp),
  56. }
  57. // Call server API
  58. newDbrp, err := apiClient.PostDBRP(ctx, params)
  59. if err != nil {
  60. panic(err)
  61. }
  62. // Check generated response errors
  63. fmt.Printf("Created DBRP: %#v\n", newDbrp)
  64. }