Skip to content

Commit 2edefa3

Browse files
committed
feat: add support for serialization FluxTable into JSON
1 parent d22a361 commit 2edefa3

File tree

6 files changed

+417
-2
lines changed

6 files changed

+417
-2
lines changed

examples/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
## Queries
1212
- [query.py](query.py) - How to query data into `FluxTable`s, `Stream` and `CSV`
1313
- [query_from_file.py](query_from_file.py) - How to use a Flux query defined in a separate file
14+
- [query_response_to_json.py](query_response_to_json.py) - How to serialize Query response to JSON
1415

1516

1617
## Management API

examples/query_response_to_json.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from influxdb_client import InfluxDBClient, Point
2+
from influxdb_client.client.write_api import SYNCHRONOUS
3+
4+
with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") as client:
5+
6+
"""
7+
Prepare data
8+
"""
9+
_point1 = Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
10+
_point2 = Point("my_measurement").tag("location", "New York").field("temperature", 24.3)
11+
12+
client.write_api(write_options=SYNCHRONOUS).write(bucket="my-bucket", record=[_point1, _point2])
13+
14+
"""
15+
Query: using Table structure
16+
"""
17+
tables = client.query_api().query('from(bucket:"my-bucket") |> range(start: -10m)')
18+
19+
"""
20+
Serialize to JSON
21+
"""
22+
import json
23+
from influxdb_client.client.flux_table import FluxStructureEncoder
24+
25+
output = json.dumps(tables, cls=FluxStructureEncoder, indent=2)
26+
print(output)

influxdb_client/client/flux_table.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
The data model consists of tables, records, columns.
55
"""
6+
from json import JSONEncoder
67

78

89
class FluxStructure:
@@ -11,8 +12,32 @@ class FluxStructure:
1112
pass
1213

1314

15+
class FluxStructureEncoder(JSONEncoder):
16+
"""The FluxStructure encoder to encode query results to JSON."""
17+
18+
def default(self, obj):
19+
"""Return serializable objects for JSONEncoder."""
20+
import datetime
21+
if isinstance(obj, FluxStructure):
22+
return obj.__dict__
23+
elif isinstance(obj, (datetime.datetime, datetime.date)):
24+
return obj.isoformat()
25+
return super().default(obj)
26+
27+
1428
class FluxTable(FluxStructure):
15-
"""A table is set of records with a common set of columns and a group key."""
29+
"""
30+
A table is set of records with a common set of columns and a group key.
31+
32+
The table can be serialized into JSON by::
33+
34+
import json
35+
from influxdb_client.client.flux_table import FluxStructureEncoder
36+
37+
output = json.dumps(tables, cls=FluxStructureEncoder, indent=2)
38+
print(output)
39+
40+
"""
1641

1742
def __init__(self) -> None:
1843
"""Initialize defaults."""

0 commit comments

Comments
 (0)