diff --git a/README.md b/README.md index 9021d2b7..bf05e318 100644 --- a/README.md +++ b/README.md @@ -85,20 +85,45 @@ for row in csv_result: ### Writes -The [WriteApiClient](https://github.com/bonitoo-io/influxdb-client-python/blob/master/influxdb2/client/write_api.py) supports synchronous, asynchronous and batching writes into InfluxDB 2.0 and could be configured by `WriteOptions`: +The [WriteApi](https://github.com/bonitoo-io/influxdb-client-python/blob/master/influxdb2/client/write_api.py) supports synchronous, asynchronous and batching writes into InfluxDB 2.0. +The data should be passed as a InfluxDB Line Protocol, Data Point or Observable stream. + +_The default instance of `WriteApi` use batching._ + +#### Batching + +The batching is configurable by `write_options`: | Property | Description | Default Value | | --- | --- | --- | -| [**write_type**](#write_type) | how the client writes data; allowed values: `batching`, `asynchronous`, `synchronous`| `batching` | | **batch_size** | the number of data point to collect in batch | `1000` | | **flush_interval** | the number of milliseconds before the batch is written | `1000` | | **jitter_interval** | the number of milliseconds to increase the batch flush interval by a random amount | `0` | | **retry_interval** | the number of milliseconds to retry unsuccessful write. The retry interval is used when the InfluxDB server does not specify "Retry-After" header. | `1000` | -##### write_type -* `batching` - data are writes in batches defined by `batch_size`, `flush_interval`, ... -* `asynchronous` - data are writes in asynchronous HTTP request -* `synchronous` - data are writes in synchronous HTTP request + +##### Asynchronous client +Data are writes in an asynchronous HTTP request. + +```python +from influxdb2.client.influxdb_client import InfluxDBClient +from influxdb2.client.write_api import ASYNCHRONOUS + +client = InfluxDBClient(url="http://localhost:9999/api/v2", token="my-token-123", org="my-org", debug=True) +write_client = client.write_api(write_options=ASYNCHRONOUS) +``` + +##### Synchronous client + +Data are writes in a synchronous HTTP request. + +```python +from influxdb2.client.influxdb_client import InfluxDBClient +from influxdb2.client.write_api import SYNCHRONOUS + +client = InfluxDBClient(url="http://localhost:9999/api/v2", token="my-token-123", org="my-org", debug=True) +write_client = client.write_api(write_options=SYNCHRONOUS) +``` #### How to efficiently import large dataset diff --git a/influxdb2/client/write/point.py b/influxdb2/client/write/point.py index b6622863..826a9048 100644 --- a/influxdb2/client/write/point.py +++ b/influxdb2/client/write/point.py @@ -9,7 +9,7 @@ from influxdb2.domain.write_precision import WritePrecision EPOCH = UTC.localize(datetime.utcfromtimestamp(0)) - +DEFAULT_WRITE_PRECISION = WritePrecision.NS class Point(object): """ @@ -27,9 +27,9 @@ def __init__(self, measurement_name): self._fields = {} self._name = measurement_name self._time = None - self._write_precision = WritePrecision.NS + self._write_precision = DEFAULT_WRITE_PRECISION - def time(self, time, write_precision=WritePrecision.NS): + def time(self, time, write_precision=DEFAULT_WRITE_PRECISION): self._write_precision = write_precision self._time = time return self @@ -111,7 +111,7 @@ def escape_value(value): return value.translate(str.maketrans({'\"': r"\"", "\\": r"\\"})) -def _convert_timestamp(timestamp, precision=WritePrecision.NS): +def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION): if isinstance(timestamp, Integral): return timestamp # assume precision is correct if timestamp is int diff --git a/influxdb2/client/write_api.py b/influxdb2/client/write_api.py index f5ba44c5..965dc038 100644 --- a/influxdb2/client/write_api.py +++ b/influxdb2/client/write_api.py @@ -11,9 +11,8 @@ from rx.scheduler import ThreadPoolScheduler from rx.subject import Subject -from influxdb2 import WritePrecision from influxdb2.client.abstract_client import AbstractClient -from influxdb2.client.write.point import Point +from influxdb2.client.write.point import Point, DEFAULT_WRITE_PRECISION from influxdb2.rest import ApiException logger = logging.getLogger(__name__) @@ -37,7 +36,6 @@ def __init__(self, write_type=WriteType.batching, batch_size=1_000, flush_interv self.write_scheduler = write_scheduler -DEFAULT_WRITE_PRECISION = WritePrecision.NS SYNCHRONOUS = WriteOptions(write_type=WriteType.synchronous) ASYNCHRONOUS = WriteOptions(write_type=WriteType.asynchronous)