influxdb3-python 0.1.8__tar.gz → 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: influxdb3-python
3
- Version: 0.1.8
3
+ Version: 0.2.0
4
4
  Summary: Community Python client for InfluxDB 3.0
5
5
  Home-page: https://github.com/InfluxCommunity/influxdb3-python
6
6
  Author: InfluxData
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: influxdb3-python
3
- Version: 0.1.8
3
+ Version: 0.2.0
4
4
  Summary: Community Python client for InfluxDB 3.0
5
5
  Home-page: https://github.com/InfluxCommunity/influxdb3-python
6
6
  Author: InfluxData
@@ -1,22 +1,43 @@
1
- import json
2
- import urllib.parse
1
+ import urllib.parse, json
3
2
  import pyarrow as pa
4
3
  from influxdb_client import InfluxDBClient as _InfluxDBClient, WriteOptions, Point
5
4
  from influxdb_client.client.write_api import WriteApi as _WriteApi, SYNCHRONOUS, ASYNCHRONOUS, PointSettings
6
5
  from influxdb_client.domain.write_precision import WritePrecision
7
6
  from influxdb_client.client.exceptions import InfluxDBError
8
7
  from pyarrow.flight import FlightClient, Ticket, FlightCallOptions
9
- from influxdb_client_3.read_file import upload_file
8
+ from influxdb_client_3.read_file import UploadFile
10
9
 
11
10
 
12
11
  def write_client_options(**kwargs):
12
+ """
13
+ Function for providing additional arguments for the WriteApi client.
14
+
15
+ :param kwargs: Additional arguments for the WriteApi client.
16
+ :return: dict with the arguments.
17
+ """
13
18
  return kwargs
14
19
 
15
20
  def default_client_options(**kwargs):
16
21
  return kwargs
17
22
 
18
23
  def flight_client_options(**kwargs):
19
- return kwargs # You can replace this with a specific data structure if needed
24
+ """
25
+ Function for providing additional arguments for the FlightClient.
26
+
27
+ :param kwargs: Additional arguments for the FlightClient.
28
+ :return: dict with the arguments.
29
+ """
30
+ return kwargs
31
+
32
+ def file_parser_options(**kwargs):
33
+ """
34
+ Function for providing additional arguments for the file parser.
35
+
36
+ :param kwargs: Additional arguments for the file parser.
37
+ :return: dict with the arguments.
38
+ """
39
+ return kwargs
40
+
20
41
 
21
42
  class InfluxDBClient3:
22
43
  def __init__(
@@ -39,14 +60,15 @@ class InfluxDBClient3:
39
60
  :type database: str
40
61
  :param token: The authentication token for accessing the InfluxDB server.
41
62
  :type token: str
42
- :param write_client_options: Options for the WriteAPI.
43
- :type write_client_options: dict
44
- :param flight_client_options: Options for the FlightClient.
45
- :type flight_client_options: dict
63
+ :param write_client_options: Function for providing additional arguments for the WriteApi client.
64
+ :type write_client_options: callable
65
+ :param flight_client_options: Function for providing additional arguments for the FlightClient.
66
+ :type flight_client_options: callable
46
67
  :param kwargs: Additional arguments for the InfluxDB Client.
47
68
  """
48
- self._org = org
69
+ self._org = org if org is not None else "default"
49
70
  self._database = database
71
+ self._token = token
50
72
  self._write_client_options = write_client_options if write_client_options is not None else default_client_options(write_options=SYNCHRONOUS)
51
73
 
52
74
  # Extracting the hostname from URL if provided
@@ -55,7 +77,7 @@ class InfluxDBClient3:
55
77
 
56
78
  self._client = _InfluxDBClient(
57
79
  url=f"https://{host}",
58
- token=token,
80
+ token=self._token,
59
81
  org=self._org,
60
82
  **kwargs)
61
83
 
@@ -63,24 +85,28 @@ class InfluxDBClient3:
63
85
  self._flight_client_options = flight_client_options or {}
64
86
  self._flight_client = FlightClient(f"grpc+tls://{host}:443", **self._flight_client_options)
65
87
 
66
- # Create an authorization header
67
- self._options = FlightCallOptions(headers=[(b"authorization", f"Bearer {token}".encode('utf-8'))])
68
88
 
69
- def write(self, record=None, **kwargs):
89
+
90
+ def write(self, record=None, database=None ,**kwargs):
70
91
  """
71
92
  Write data to InfluxDB.
72
93
 
73
94
  :param record: The data point(s) to write.
74
- :type record: Point or list of Point objects
95
+ :type record: object or list of objects
96
+ :param database: The database to write to. If not provided, uses the database provided during initialization.
97
+ :type database: str
75
98
  :param kwargs: Additional arguments to pass to the write API.
76
99
  """
100
+ if database is None:
101
+ database = self._database
102
+
77
103
  try:
78
- self._write_api.write(bucket=self._database, record=record, **kwargs)
104
+ self._write_api.write(bucket=database, record=record, **kwargs)
79
105
  except InfluxDBError as e:
80
106
  raise e
81
107
 
82
108
 
83
- def write_file(self, file, measurement_name=None, tag_columns=None, timestamp_column='time', **kwargs):
109
+ def write_file(self, file, measurement_name=None, tag_columns=None, timestamp_column='time', database=None, file_parser_options=None ,**kwargs):
84
110
  """
85
111
  Write data from a file to InfluxDB.
86
112
 
@@ -92,17 +118,24 @@ class InfluxDBClient3:
92
118
  :type tag_columns: list
93
119
  :param timestamp_column: Timestamp column name. Defaults to 'time'.
94
120
  :type timestamp_column: str
121
+ :param database: The database to write to. If not provided, uses the database provided during initialization.
122
+ :type database: str
123
+ :param file_parser_options: Function for providing additional arguments for the file parser.
124
+ :type file_parser_options: callable
95
125
  :param kwargs: Additional arguments to pass to the write API.
96
126
  """
127
+ if database is None:
128
+ database = self._database
129
+
97
130
  try:
98
- table = upload_file(file).load_file()
131
+ table = UploadFile(file, file_parser_options).load_file()
99
132
  df = table.to_pandas() if isinstance(table, pa.Table) else table
100
- self._process_dataframe(df, measurement_name, tag_columns or [], timestamp_column)
133
+ self._process_dataframe(df, measurement_name, tag_columns or [], timestamp_column, database=database, **kwargs)
101
134
  except Exception as e:
102
135
  raise e
103
136
 
104
137
 
105
- def _process_dataframe(self, df, measurement_name, tag_columns, timestamp_column):
138
+ def _process_dataframe(self, df, measurement_name, tag_columns, timestamp_column, database, **kwargs):
106
139
  # This function is factored out for clarity.
107
140
  # It processes a DataFrame before writing to InfluxDB.
108
141
 
@@ -120,27 +153,35 @@ class InfluxDBClient3:
120
153
  print("'measurement' column not found in the dataframe.")
121
154
  else:
122
155
  df = df.drop(columns=['measurement'], errors='ignore')
123
- self._write_api.write(bucket=self._database, record=df,
156
+ self._write_api.write(bucket=database, record=df,
124
157
  data_frame_measurement_name=measurement_name,
125
158
  data_frame_tag_columns=tag_columns,
126
- data_frame_timestamp_column=timestamp_column)
159
+ data_frame_timestamp_column=timestamp_column, **kwargs)
127
160
 
128
- def query(self, query, language="sql", mode="all"):
161
+ def query(self, query, language="sql", mode="all", database=None, **kwargs ):
129
162
  """
130
163
  Query data from InfluxDB.
131
164
 
132
165
  :param query: The query string.
133
166
  :type query: str
134
- :param language: The query language (default is "sql").
167
+ :param language: The query language; "sql" or "influxql" (default is "sql").
135
168
  :type language: str
136
169
  :param mode: The mode of fetching data (all, pandas, chunk, reader, schema).
137
170
  :type mode: str
171
+ :param database: The database to query from. If not provided, uses the database provided during initialization.
172
+ :type database: str
173
+ :param kwargs: Additional arguments for the query.
138
174
  :return: The queried data.
139
175
  """
176
+ if database is None:
177
+ database = self._database
178
+
140
179
  try:
141
- ticket_data = {"database": self._database, "sql_query": query, "query_type": language}
180
+ # Create an authorization header
181
+ _options = FlightCallOptions(headers=[(b"authorization", f"Bearer {self._token}".encode('utf-8'))], **kwargs)
182
+ ticket_data = {"database": database, "sql_query": query, "query_type": language}
142
183
  ticket = Ticket(json.dumps(ticket_data).encode('utf-8'))
143
- flight_reader = self._flight_client.do_get(ticket, self._options)
184
+ flight_reader = self._flight_client.do_get(ticket, _options)
144
185
 
145
186
  mode_func = {
146
187
  "all": flight_reader.read_all,
@@ -176,5 +217,6 @@ __all__ = [
176
217
  "WritePrecision",
177
218
  "WriteOptions",
178
219
  "write_client_options",
179
- "flight_client_options"
220
+ "flight_client_options",
221
+ "file_parser_options"
180
222
  ]
@@ -0,0 +1,99 @@
1
+ import os
2
+ import pyarrow.csv as csv
3
+ import pyarrow.feather as feather
4
+ import pyarrow.parquet as parquet
5
+ import pandas as pd
6
+
7
+ # Check if the OS is not Windows
8
+ if os.name != 'nt':
9
+ import pyarrow.orc as orc
10
+
11
+
12
+ class UploadFile:
13
+ """
14
+ Class for uploading and reading different types of files.
15
+ """
16
+ def __init__(self, file, file_parser_options=None):
17
+ """
18
+ Initialize an UploadFile instance.
19
+
20
+ :param file: The file to upload.
21
+ :type file: str
22
+ :param kwargs: Additional arguments for file loading functions.
23
+ """
24
+ self._file = file
25
+ self._kwargs = file_parser_options if file_parser_options is not None else {}
26
+
27
+ def load_file(self):
28
+ """
29
+ Load a file based on its extension.
30
+
31
+ :return: The loaded file.
32
+ :raises ValueError: If the file type is not supported.
33
+ """
34
+ if self._file.endswith(".feather"):
35
+ return self.load_feather(self._file)
36
+ elif self._file.endswith(".parquet"):
37
+ return self.load_parquet(self._file)
38
+ elif self._file.endswith(".csv"):
39
+ return self.load_csv(self._file)
40
+ elif self._file.endswith(".json"):
41
+ return self.load_json(self._file)
42
+ elif self._file.endswith(".orc"):
43
+ return self.load_orc(self._file)
44
+ else:
45
+ raise ValueError("Unsupported file type")
46
+
47
+ def load_feather(self, file ):
48
+ """
49
+ Load a Feather file.
50
+
51
+ :param file: The Feather file to load.
52
+ :type file: str
53
+ :return: The loaded Feather file.
54
+ """
55
+ return feather.read_table(file, **self._kwargs)
56
+
57
+ def load_parquet(self, file):
58
+ """
59
+ Load a Parquet file.
60
+
61
+ :param file: The Parquet file to load.
62
+ :type file: str
63
+ :return: The loaded Parquet file.
64
+ """
65
+ return parquet.read_table(file, **self._kwargs)
66
+
67
+ def load_csv(self, file):
68
+ """
69
+ Load a CSV file.
70
+
71
+ :param file: The CSV file to load.
72
+ :type file: str
73
+ :return: The loaded CSV file.
74
+ """
75
+ return csv.read_csv(file, **self._kwargs)
76
+
77
+ def load_orc(self, file):
78
+ """
79
+ Load an ORC file.
80
+
81
+ :param file: The ORC file to load.
82
+ :type file: str
83
+ :return: The loaded ORC file.
84
+ :raises ValueError: If the OS is Windows.
85
+ """
86
+ if os.name == 'nt':
87
+ raise ValueError("Unsupported file type for this OS")
88
+ else:
89
+ return orc.read_table(file, **self._kwargs)
90
+
91
+ def load_json(self, file):
92
+ """
93
+ Load a JSON file.
94
+
95
+ :param file: The JSON file to load.
96
+ :type file: str
97
+ :return: The loaded JSON file.
98
+ """
99
+ return pd.read_json(file, **self._kwargs)
@@ -1,51 +0,0 @@
1
- from pyarrow import json as pa_json
2
- import pyarrow.csv as csv
3
- import pyarrow.feather as feather
4
- import pyarrow.parquet as parquet
5
- import os
6
-
7
- # Check if the OS is not Windows
8
- if os.name != 'nt':
9
- import pyarrow.orc as orc
10
-
11
- import pandas as pd
12
-
13
- class upload_file:
14
- def __init__(self, file, **kwargs):
15
- self._file = file
16
- self._kwargs = kwargs
17
-
18
- def load_file(self):
19
- if self._file.endswith(".feather"):
20
- return self.load_feather(self._file, **self._kwargs)
21
- elif self._file.endswith(".parquet"):
22
- return self.load_parquet(self._file)
23
- elif self._file.endswith(".csv"):
24
- return self.load_csv(self._file)
25
- elif self._file.endswith(".json"):
26
- return self.load_json(self._file)
27
- elif self._file.endswith(".orc"):
28
-
29
- return self.load_orc(self._file)
30
- else:
31
- raise ValueError("Unsupported file type")
32
-
33
- def load_feather(self, file):
34
- return feather.read_table(file, **self._kwargs)
35
-
36
- def load_parquet(self, file):
37
- return parquet.read_table(file, **self._kwargs)
38
-
39
- def load_csv(self, file):
40
- return csv.read_csv(file, **self._kwargs)
41
-
42
- def load_orc(self, file):
43
- if os.name == 'nt':
44
- raise ValueError("Unsupported file type for this OS")
45
- else:
46
- return orc.read_table(file, **self._kwargs)
47
-
48
- #TODO: Use pyarrow.json.read_json() instead of pandas.read_json()
49
- def load_json(self, file):
50
- return pd.read_json(file, **self._kwargs)
51
-