6
6
from influxdb_client import WriteOptions as _WriteOptions
7
7
from influxdb_client .client .write_api import WriteApi as _WriteApi
8
8
from influxdb_client .client .write_api import SYNCHRONOUS , ASYNCHRONOUS
9
+ from influxdb_client .client .write_api import PointSettings
10
+ from influxdb_client .domain .write_precision import WritePrecision
9
11
from influxdb_client import Point
10
12
import json
11
13
@@ -14,6 +16,7 @@ def write_options(**kwargs):
14
16
15
17
16
18
19
+
17
20
class InfluxDBClient3 :
18
21
def __init__ (self , host = None , org = None , database = None , token = None , write_options = None , ** kwargs ):
19
22
"""
@@ -27,15 +30,9 @@ def __init__(self, host=None, org=None, database=None, token=None, write_options
27
30
"""
28
31
self ._org = org
29
32
self ._database = database
30
- self .write_options = write_options
31
-
32
- if self .write_options == None :
33
- self .write_options = SYNCHRONOUS
34
-
35
-
33
+ self .write_options = write_options if write_options is not None else SYNCHRONOUS
36
34
self ._client = _InfluxDBClient (url = f"https://{ host } " , token = token , org = self ._org , ** kwargs )
37
35
self ._write_api = _WriteApi (self ._client , write_options = self .write_options )
38
-
39
36
self ._flight_client = FlightClient (f"grpc+tls://{ host } :443" )
40
37
# create an authorization header
41
38
self ._options = FlightCallOptions (headers = [(b"authorization" ,f"Bearer { token } " .encode ('utf-8' ))])
@@ -66,7 +63,6 @@ def write_csv(self, csv_file, measurement_name=None, tag_columns = [],timestamp_
66
63
atable = csv .read_csv (csv_file , ** kwargs )
67
64
68
65
df = atable .to_pandas ()
69
- print (df )
70
66
self ._write_api .write (bucket = self ._database , record = df ,
71
67
data_frame_measurement_name = measurement_name ,
72
68
data_frame_tag_columns = tag_columns ,
@@ -96,8 +92,21 @@ def query(self, query, language="sql"):
96
92
# which is useful if you have huge data sets
97
93
return flight_reader .read_all ()
98
94
99
- def __del__ (self ):
100
- self ._write_api .__del__ ()
101
- return self ._client .__del__ ()
102
95
103
- __all__ = ["InfluxDBClient3" , "Point" ]
96
+ def close (self ):
97
+ # Clean up resources here.
98
+ # Call close method of _write_api and _flight_client, if they exist.
99
+ if hasattr (self ._write_api , 'close' ):
100
+ self ._write_api .close ()
101
+ if hasattr (self ._flight_client , 'close' ):
102
+ self ._flight_client .close ()
103
+ if hasattr (self ._client , 'close' ):
104
+ self ._client .close ()
105
+
106
+ def __enter__ (self ):
107
+ return self
108
+
109
+ def __exit__ (self , exc_type , exc_val , exc_tb ):
110
+ self .close ()
111
+
112
+ __all__ = ["InfluxDBClient3" , "Point" , "PointSettings" , "SYNCHRONOUS" , "ASYNCHRONOUS" , "write_options" , "WritePrecision" ]
0 commit comments