-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: added an example how to use RxPY and sync batching (#202)
- Loading branch information
Showing
5 changed files
with
105 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Examples | ||
|
||
## Writes | ||
- [import_data_set.py](import_data_set.py) - How to import CSV file | ||
- [import_data_set_multiprocessing.py](import_data_set_multiprocessing.py) - How to large CSV file by Python Multiprocessing | ||
- [ingest_dataframe_default_tags.py](ingest_dataframe_default_tags.py) - How to ingest DataFrame with default tags | ||
- [iot_sensor.py](iot_sensor.py) - How to write sensor data every minute by [RxPY](https://rxpy.readthedocs.io/en/latest/) | ||
- [import_data_set_sync_batching.py](import_data_set_sync_batching.py) - How to use [RxPY](https://rxpy.readthedocs.io/en/latest/) to prepare batches for synchronous write into InfluxDB | ||
|
||
## 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 | ||
|
||
## Others | ||
- [influx_cloud.py](influx_cloud.py) - How to connect to InfluxDB 2 Cloud | ||
- [influxdb_18_example.py](influxdb_18_example.py) - How to connect to InfluxDB 1.8 | ||
- [nanosecond_precision.py](nanosecond_precision.py) - How to use nanoseconds precision | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
How to use RxPY to prepare batches for synchronous write into InfluxDB | ||
""" | ||
|
||
from csv import DictReader | ||
|
||
import rx | ||
from rx import operators as ops | ||
|
||
from influxdb_client import InfluxDBClient, Point | ||
from influxdb_client.client.write.retry import WritesRetry | ||
from influxdb_client.client.write_api import SYNCHRONOUS | ||
|
||
|
||
def csv_to_generator(csv_file_path): | ||
""" | ||
Parse your CSV file into generator | ||
""" | ||
for row in DictReader(open(csv_file_path, 'r')): | ||
point = Point('financial-analysis') \ | ||
.tag('type', 'vix-daily') \ | ||
.field('open', float(row['VIX Open'])) \ | ||
.field('high', float(row['VIX High'])) \ | ||
.field('low', float(row['VIX Low'])) \ | ||
.field('close', float(row['VIX Close'])) \ | ||
.time(row['Date']) | ||
yield point | ||
|
||
|
||
""" | ||
Define Retry strategy - 3 attempts => 2, 4, 8 | ||
""" | ||
retries = WritesRetry(total=3, backoff_factor=1, exponential_base=2) | ||
client = InfluxDBClient(url='http://localhost:8086', token='my-token', org='my-org', retries=retries) | ||
|
||
""" | ||
Use synchronous version of WriteApi to strongly depends on result of write | ||
""" | ||
write_api = client.write_api(write_options=SYNCHRONOUS) | ||
|
||
""" | ||
Prepare batches from generator | ||
""" | ||
batches = rx \ | ||
.from_iterable(csv_to_generator('vix-daily.csv')) \ | ||
.pipe(ops.buffer_with_count(500)) | ||
|
||
|
||
def write_batch(batch): | ||
""" | ||
Synchronous write | ||
""" | ||
print(f'Writing... {len(batch)}') | ||
write_api.write(bucket='my-bucket', record=batch) | ||
|
||
|
||
""" | ||
Write batches | ||
""" | ||
batches.subscribe(on_next=lambda batch: write_batch(batch), | ||
on_error=lambda ex: print(f'Unexpected error: {ex}'), | ||
on_completed=lambda: print('Import finished!')) | ||
|
||
""" | ||
Dispose client | ||
""" | ||
write_api.close() | ||
client.close() |