Skip to content

Commit 97d4453

Browse files
Merge pull request #18 from InfluxCommunity/17-add-ability-to-expose-batching-options
exposed write options
2 parents 11cc6ce + 7066452 commit 97d4453

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

Examples/cloud_dedicated_example.py renamed to Examples/cloud_dedicated_query.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
client = InfluxDBClient3.InfluxDBClient3(token="",
66
host="b0c7cce5-8dbc-428e-98c6-7f996fb96467.a.influxdb.io",
77
org="6a841c0c08328fb1",
8-
database="flight2", write_options="SYNCHRONOUS")
8+
database="flight2")
99

1010

1111
table = client.query(query="SELECT * FROM flight WHERE time > now() - 4h", language="influxql")

Examples/cloud_dedicated_write.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
import influxdb_client_3 as InfluxDBClient3
3+
from influxdb_client_3 import write_options
4+
import pandas as pd
5+
import numpy as np
6+
7+
8+
9+
client = InfluxDBClient3.InfluxDBClient3(token="",
10+
host="b0c7cce5-8dbc-428e-98c6-7f996fb96467.a.influxdb.io",
11+
org="6a841c0c08328fb1",
12+
database="flight2", write_options=write_options(batch_size=500,
13+
flush_interval=10_000,
14+
jitter_interval=2_000,
15+
retry_interval=5_000,
16+
max_retries=5,
17+
max_retry_delay=30_000,
18+
max_close_wait=300_000,
19+
exponential_base=2, write_type='batching'))
20+
21+
22+
23+
24+
25+
# Create a dataframe
26+
df = pd.DataFrame({'a': [1, 2, 3], 'b': [4, 5, 6]})
27+
28+
29+
# Create a range of datetime values
30+
dates = pd.date_range(start='2023-05-01', end='2023-05-29', freq='5min')
31+
32+
# Create a DataFrame with random data and datetime index
33+
df = pd.DataFrame(np.random.randn(len(dates), 3), index=dates, columns=['Column 1', 'Column 2', 'Column 3'])
34+
df['tagkey'] = 'Hello World'
35+
36+
print(df)
37+
38+
# Write the DataFrame to InfluxDB
39+
client.write(df, data_frame_measurement_name='table', data_frame_tag_columns=['tagkey'])

Examples/csv_write.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
import pandas as pd
33
import numpy as np
44

5-
client = InfluxDBClient3.InfluxDBClient3(token="pjvlpe-olsMjvdmw8pejTIqkknI2KUGIUG-mIrrT824Y7H_tMkSwPqXn6IoEKnwqoGU8WznsJJWk4N5uI8MBsw==",
5+
client = InfluxDBClient3.InfluxDBClient3(token="",
66
host="eu-central-1-1.aws.cloud2.influxdata.com",
77
org="6a841c0c08328fb1",
8-
database="test", write_options="SYNCHRONOUS")
8+
database="test")
99

1010

1111

influxdb_client_3/__init__.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33
from pyarrow import csv
44
from pyarrow.flight import FlightClient, Ticket, FlightCallOptions
55
from influxdb_client import InfluxDBClient as _InfluxDBClient
6+
from influxdb_client import WriteOptions as _WriteOptions
67
from influxdb_client.client.write_api import WriteApi as _WriteApi
78
from influxdb_client.client.write_api import SYNCHRONOUS, ASYNCHRONOUS
89
from influxdb_client import Point
910
import json
1011

12+
def write_options(**kwargs):
13+
return _WriteOptions(**kwargs)
14+
15+
16+
1117
class InfluxDBClient3:
12-
def __init__(self, host=None, org=None, database=None, token=None, write_options="SYNCHRONOUS", **kwargs):
18+
def __init__(self, host=None, org=None, database=None, token=None, write_options=None, **kwargs):
1319
"""
1420
This class provides an interface for interacting with an InfluxDB server, simplifying common operations such as writing, querying.
1521
* host (str, optional): The hostname or IP address of the InfluxDB server. Defaults to None.
@@ -21,13 +27,10 @@ def __init__(self, host=None, org=None, database=None, token=None, write_options
2127
"""
2228
self._org = org
2329
self._database = database
30+
self.write_options = write_options
2431

25-
if write_options == "SYNCHRONOUS":
32+
if self.write_options == None:
2633
self.write_options = SYNCHRONOUS
27-
elif write_options == "ASYNCHRONOUS":
28-
self.write_options = ASYNCHRONOUS
29-
else:
30-
raise ValueError("write_options must be SYNCHRONOUS or ASYNCHRONOUS")
3134

3235

3336
self._client = _InfluxDBClient(url=f"https://{host}", token=token, org=self._org, **kwargs )

0 commit comments

Comments
 (0)