Skip to content

Commit

Permalink
feat: add support for serialization FluxTable into JSON (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored Aug 31, 2021
1 parent 4a16049 commit 8539a9b
Show file tree
Hide file tree
Showing 7 changed files with 418 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features
1. [#319](https://github.com/influxdata/influxdb-client-python/pull/319): Add supports for array expressions in query parameters
2. [#320](https://github.com/influxdata/influxdb-client-python/pull/320): Add JSONEncoder to encode query results to JSON

### Bug Fixes
1. [#321](https://github.com/influxdata/influxdb-client-python/pull/321): Fixes return type for dashboard when `include=properties` is used
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
## Queries
- [query.py](query.py) - How to query data into `FluxTable`s, `Stream` and `CSV`
- [query_from_file.py](query_from_file.py) - How to use a Flux query defined in a separate file
- [query_response_to_json.py](query_response_to_json.py) - How to serialize Query response to JSON


## Management API
Expand Down
26 changes: 26 additions & 0 deletions examples/query_response_to_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS

with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") as client:

"""
Prepare data
"""
_point1 = Point("my_measurement").tag("location", "Prague").field("temperature", 25.3)
_point2 = Point("my_measurement").tag("location", "New York").field("temperature", 24.3)

client.write_api(write_options=SYNCHRONOUS).write(bucket="my-bucket", record=[_point1, _point2])

"""
Query: using Table structure
"""
tables = client.query_api().query('from(bucket:"my-bucket") |> range(start: -10m)')

"""
Serialize to JSON
"""
import json
from influxdb_client.client.flux_table import FluxStructureEncoder

output = json.dumps(tables, cls=FluxStructureEncoder, indent=2)
print(output)
27 changes: 26 additions & 1 deletion influxdb_client/client/flux_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
The data model consists of tables, records, columns.
"""
from json import JSONEncoder


class FluxStructure:
Expand All @@ -11,8 +12,32 @@ class FluxStructure:
pass


class FluxStructureEncoder(JSONEncoder):
"""The FluxStructure encoder to encode query results to JSON."""

def default(self, obj):
"""Return serializable objects for JSONEncoder."""
import datetime
if isinstance(obj, FluxStructure):
return obj.__dict__
elif isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
return super().default(obj)


class FluxTable(FluxStructure):
"""A table is set of records with a common set of columns and a group key."""
"""
A table is set of records with a common set of columns and a group key.
The table can be serialized into JSON by::
import json
from influxdb_client.client.flux_table import FluxStructureEncoder
output = json.dumps(tables, cls=FluxStructureEncoder, indent=2)
print(output)
"""

def __init__(self) -> None:
"""Initialize defaults."""
Expand Down
Loading

0 comments on commit 8539a9b

Please sign in to comment.