-
Notifications
You must be signed in to change notification settings - Fork 19
/
influxdb_18_example.rb
34 lines (26 loc) · 1.07 KB
/
influxdb_18_example.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$LOAD_PATH.unshift File.expand_path('../lib', __dir__)
require 'influxdb-client'
username = 'username'
password = 'password'
database = 'telegraf'
retention_policy = 'autogen'
bucket = "#{database}/#{retention_policy}"
InfluxDB2::Client.use('http://localhost:8086',
"#{username}:#{password}",
bucket: bucket,
org: '-',
use_ssl: false,
precision: InfluxDB2::WritePrecision::NANOSECOND) do |client|
puts '*** Write Points ***'
write_api = client.create_write_api
point = InfluxDB2::Point.new(name: 'mem')
.add_tag('host', 'host1')
.add_field('used_percent', 21.43234543)
puts point.to_line_protocol
write_api.write(data: point)
puts '*** Query Points ***'
query_api = client.create_query_api
query = "from(bucket: \"#{bucket}\") |> range(start: -1h)"
result = query_api.query(query: query)
result[0].records.each { |record| puts "#{record.time} #{record.measurement}: #{record.field} #{record.value}" }
end