influxdb-client-ruby

This repository contains the reference Ruby client for the InfluxDB 2.x.

Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+ (). For connecting to InfluxDB 1.7 or earlier instances, use the influxdb-ruby client library.

This section contains links to the client library documentation.

Features

InfluxDB 2.x client consists of two packages

    • Querying data using the Flux language
    • Writing data
      • batched in chunks on background
      • automatic retries on write failures
  • influxdb-client-apis
    • provides all other InfluxDB 2.x APIs for managing
      • buckets
      • labels
      • authorizations
    • built on top of influxdb-client

The InfluxDB 2 client is bundled as a gem and is hosted on Rubygems.

The client can be installed manually or with bundler.

To install the client gem manually:

For management API:

  1. gem install influxdb-client-apis -v 2.3.0

Usage

Creating a client

Use InfluxDB::Client to create a client connected to a running InfluxDB 2 instance.

  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token')

Client Options

  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org',
  4. precision: InfluxDB2::WritePrecision::NANOSECOND)

Queries

The result retrieved by could be formatted as a:

  1. Raw query response
  2. Flux data structure: FluxTable, FluxColumn and FluxRecord
  3. Stream of

Query raw

Synchronously executes the Flux query and return result as unprocessed String

  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org')
  4. query_api = client.create_query_api
  5. result = query_api.query_raw(query: 'from(bucket:"' + bucket + '") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()')

Synchronous query

Synchronously executes the Flux query and return result as a Array of FluxTables

  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org')
  4. query_api = client.create_query_api

Query stream

Synchronously executes the Flux query and return stream of

Parameterized queries

InfluxDB Cloud inserts the params object into the Flux query as a Flux record named params. Use dot or bracket notation to access parameters in the params record in your Flux query. Parameterized Flux queries support only int , float, and string data types. To convert the supported data types into other Flux basic data types, use Flux type conversion functions.

Parameterized query example:

  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org')
  4. query = 'from(bucket: params.bucketParam) |> range(start: duration(v: params.startParam))'
  5. params = { 'bucketParam' => 'my-bucket', 'startParam' => '-1h' }
  6. query_api = client.create_query_api
  7. result = query_api.query(query: query, params: params)
  8. result[0].records.each { |record| puts "#{record.time} #{record.measurement}: #{record.field} #{record.value}" }

The supports synchronous and batching writes into InfluxDB 2.x. In default api uses synchronous write. To enable batching you can use WriteOption.

  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org',
  4. precision: InfluxDB2::WritePrecision::NANOSECOND)
  5. write_api = client.create_write_api
  6. write_api.write(data: 'h2o,location=west value=33i 15')

Batching

The writes are processed in batches which are configurable by WriteOptions:

  1. write_options = InfluxDB2::WriteOptions.new(write_type: InfluxDB2::WriteType::BATCHING,
  2. batch_size: 10, flush_interval: 5_000,
  3. max_retries: 3, max_retry_delay: 15_000,
  4. exponential_base: 2)
  5. client = InfluxDB2::Client.new('http://localhost:8086',
  6. 'my-token',
  7. bucket: 'my-bucket',
  8. org: 'my-org',
  9. precision: InfluxDB2::WritePrecision::NANOSECOND,
  10. use_ssl: false)
  11. write_api = client.create_write_api(write_options: write_options)
  12. write_api.write(data: 'h2o,location=west value=33i 15')

Time precision

Configure default time precision:

  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org',

Configure precision per write:

  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org')
  4. write_api = client.create_write_api
  5. write_api.write(data: 'h2o,location=west value=33i 15', precision: InfluxDB2::WritePrecision::SECOND)

Allowed values for precision are:

  • InfluxDB2::WritePrecision::NANOSECOND for nanosecond
  • InfluxDB2::WritePrecision::MICROSECOND for microsecond
  • InfluxDB2::WritePrecision::MILLISECOND for millisecond
  • InfluxDB2::WritePrecision::SECOND for second

Configure destination

Default bucket and organization destination are configured via InfluxDB::Client:

but there is also possibility to override configuration per write:

  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token')
  2. write_api = client.create_write_api
  3. write_api.write(data: 'h2o,location=west value=33i 15', bucket: 'production-data', org: 'customer-1')

Data format

The data could be written as:

  1. String that is formatted as a InfluxDB’s line protocol
  2. Hash with keys: name, tags, fields and time
  3. Data Point structure
  4. Array of above items
  1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org',
  4. precision: InfluxDB2::WritePrecision::NANOSECOND)
  5. point = InfluxDB2::Point.new(name: 'h2o')
  6. .add_tag('location', 'europe')
  7. .add_field('level', 2)
  8. hash = { name: 'h2o',
  9. tags: { host: 'aws', region: 'us' },
  10. fields: { level: 5, saturation: '99%' }, time: 123 }
  11. write_api = client.create_write_api
  12. write_api.write(data: ['h2o,location=west value=33i 15', point, hash])

Default Tags

Sometimes is useful to store same information in every measurement e.g. hostname, location, customer. The client is able to use static value, app settings or env variable as a tag value.

  • California Miner - static value
  • ${env.hostname} - environment property
Via API
  1. client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org',
  4. precision: InfluxDB2::WritePrecision::NANOSECOND,
  5. use_ssl: false,
  6. tags: { id: '132-987-655' })
  7. point_settings = InfluxDB2::PointSettings.new(default_tags: { customer: 'California Miner' })
  8. point_settings.add_default_tag('data_center', '${env.data_center}')
  9. write_api = client.create_write_api(write_options: InfluxDB2::SYNCHRONOUS,
  10. point_settings: point_settings)
  11. write_api.write(data: InfluxDB2::Point.new(name: 'h2o')
  12. .add_tag('location', 'europe')
  13. .add_field('level', 2))

Delete data

The supports deletes points from an InfluxDB bucket.

  1. client = InfluxDB2::Client.new('http://localhost:8086', 'my-token',
  2. bucket: 'my-bucket',
  3. org: 'my-org',
  4. precision: InfluxDB2::WritePrecision::NANOSECOND)
  5. client.create_delete_api.delete(DateTime.rfc3339('2019-02-03T04:05:06+07:00'),
  6. DateTime.rfc3339('2019-03-03T04:05:06+07:00'),
  7. predicate: 'key1="value1" AND key2="value"')

The time range could be specified as:

  1. String - "2019-02-03T04:05:06+07:00"
  2. DateTime - DateTime.rfc3339('2019-03-03T04:05:06+07:00')
  3. Time - Time.utc(2015, 10, 16, 8, 20, 15)

Management API

The client supports following management API:

The following example demonstrates how to use a InfluxDB 2.x Management API to create new bucket. For further information see docs and .

  1. # This is an example how to create new bucket with permission to write.
  2. #
  3. # You could run example via: `cd apis && bundle exec ruby ../examples/create_new_bucket.rb`
  4. #
  5. $LOAD_PATH.unshift File.expand_path('../lib', __dir__)
  6. require 'influxdb-client'
  7. $LOAD_PATH.unshift File.expand_path('../apis/lib', __dir__)
  8. require 'influxdb-client-apis'
  9. url = 'http://localhost:8086'
  10. bucket = 'my-bucket'
  11. org = 'my-org'
  12. token = 'my-token'
  13. client = InfluxDB2::Client.new(url,
  14. token,
  15. bucket: bucket,
  16. org: org,
  17. use_ssl: false,
  18. precision: InfluxDB2::WritePrecision::NANOSECOND)
  19. api = InfluxDB2::API::Client.new(client)
  20. # Find my organization
  21. organization = api.create_organizations_api
  22. .get_orgs
  23. .orgs
  24. .select { |it| it.name == 'my-org' }
  25. .first
  26. #
  27. # Create new Bucket
  28. #
  29. retention_rule = InfluxDB2::API::RetentionRule.new(type: 'expire', every_seconds: 3600)
  30. bucket_name = 'new-bucket-name'
  31. request = InfluxDB2::API::PostBucketRequest.new(org_id: organization.id,
  32. name: bucket_name,
  33. retention_rules: [retention_rule])
  34. bucket = api.create_buckets_api
  35. .post_buckets(request)
  36. #
  37. # Create Permission to read/write from Bucket
  38. #
  39. resource = InfluxDB2::API::Resource.new(type: 'buckets',
  40. id: bucket.id,
  41. org_id: organization.id)
  42. authorization = InfluxDB2::API::Authorization.new(description: "Authorization to read/write bucket: #{bucket.name}",
  43. org_id: organization.id,
  44. permissions: [
  45. InfluxDB2::API::Permission.new(action: 'read', resource: resource),
  46. InfluxDB2::API::Permission.new(action: 'write', resource: resource)
  47. ])
  48. result = api.create_authorizations_api
  49. .post_authorizations(authorization)
  50. print("The token: '#{result.token}' is authorized to read/write from/to bucket: '#{bucket.name}'.")
  51. client.close!

Server availability can be checked using the client.ping method. That is equivalent of the .

Proxy configuration

You can configure the client to tunnel requests through an HTTP proxy. To configure the proxy use a http_proxy environment variable.

Client automatically follows HTTP redirects. The default redirect policy is to follow up to 10 consecutive requests. You can configure redirect counts by the client property: max_redirect_count.

Due to a security reason Authorization header is not forwarded when redirect leads to a different domain. To overcome this limitation you have to set the client property redirect_forward_authorization to true.

InfluxDB 1.8 API compatibility

InfluxDB 1.8.0 introduced forward compatibility APIs for InfluxDB 2.x. This allow you to easily move from InfluxDB 1.x to InfluxDB 2.x Cloud or open source.

The following forward compatible APIs are available:

For detail info see .

Local tests

  1. brew install wget # on a mac, if not yet installed!

Bug reports and pull requests are welcome on GitHub at https://github.com/influxdata/influxdb-client-ruby.

License