-
Notifications
You must be signed in to change notification settings - Fork 18
/
record_row_example.rb
37 lines (31 loc) · 1.22 KB
/
record_row_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
35
36
37
require 'influxdb-client'
url = 'http://localhost:8086'
token = 'my-token'
bucket = 'my-bucket'
org = 'my-org'
InfluxDB2::Client.new(url,
token,
bucket: bucket,
org: org,
precision: InfluxDB2::WritePrecision::NANOSECOND,
use_ssl: false) do |client|
# Prepare Data
write_api = client.create_write_api
(1..5).each do |i|
write_api.write(data: "point,table=my-table result=#{i}", bucket: bucket, org: org)
end
# Query data with pivot
query_api = client.create_query_api
query = "from(bucket: \"#{bucket}\") |> range(start: -1m) |> filter(fn: (r) => (r[\"_measurement\"] == \"point\"))
|> pivot(rowKey:[\"_time\"], columnKey: [\"_field\"], valueColumn: \"_value\")"
result = query_api.query(query: query)
# Write data to output
puts '---------------------------------------------- FluxRecord.values ---------------------------------------------'
result[0].records.each do |record|
puts record.values
end
puts '------------------------------------------------ FluxRecord.row ----------------------------------------------'
result[0].records.each do |record|
puts record.row.join(',')
end
end