Skip to content

Commit 93fca31

Browse files
committed
docs: added example with query that is stored in file
1 parent e3d40fe commit 93fca31

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

examples/query.flux

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import "date"
2+
3+
from(bucket: "my-bucket")
4+
|> range(start: -50d)
5+
|> filter(fn: (r) => r["_measurement"] == "weather" and r["_field"] == "temperature")
6+
|> pivot(rowKey:["_time"], columnKey: ["_field"], valueColumn: "_value")
7+
|> map(fn: (r) => ({ r with weekDay: date.weekDay(t: r._time) }))

examples/query_from_file.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
How to load and execute query that is stored in file.
3+
"""
4+
import calendar
5+
import random
6+
from datetime import datetime, timedelta
7+
8+
from pytz import UTC
9+
10+
from influxdb_client import InfluxDBClient, Point
11+
from influxdb_client.client.write_api import SYNCHRONOUS
12+
13+
client = InfluxDBClient(url="http://localhost:9999", token="my-token", org="my-org")
14+
15+
write_api = client.write_api(write_options=SYNCHRONOUS)
16+
query_api = client.query_api()
17+
18+
"""
19+
Prepare data
20+
"""
21+
22+
_points = []
23+
now = datetime.now(UTC).replace(hour=13, minute=20, second=15, microsecond=0)
24+
for i in range(50):
25+
_point = Point("weather")\
26+
.tag("location", "New York")\
27+
.field("temperature", random.randint(-10, 30))\
28+
.time(now - timedelta(days=i))
29+
_points.append(_point)
30+
31+
write_api.write(bucket="my-bucket", record=_points)
32+
33+
"""
34+
Query: using Flux from file
35+
"""
36+
with open('query.flux', 'r') as file:
37+
query = file.read()
38+
39+
tables = query_api.query(query)
40+
41+
for table in tables:
42+
for record in table.records:
43+
day_name = calendar.day_name[record["weekDay"]]
44+
print(f'Temperature in {record["location"]} is {record["temperature"]}°C at {day_name}')
45+
46+
"""
47+
Close client
48+
"""
49+
client.__del__()
50+
51+
52+
53+
54+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy