|
1 | | -import json |
2 | 1 | import urllib.parse |
3 | 2 |
|
4 | 3 | import pyarrow as pa |
5 | | -from pyarrow.flight import FlightClient, Ticket, FlightCallOptions |
| 4 | +import importlib.util |
6 | 5 |
|
| 6 | +from influxdb_client_3.query.query_api import QueryApi as _QueryApi |
7 | 7 | from influxdb_client_3.read_file import UploadFile |
8 | 8 | from influxdb_client_3.write_client import InfluxDBClient as _InfluxDBClient, WriteOptions, Point |
9 | 9 | from influxdb_client_3.write_client.client.exceptions import InfluxDBError |
10 | 10 | from influxdb_client_3.write_client.client.write_api import WriteApi as _WriteApi, SYNCHRONOUS, ASYNCHRONOUS, \ |
11 | 11 | PointSettings |
12 | 12 | from influxdb_client_3.write_client.domain.write_precision import WritePrecision |
13 | | -from influxdb_client_3.version import USER_AGENT |
14 | 13 |
|
15 | | -try: |
16 | | - import polars as pl |
17 | | - |
18 | | - polars = True |
19 | | -except ImportError: |
20 | | - polars = False |
| 14 | +polars = importlib.util.find_spec("polars") is not None |
21 | 15 |
|
22 | 16 |
|
23 | 17 | def write_client_options(**kwargs): |
@@ -144,23 +138,15 @@ def __init__( |
144 | 138 | **kwargs) |
145 | 139 |
|
146 | 140 | self._write_api = _WriteApi(influxdb_client=self._client, **self._write_client_options) |
147 | | - self._flight_client_options = flight_client_options or {} |
148 | 141 |
|
149 | 142 | if query_port_overwrite is not None: |
150 | 143 | port = query_port_overwrite |
151 | | - |
152 | | - gen_opts = [ |
153 | | - ("grpc.secondary_user_agent", USER_AGENT) |
154 | | - ] |
155 | | - |
156 | | - self._flight_client_options["generic_options"] = gen_opts |
157 | | - |
158 | 144 | if scheme == 'https': |
159 | 145 | connection_string = f"grpc+tls://{hostname}:{port}" |
160 | 146 | else: |
161 | 147 | connection_string = f"grpc+tcp://{hostname}:{port}" |
162 | | - |
163 | | - self._flight_client = FlightClient(connection_string, **self._flight_client_options) |
| 148 | + self._query_api = _QueryApi(connection_string=connection_string, token=token, |
| 149 | + flight_client_options=flight_client_options) |
164 | 150 |
|
165 | 151 | def write(self, record=None, database=None, **kwargs): |
166 | 152 | """ |
@@ -258,48 +244,14 @@ def query(self, query: str, language: str = "sql", mode: str = "all", database: |
258 | 244 | database = self._database |
259 | 245 |
|
260 | 246 | try: |
261 | | - # Create an authorization header |
262 | | - optargs = { |
263 | | - "headers": [(b"authorization", f"Bearer {self._token}".encode('utf-8'))], |
264 | | - "timeout": 300 |
265 | | - } |
266 | | - opts = _merge_options(optargs, exclude_keys=['query_parameters'], custom=kwargs) |
267 | | - _options = FlightCallOptions(**opts) |
268 | | - |
269 | | - # |
270 | | - # Ticket data |
271 | | - # |
272 | | - ticket_data = { |
273 | | - "database": database, |
274 | | - "sql_query": query, |
275 | | - "query_type": language |
276 | | - } |
277 | | - # add query parameters |
278 | | - query_parameters = kwargs.get("query_parameters", None) |
279 | | - if query_parameters: |
280 | | - ticket_data["params"] = query_parameters |
281 | | - |
282 | | - ticket = Ticket(json.dumps(ticket_data).encode('utf-8')) |
283 | | - flight_reader = self._flight_client.do_get(ticket, _options) |
284 | | - |
285 | | - mode_func = { |
286 | | - "all": flight_reader.read_all, |
287 | | - "pandas": flight_reader.read_pandas, |
288 | | - "polars": lambda: pl.from_arrow(flight_reader.read_all()), |
289 | | - "chunk": lambda: flight_reader, |
290 | | - "reader": flight_reader.to_reader, |
291 | | - "schema": lambda: flight_reader.schema |
292 | | - |
293 | | - }.get(mode, flight_reader.read_all) |
294 | | - |
295 | | - return mode_func() if callable(mode_func) else mode_func |
296 | | - except Exception as e: |
| 247 | + return self._query_api.query(query=query, language=language, mode=mode, database=database, **kwargs) |
| 248 | + except InfluxDBError as e: |
297 | 249 | raise e |
298 | 250 |
|
299 | 251 | def close(self): |
300 | 252 | """Close the client and clean up resources.""" |
301 | 253 | self._write_api.close() |
302 | | - self._flight_client.close() |
| 254 | + self._query_api.close() |
303 | 255 | self._client.close() |
304 | 256 |
|
305 | 257 | def __enter__(self): |
|
0 commit comments