Mais conteúdo relacionado Semelhante a Introduction to Flux and Functional Data Scripting (20) Introduction to Flux and Functional Data Scripting2. © 2021 InfluxData. All rights reserved.
2
What is Flux?
A functional data scripting
language designed to query,
process, and act on data.
3. © 2021 InfluxData. All rights reserved.
3
Design Goals
– Turing-complete
– Useable
– Readable
– Flexible
– Extensible
– Testable
– Contributable
– Shareable
10. © 2021 InfluxData. All rights reserved.
10 © 2021 InfluxData. All rights reserved.
10
11. © 2021 InfluxData. All rights reserved.
11 © 2021 InfluxData. All rights reserved.
11
from(bucket: "system-data")
|> range(start: -1h)
|> filter(fn: (r) =>
r._measurement == "mem" and
r._field == "used_percent"
)
|> aggregateWindow(every: 5m, fn: mean)
13. © 2021 InfluxData. All rights reserved.
13 © 2021 InfluxData. All rights reserved.
13
14. © 2021 InfluxData. All rights reserved.
14 © 2021 InfluxData. All rights reserved.
14
15. © 2021 InfluxData. All rights reserved.
15 © 2021 InfluxData. All rights reserved.
15
Stream of tables
17. © 2021 InfluxData. All rights reserved.
17
version userID sessionID _field _value
Table
18. © 2021 InfluxData. All rights reserved.
18
version userID sessionID _field _value
v1 ex123 ex456 example 1.2
v1 ex123 ex456 example 2.2
v1 ex123 ex456 example 2.3
v2 ex123 ex456 example 1.9
v2 ex123 ex456 example 2.5
v2 ex123 ex456 example 3.1
Table
19. © 2021 InfluxData. All rights reserved.
19
version userID sessionID _field _value
v1 ex123 ex456 example 1.2
v1 ex123 ex456 example 2.2
v1 ex123 ex456 example 2.3
v2 ex123 ex456 example 1.9
v2 ex123 ex456 example 2.5
v2 ex123 ex456 example 3.1
Table
Group key: [ ]
20. © 2021 InfluxData. All rights reserved.
20
version userID sessionID _field _value
v1 ex123 ex456 example 1.2
v1 ex123 ex456 example 2.2
v1 ex123 ex456 example 2.3
Tables
Group key: ["version", "userID" ]
version userID sessionID _field _value
v2 ex123 ex456 example 1.9
v2 ex123 ex456 example 2.5
v2 ex123 ex456 example 3.1
21. © 2021 InfluxData. All rights reserved.
21
version userID sessionID _field _value
v1 ex123 ex456 example 1.2
v1 ex123 ex456 example 2.2
v1 ex123 ex456 example 2.3
Tables
Group key: ["version", "userID" ]
version userID sessionID _field _value
v2 ex123 ex456 example 1.9
v2 ex123 ex456 example 2.5
v2 ex123 ex456 example 3.1
22. © 2021 InfluxData. All rights reserved.
22 © 2021 InfluxData. All rights reserved.
22
sql.from(
driverName: "mysql",
dataSourceName: "${username}:${password}@tcp(localhost:3306)/db",
query:"SELECT CustomerName,City FROM Customers;"
)
CustomerName City
John Doe New York
Jane Seymour London
Rufus Peck San Francisco
Group key: [ ]
23. © 2021 InfluxData. All rights reserved.
23 © 2021 InfluxData. All rights reserved.
23
from(bucket: "example-bucket")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "m1" and r.field == "f1")
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:09:00Z m1 host1 f1 1.2
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:28:00Z m1 host1 f1 2.5
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:39:00Z m1 host1 f1 2.2
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:18:00Z m1 host2 f1 1.2
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:24:00Z m1 host2 f1 2.5
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:42:00Z m1 host2 f1 2.2
Group key: ["_start", "_stop", "_measurement", "host", "_field"]
25. © 2021 InfluxData. All rights reserved.
25 © 2021 InfluxData. All rights reserved.
25
Pipe-forward
|>
26. © 2021 InfluxData. All rights reserved.
26 © 2021 InfluxData. All rights reserved.
26
Basic data types
string "example string"
bool true
float 1.20
int 123456789
uint 123456789
duration 1h
time 2019-12-01T00:00:00Z
bytes [104 101 108 108 111]
27. © 2021 InfluxData. All rights reserved.
27 © 2021 InfluxData. All rights reserved.
27
Composite data types
record {key1: "value1", key2: 2.0}
array [12, 34, 56]
dict [1: "foo", 2: "bar"]
function f = (x) => x
28. © 2021 InfluxData. All rights reserved.
28 © 2021 InfluxData. All rights reserved.
28
Variable declarations
varName = varValue
foo = "bar"
bazRecord = {this: 1, that: 8}
data = from(...)
29. © 2021 InfluxData. All rights reserved.
29 © 2021 InfluxData. All rights reserved.
29
Reference composite types
r = {key1: "value1", key2: 2.0}
Dot notation
r.key1
Returns "value1"
Bracket notation
r["key1"]
Returns "value1"
30. © 2021 InfluxData. All rights reserved.
30 © 2021 InfluxData. All rights reserved.
30
Reference composite types
arr = [12, 34, 56]
Bracket notation
arr[0]
Returns 12
31. © 2021 InfluxData. All rights reserved.
31 © 2021 InfluxData. All rights reserved.
31
Reference composite types
dictEx = [1: "foo", 2: "bar"]
Function reference
dict.get(dict: dictEx, key: 1, default: "baz")
Returns "foo"
32. © 2021 InfluxData. All rights reserved.
32 © 2021 InfluxData. All rights reserved.
32
Reference composite types
dictEx = [1: "foo", 2: "bar"]
Function reference
dict.get(dict: dictEx, key: 3, default: "baz")
Returns "baz"
33. © 2021 InfluxData. All rights reserved.
33 © 2021 InfluxData. All rights reserved.
33
Reference composite types
r = {key1: "value1", key2: 2.0}
key = "key1"
r.key Returns an error
r["key"] Returns an error
r["${key}"] Returns an error
r = ["key1": "value1", "key2": "2.0"]
key = "key1"
dict.get(
dict: r,
key: key,
default: ""
) Returns "value1"
34. © 2021 InfluxData. All rights reserved.
34 © 2021 InfluxData. All rights reserved.
34
Functions
f = (x, y) => x / y
f(x: 12, y: 2)
Returns 6
35. © 2021 InfluxData. All rights reserved.
35 © 2021 InfluxData. All rights reserved.
35
Function types
Static
f = (x, y=2) => x / y
Transformation
f = (t=<-) => t
Predicate
(r) => r.key1 == "foo"
36. © 2021 InfluxData. All rights reserved.
36 © 2021 InfluxData. All rights reserved.
36
Predicate functions
(r) => 1.0 == 1.0
Parameter(s) Predicate Expression
37. © 2021 InfluxData. All rights reserved.
37 © 2021 InfluxData. All rights reserved.
37
Predicate functions
(r) => true
38. © 2021 InfluxData. All rights reserved.
38 © 2021 InfluxData. All rights reserved.
38
Predicate functions
(r) => 1.0 != 1.0
39. © 2021 InfluxData. All rights reserved.
39 © 2021 InfluxData. All rights reserved.
39
Predicate functions
(r) => false
40. © 2021 InfluxData. All rights reserved.
40 © 2021 InfluxData. All rights reserved.
40
{
Predicate functions
(r) => 1.0 > 1.0
== != < > <= >= =~ !~
41. © 2021 InfluxData. All rights reserved.
41 © 2021 InfluxData. All rights reserved.
41
Predicate functions
(r) => 1.0 == 1.0 and "foo" != "bar"
42. © 2021 InfluxData. All rights reserved.
42 © 2021 InfluxData. All rights reserved.
42
Predicate functions
(r) => true and true
43. © 2021 InfluxData. All rights reserved.
43 © 2021 InfluxData. All rights reserved.
43
Predicate functions
(r) => true
44. © 2021 InfluxData. All rights reserved.
44 © 2021 InfluxData. All rights reserved.
44
Predicate functions
(r) => 1.0 > 1.0 and "foo" != "bar"
45. © 2021 InfluxData. All rights reserved.
45 © 2021 InfluxData. All rights reserved.
45
Predicate functions
(r) => false and true
46. © 2021 InfluxData. All rights reserved.
46 © 2021 InfluxData. All rights reserved.
46
Predicate functions
(r) => false
47. © 2021 InfluxData. All rights reserved.
47 © 2021 InfluxData. All rights reserved.
47
Predicate functions
(r) => 1.0 > 1.0 or "foo" != "bar"
48. © 2021 InfluxData. All rights reserved.
48 © 2021 InfluxData. All rights reserved.
48
Predicate functions
(r) => true
49. © 2021 InfluxData. All rights reserved.
49 © 2021 InfluxData. All rights reserved.
49
Predicate functions
(r) =>
1.0 == 1.0 and
("foo" == "bar" or "baz" != "quz")
50. © 2021 InfluxData. All rights reserved.
50 © 2021 InfluxData. All rights reserved.
50
Predicate functions
r = {fname: "John", lname: "Doe", age: 42}
(r) => r.fname == "John" and r.age > 40
51. © 2021 InfluxData. All rights reserved.
51 © 2021 InfluxData. All rights reserved.
51
Predicate functions
r = {fname: "Mike", lname: "Smith", age: 29}
(r) => r.fname == "John" and r.age > 40
53. © 2021 InfluxData. All rights reserved.
53 © 2021 InfluxData. All rights reserved.
53
Basic Query
from(bucket: "system-data")
Source
54. © 2021 InfluxData. All rights reserved.
54 © 2021 InfluxData. All rights reserved.
54
Basic Query
sql.from(
driverName: "postgres",
dataSourceName: "...",
query: "..."
)
Source
55. © 2021 InfluxData. All rights reserved.
55 © 2021 InfluxData. All rights reserved.
55
Basic Query
csv.from(csv: "...")
Source
56. © 2021 InfluxData. All rights reserved.
56 © 2021 InfluxData. All rights reserved.
56
Basic Query
from(bucket: "system-data")
Source
57. © 2021 InfluxData. All rights reserved.
57 © 2021 InfluxData. All rights reserved.
57
Basic Query
from(bucket: "system-data")
|> range(start: -1h)
|> filter(fn: (r) =>
r._measurement == "mem" and
r._field == "used_percent")
Source
Filter
58. © 2021 InfluxData. All rights reserved.
58 © 2021 InfluxData. All rights reserved.
58
Basic Query
from(bucket: "system-data")
|> range(start: -1h)
|> filter(fn: (r) =>
r._measurement == "mem" and
r._field == "used_percent")
|> group(columns: ["host"])
Source
Filter
Shape
59. © 2021 InfluxData. All rights reserved.
59 © 2021 InfluxData. All rights reserved.
59
Basic Query
from(bucket: "system-data")
|> range(start: -1h)
|> filter(fn: (r) =>
r._measurement == "mem" and
r._field == "used_percent")
|> group(columns: ["host"])
|> mean()
Source
Filter
Shape
Process
60. © 2021 InfluxData. All rights reserved.
60 © 2021 InfluxData. All rights reserved.
60
Basic Query
from(bucket: "system-data")
|> range(start: -1h)
|> filter(fn: (r) =>
r._measurement == "mem" and
r._field == "used_percent")
|> group(columns: ["host"])
|> mean()
Source
Filter
Shape
Process
61. © 2021 InfluxData. All rights reserved.
61 © 2021 InfluxData. All rights reserved.
61
Process Data
data = from(bucket: "system-data")
|> range(start: -1h)
|> filter(fn: (r) =>
r._measurement == "mem" and
r._field == "used_percent"
)
62. © 2021 InfluxData. All rights reserved.
62 © 2021 InfluxData. All rights reserved.
62
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:09:00Z mem host1 used_percent 62.1
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:28:00Z mem host1 used_percent 63.8
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:39:00Z mem host1 used_percent 65.4
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:18:00Z mem host2 used_percent 44.0
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:24:00Z mem host2 used_percent 52.4
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:42:00Z mem host2 used_percent 50.7
Process Data
63. © 2021 InfluxData. All rights reserved.
63 © 2021 InfluxData. All rights reserved.
63
Process Data
data
|> map(fn: (r) => ({_time: r._time, _value: r._value}))
64. © 2021 InfluxData. All rights reserved.
64 © 2021 InfluxData. All rights reserved.
64
_time _value
2021-01-01T00:09:00Z 62.1
2021-01-01T00:18:00Z 44.0
2021-01-01T00:24:00Z 52.4
2021-01-01T00:28:00Z 63.8
2021-01-01T00:39:00Z 65.4
2021-01-01T00:42:00Z 50.7
Process Data
65. © 2021 InfluxData. All rights reserved.
65 © 2021 InfluxData. All rights reserved.
65
Process Data
data
|> map(fn: (r) => ({r with _value: r._value * 0.01}))
66. © 2021 InfluxData. All rights reserved.
66 © 2021 InfluxData. All rights reserved.
66
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:09:00Z mem host1 used_percent 0.621
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:28:00Z mem host1 used_percent 0.638
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:39:00Z mem host1 used_percent 0.654
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:18:00Z mem host2 used_percent 0.44
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:24:00Z mem host2 used_percent 0.524
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:42:00Z mem host2 used_percent 0.507
Process Data
67. © 2021 InfluxData. All rights reserved.
67 © 2021 InfluxData. All rights reserved.
67
Process Data
data
|> map(fn: (r) => ({
r with
state: if r._value > 65.0 then "high" else "ok"
}))
68. © 2021 InfluxData. All rights reserved.
68 © 2021 InfluxData. All rights reserved.
68
_start _stop _time _measurement host _field _value state
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:09:00Z mem host1 used_percent 62.1 ok
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:28:00Z mem host1 used_percent 63.8 ok
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:39:00Z mem host1 used_percent 65.4 high
_start _stop _time _measurement host _field _value state
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:18:00Z mem host2 used_percent 44.0 ok
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:24:00Z mem host2 used_percent 52.4 ok
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:42:00Z mem host2 used_percent 50.7 ok
Process Data
69. © 2021 InfluxData. All rights reserved.
69 © 2021 InfluxData. All rights reserved.
69
Process Data
data |> mean()
71. © 2021 InfluxData. All rights reserved.
71 © 2021 InfluxData. All rights reserved.
71
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:09:00Z mem host1 used_percent 62.1
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:28:00Z mem host1 used_percent 63.8
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:39:00Z mem host1 used_percent 65.4
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:18:00Z mem host2 used_percent 44.0
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:24:00Z mem host2 used_percent 52.4
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:42:00Z mem host2 used_percent 50.7
Process Data
72. © 2021 InfluxData. All rights reserved.
72 © 2021 InfluxData. All rights reserved.
72
_start _stop _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z mem host1 used_percent 63.8
_start _stop _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z mem host2 used_percent 49.0
Process Data
73. © 2021 InfluxData. All rights reserved.
73 © 2021 InfluxData. All rights reserved.
73
Process Data
data |> sum()
74. © 2021 InfluxData. All rights reserved.
74 © 2021 InfluxData. All rights reserved.
74
_start _stop _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z mem host1 used_percent 191.3
_start _stop _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z mem host2 used_percent 147.1
Process Data
75. © 2021 InfluxData. All rights reserved.
75 © 2021 InfluxData. All rights reserved.
75
Process Data
data |> max()
76. © 2021 InfluxData. All rights reserved.
76 © 2021 InfluxData. All rights reserved.
76
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:39:00Z mem host1 used_percent 65.4
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:24:00Z mem host2 used_percent 52.4
Process Data
77. © 2021 InfluxData. All rights reserved.
77 © 2021 InfluxData. All rights reserved.
77
Process Data
data |> last()
78. © 2021 InfluxData. All rights reserved.
78 © 2021 InfluxData. All rights reserved.
78
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:39:00Z mem host1 used_percent 65.4
_start _stop _time _measurement host _field _value
2021-01-01T00:00:00Z 2021-01-01T01:00:00Z 2021-01-01T00:42:00Z mem host2 used_percent 50.7
Process Data
80. © 2021 InfluxData. All rights reserved.
80 © 2021 InfluxData. All rights reserved.
80
from(bucket: "market-summary")
|> range(start: -6mo)
|> filter(fn: (r) =>
r._measurement == "stockPrices" and
r.symbol == "GME"
)
|> holtWinters(n: 24, seasonality: 12, interval: 1w)
81. © 2021 InfluxData. All rights reserved.
81 © 2021 InfluxData. All rights reserved.
81
heatIndex = (t, h) => //...
from(bucket: "sensor-data")
|> range(start: -1d)
|> filter(fn: (r) => r._measurement == "sensors")
|> filter(fn: (r) => r._field == "temp" or r._field == "hum")
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
|> map(fn: (r) => ({ r with heatIndex: heatIndex(t: r.temp, h: r.hum) }))
82. © 2021 InfluxData. All rights reserved.
82 © 2021 InfluxData. All rights reserved.
82
import "sql"
sensorInfo = sql.from(
driverName: "postgres",
dataSourceName: "postgresql://localhost?sslmode=disable",
query: "SELECT * FROM sensors"
)
sensorMetrics = from(bucket: "example-bucket")
|> range(start: -1h)
|> filter(fn: (r) => r._measurement == "airSensors")
join(tables: {metric: sensorMetrics, info: sensorInfo}, on: ["sensor_id"])
|> group(columns: ["sensor_id", "lastInspected"]),
|> aggregateWindow(
every: 5m,
fn: (tables=<-, column="error") => tables |> count(column: column)
)
84. October 11-12, 2021
Hands-On Flux Training
October 26-27, 2021
Virtual Experience
We look forward to bringing together our
community of developers to learn, interact and
share tips and use cases.
https://www.influxdays.com/influxdays-north-america-2021-virtual-experience/