-
Couldn't load subscription status.
- Fork 16
chore: refactor init and extract Query API #93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7a34292
refactor: extract query API
alespour e8eb784
test: add pytest.ini with custom marker definition
alespour 32ce8ea
fix: polars pkg check
alespour b1912e9
test: remove unused import
alespour 56089aa
fix: remove unused optional kwargs
alespour File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| """Query data in InfluxDB 3.""" | ||
|
|
||
| # coding: utf-8 | ||
| import json | ||
|
|
||
| from pyarrow.flight import FlightClient, Ticket, FlightCallOptions, FlightStreamReader | ||
| from influxdb_client_3.version import USER_AGENT | ||
|
|
||
|
|
||
| class QueryApi(object): | ||
| """ | ||
| Implementation for '/api/v2/query' endpoint. | ||
|
|
||
| Example: | ||
| .. code-block:: python | ||
|
|
||
| from influxdb_client import InfluxDBClient | ||
|
|
||
|
|
||
| # Initialize instance of QueryApi | ||
| with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") as client: | ||
| query_api = client.query_api() | ||
| """ | ||
|
|
||
| def __init__(self, | ||
| connection_string, | ||
| token, | ||
| flight_client_options, | ||
| **kwargs) -> None: | ||
| """ | ||
| Initialize defaults. | ||
|
|
||
| :param connection_string: Flight/gRPC connection string | ||
| :param token: access token | ||
| :param flight_client_options: Flight client options | ||
| """ | ||
| self._token = token | ||
| self._flight_client_options = flight_client_options or {} | ||
| self._flight_client_options["generic_options"] = [ | ||
| ("grpc.secondary_user_agent", USER_AGENT) | ||
| ] | ||
| self._flight_client = FlightClient(connection_string, **self._flight_client_options) | ||
|
|
||
| def query(self, query: str, language: str, mode: str, database: str, **kwargs): | ||
| """Query data from InfluxDB. | ||
|
|
||
| :param query: The query to execute on the database. | ||
| :param language: The query language. | ||
| :param mode: The mode to use for the query. | ||
| It should be one of "all", "pandas", "polars", "chunk", "reader" or "schema". | ||
| :param database: The database to query from. | ||
| :param kwargs: Additional arguments to pass to the ``FlightCallOptions headers``. | ||
| For example, it can be used to set up per request headers. | ||
| :keyword query_parameters: The query parameters to use in the query. | ||
| It should be a ``dictionary`` of key-value pairs. | ||
| :return: The query result in the specified mode. | ||
| """ | ||
| from influxdb_client_3 import polars as has_polars, _merge_options as merge_options | ||
| try: | ||
| # Create an authorization header | ||
| optargs = { | ||
| "headers": [(b"authorization", f"Bearer {self._token}".encode('utf-8'))], | ||
| "timeout": 300 | ||
| } | ||
| opts = merge_options(optargs, exclude_keys=['query_parameters'], custom=kwargs) | ||
| _options = FlightCallOptions(**opts) | ||
|
|
||
| # | ||
| # Ticket data | ||
| # | ||
| ticket_data = { | ||
| "database": database, | ||
| "sql_query": query, | ||
| "query_type": language | ||
| } | ||
| # add query parameters | ||
| query_parameters = kwargs.get("query_parameters", None) | ||
| if query_parameters: | ||
| ticket_data["params"] = query_parameters | ||
|
|
||
| ticket = Ticket(json.dumps(ticket_data).encode('utf-8')) | ||
| flight_reader = self._do_get(ticket, _options) | ||
|
|
||
| mode_funcs = { | ||
| "all": flight_reader.read_all, | ||
| "pandas": flight_reader.read_pandas, | ||
| "chunk": lambda: flight_reader, | ||
| "reader": flight_reader.to_reader, | ||
| "schema": lambda: flight_reader.schema | ||
| } | ||
| if has_polars: | ||
| import polars as pl | ||
| mode_funcs["polars"] = lambda: pl.from_arrow(flight_reader.read_all()) | ||
| mode_func = mode_funcs.get(mode, flight_reader.read_all) | ||
|
|
||
| return mode_func() if callable(mode_func) else mode_func | ||
| except Exception as e: | ||
| raise e | ||
|
|
||
| def _do_get(self, ticket: Ticket, options: FlightCallOptions = None) -> FlightStreamReader: | ||
| return self._flight_client.do_get(ticket, options) | ||
|
|
||
| def close(self): | ||
| """Close the Flight client.""" | ||
| self._flight_client.close() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| [pytest] | ||
| markers = | ||
| integration: marks integration tests (deselect with '-m "not integration"') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.