• influxdb-client-ruby
    • Note: Use this client library with InfluxDB 2.x and InfluxDB 1.8+ ( see details ). For connecting to InfluxDB 1.7 or earlier instances, use the influxdb-ruby client library.
  • Installation
    • Install the Gem
  • Usage
    • Creating a client
      • Client Options
    • Queries
      • Query raw
      • Synchronous query
      • Query stream
    • Writing data
      • Batching
      • Time precision
      • Configure destination
      • Data format
      • Default Tags
        • Via API
    • Delete data
  • Advanced Usage
    • Check the server status
    • InfluxDB 1.8 API compatibility
  • Local tests
  • Contributing
  • License

    Ruby - 图1influxdb-client-ruby

    CircleCI codecov Gem Version License GitHub issues GitHub pull requests Slack Status

    This repository contains the reference Ruby client for the InfluxDB 2.0.

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

    Ruby - 图10Installation

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

    Ruby - 图11Install the Gem

    The client can be installed manually or with bundler.

    To install the client gem manually:

    1. gem install influxdb-client -v 1.9.0

    Ruby - 图12Usage

    Ruby - 图13Creating 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')

    Ruby - 图14Client Options

    OptionDescriptionTypeDefault
    bucketDefault destination bucket for writesStringnone
    orgDefault organization bucket for writesStringnone
    precisionDefault precision for the unix timestamps within the body line-protocolStringnone
    open_timeoutNumber of seconds to wait for the connection to openInteger10
    write_timeoutNumber of seconds to wait for one block of data to be writtenInteger10
    read_timeoutNumber of seconds to wait for one block of data to be readInteger10
    max_redirect_countMaximal number of followed HTTP redirectsInteger10
    use_sslTurn on/off SSL for HTTP communicationbooltrue
    1. client = InfluxDB2::Client.new('https://localhost:8086', 'my-token',
    2. bucket: 'my-bucket',
    3. org: 'my-org',
    4. precision: InfluxDB2::WritePrecision::NANOSECOND)

    Ruby - 图15Queries

    The result retrieved by QueryApi could be formatted as a:

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

    Ruby - 图16Query 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()')

    Ruby - 图17Synchronous 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
    5. result = query_api.query(query: 'from(bucket:"' + bucket + '") |> range(start: 1970-01-01T00:00:00.000000001Z) |> last()')

    Ruby - 图18Query stream

    Synchronously executes the Flux query and return stream of FluxRecord

    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. query = 'from(bucket: "my-bucket") |> range(start: -10m, stop: now()) ' \
    6. "|> filter(fn: (r) => r._measurement == \"#{measurement}\")"
    7. query_api.query_stream(query: query).each do |record|
    8. puts record.to_s
    9. end

    Ruby - 图19Writing data

    The WriteApi supports synchronous and batching writes into InfluxDB 2.0. 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')

    Ruby - 图20Batching

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

    PropertyDescriptionDefault Value
    batchSizethe number of data point to collect in batch1000
    flush_intervalthe number of milliseconds before the batch is written1000
    retry_intervalthe number of milliseconds to retry unsuccessful write. The retry interval is used when the InfluxDB server does not specify “Retry-After” header.5000
    jitter_intervalthe number of milliseconds to increase the batch flush interval by a random amount0
    max_retriesthe number of max retries when write fails5
    max_retry_delaymaximum delay when retrying write in milliseconds180000
    exponential_basethe base for the exponential retry delay, the next delay is computed as retry_interval * exponential_base^(attempts - 1) + random(jitter_interval)5
    batch_abort_on_exceptionthe batching worker will be aborted after failed retry strategyfalse
    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')

    Ruby - 图21Time precision

    Configure default time precision:

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

    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

    Ruby - 图22Configure destination

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

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

    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')

    Ruby - 图23Data 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])

    Ruby - 图24Default 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.

    The expressions:

    • California Miner - static value
    • ${env.hostname} - environment property
    Ruby - 图25Via 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))

    Ruby - 图26Delete data

    The DeleteApi 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)

    Ruby - 图27Advanced Usage

    Ruby - 图28Check the server status

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

    Ruby - 图29InfluxDB 1.8 API compatibility

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

    The following forward compatible APIs are available:

    APIEndpointDescription
    query_api.rb/api/v2/queryQuery data in InfluxDB 1.8.0+ using the InfluxDB 2.0 API and Flux (endpoint should be enabled by flux-enabled option)
    write_api.rb/api/v2/writeWrite data to InfluxDB 1.8.0+ using the InfluxDB 2.0 API
    health_api.rb/healthCheck the health of your InfluxDB instance

    For detail info see InfluxDB 1.8 example.

    Ruby - 图30Local tests

    1. brew install wget # on a mac, if not yet installed!
    2. bin/influxdb-restart.sh
    3. rake test

    Ruby - 图31Contributing

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

    Ruby - 图32License

    The gem is available as open source under the terms of the MIT License.