Class: InfluxDB2::WriteApi

Inherits:
DefaultApi show all
Defined in:
lib/influxdb2/client/write_api.rb

Overview

Write time series data into InfluxDB.

Defined Under Namespace

Classes: BatchItem, BatchItemKey

Constant Summary

Constants inherited from DefaultApi

DefaultApi::DEFAULT_REDIRECT_COUNT, DefaultApi::DEFAULT_TIMEOUT, DefaultApi::HEADER_CONTENT_TYPE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from DefaultApi

create_logger, #log

Constructor Details

#initialize(options:, write_options: SYNCHRONOUS, point_settings: InfluxDB2::PointSettings.new) ⇒ WriteApi

Returns a new instance of WriteApi.

Parameters:

  • options (Hash)

    The options to be used by the client.

  • write_options (WriteOptions) (defaults to: SYNCHRONOUS)

    Write api configuration.

  • point_settings (PointSettings) (defaults to: InfluxDB2::PointSettings.new)

    Default tags configuration



129
130
131
132
133
134
135
# File 'lib/influxdb2/client/write_api.rb', line 129

def initialize(options:, write_options: SYNCHRONOUS, point_settings: InfluxDB2::PointSettings.new)
  super(options: options)
  @write_options = write_options
  @point_settings = point_settings
  @closed = false
  @options[:tags].each { |key, value| point_settings.add_default_tag(key, value) } if @options.key?(:tags)
end

Instance Attribute Details

#closedObject (readonly)

Returns the value of attribute closed.



136
137
138
# File 'lib/influxdb2/client/write_api.rb', line 136

def closed
  @closed
end

Instance Method Details

#close!true

Returns Always true.

Returns:

  • (true)

    Always true.



191
192
193
194
195
# File 'lib/influxdb2/client/write_api.rb', line 191

def close!
  _worker.flush_all unless _worker.nil?
  @closed = true
  true
end

#write(data:, precision: nil, bucket: nil, org: nil) ⇒ Object

Write data into specified Bucket.

)

hash = { name: 'h2o', tags: { host: 'aws', region: 'us' }, fields: { level: 5, saturation: '99%' }, time: 123 }

write(data: ['h2o,location=west value=33i 15', point, hash])

Examples:

write(data:

[
  {
    name: 'cpu',
    tags: { host: 'server_nl', region: 'us' },
    fields: {internal: 5, external: 6},
    time: 1422568543702900257
  },
  {name: 'gpu', fields: {value: 0.9999}}
],
precision: InfluxDB2::WritePrecision::NANOSECOND,
bucket: 'my-bucket',
org: 'my-org'

write(data: 'h2o,location=west value=33i 15')

point = InfluxDB2::Point.new(name: 'h2o')

.add_tag('location', 'europe')
.add_field('level', 2)

Parameters:

  • data (Object)

    DataPoints to write into InfluxDB. The data could be represent by [Hash], [Point], [String] or by collection of these types

  • precision (WritePrecision) (defaults to: nil)

    The precision for the unix timestamps within the body line-protocol

  • bucket (String) (defaults to: nil)

    specifies the destination bucket for writes

  • org (String) (defaults to: nil)

    specifies the destination organization for writes



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/influxdb2/client/write_api.rb', line 170

def write(data:, precision: nil, bucket: nil, org: nil)
  precision_param = precision || @options[:precision]
  bucket_param = bucket || @options[:bucket]
  org_param = org || @options[:org]
  _check('precision', precision_param)
  _check('bucket', bucket_param)
  _check('org', org_param)

  _add_default_tags(data)

  payload = _generate_payload(data, bucket: bucket_param, org: org_param, precision: precision_param)
  return nil if payload.nil?

  if WriteType::BATCHING == @write_options.write_type
    _worker.push(payload)
  else
    write_raw(payload, precision: precision_param, bucket: bucket_param, org: org_param)
  end
end

#write_raw(payload, precision: nil, bucket: nil, org: nil) ⇒ Object

Parameters:

  • payload (String)

    data as String

  • precision (WritePrecision) (defaults to: nil)

    The precision for the unix timestamps within the body line-protocol

  • bucket (String) (defaults to: nil)

    specifies the destination bucket for writes

  • org (String) (defaults to: nil)

    specifies the destination organization for writes



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/influxdb2/client/write_api.rb', line 201

def write_raw(payload, precision: nil, bucket: nil, org: nil)
  precision_param = precision || @options[:precision]
  bucket_param = bucket || @options[:bucket]
  org_param = org || @options[:org]
  _check('precision', precision_param)
  _check('bucket', bucket_param)
  _check('org', org_param)

  return nil unless payload.instance_of?(String) || payload.empty?

  uri = _parse_uri('/api/v2/write')
  uri.query = URI.encode_www_form(bucket: bucket_param, org: org_param, precision: precision_param.to_s)

  _post_text(payload, uri)
end