Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit600c3ed

Browse files
committed
Major refactor post-review
* Rename to "sql_client" remove API from class/examples* Constructor now sets up gRPC client for re-use* Constructor now takes a bucket, one client per bucket* Add close function* Add schemas fucntion* Add tables function
1 parent52bbaf8 commit600c3ed

File tree

9 files changed

+171
-67
lines changed

9 files changed

+171
-67
lines changed

‎README.rst

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ InfluxDB 2.0 client features
8484
- `Nanosecond precision`_
8585
- `Delete data`_
8686
- `Handling Errors`_
87-
- `SQLSupport`_
87+
- `SQLclient support`_
8888
- `Logging`_
8989

9090
Installation
@@ -114,15 +114,23 @@ The python package is hosted on `PyPI <https://pypi.org/project/influxdb-client/
114114

115115
..code-block::sh
116116
117-
pip install'influxdb-client[ciso]'
117+
pip install'influxdb-client'
118118
119119
Then import the package:
120120

121121
..code-block::python
122122
123123
import influxdb_client
124124
125-
If your application uses async/await in Python you can install with the ``async`` extra::
125+
There are additional package extras that will pull in additional dependencies:
126+
127+
* `async`: async/await support
128+
* `ciso`: faster date and time parsing
129+
* `extra`: Pandas and NumPy support
130+
* `sql`: SQL client support
131+
132+
For example if your application uses async/await in Python you can install the
133+
``async`` extra::
126134

127135
$ pip install influxdb-client[async]
128136

@@ -1580,25 +1588,25 @@ Client automatically follows HTTP redirects. The default redirect policy is to f
15801588

15811589
.. marker-asyncio-end
15821590
1583-
SQL Support
1591+
SQLClientSupport
15841592
^^^^^^^^^^^
15851593
.. marker-sql-support-start
15861594
15871595
The ability to query InfluxDB with SQL was introduced with the IOX backend.
1588-
To make use of the SQL support users canmake use of the SQL Query API:
1596+
To make use of the SQL support users cancreate a SQL Client with this library:
15891597

15901598
..code-block::python
15911599
15921600
from influxdb_clientimport InfluxDBClient
15931601
15941602
with InfluxDBClient(url="http://localhost:8086",token="my-token",org="my-org",debug=False)as client:
1595-
query_sql_api= client.query_sql_api()
1596-
reader=query_sql_api.query("my-bucket","select * from cpu limit 10")
1603+
sql_client= client.sql_client()
1604+
reader=sql_client.query("my-bucket","select * from cpu limit 10")
15971605
print(reader.read_all())
15981606
15991607
..warning::
16001608

1601-
The ``QuerySQLApi`` only works with InfluxDB that has SQL support enabled.
1609+
The ``SQLClient`` only works with InfluxDB that has SQL support enabled.
16021610
This does not apply to all InfluxDB versions.
16031611

16041612
.. marker-sql-support-end

‎examples/query_sql.py

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎examples/sql_client.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env python3
2+
"""Demonstrate how to use the SQL client with InfluxDB."""
3+
frominfluxdb_clientimportInfluxDBClient
4+
5+
6+
withInfluxDBClient(url="http://localhost:8086",token="my-token",org="my-org")asclient:
7+
# Each connection is specific to a bucket
8+
sql_client=client.sql_client("my-bucket")
9+
10+
# To help understand the shape and makeup of a table users can use these
11+
# two helper functions.
12+
tables=sql_client.tables()
13+
print(tables)
14+
schemas=sql_client.schemas()
15+
print(schemas)
16+
17+
# The returned result is a stream of data. For large result-sets users can
18+
# iterate through those one-by-one to avoid using large chunks of memory.
19+
withsql_client.query("select * from cpu")asreader:
20+
forbatchinreader:
21+
print(batch)
22+
23+
# For smaller results you might want to read the results at once. You
24+
# can do so by using the `read_all()` method.
25+
withsql_client.query("select * from cpu limit 10")asresult:
26+
data=result.read_all()
27+
print(data)
28+
29+
# To get you data into a Pandas DataFrame use the following helper function
30+
df=data.to_pandas()
31+
32+
# Close the connection to this bucket.
33+
sql_client.close()

‎influxdb_client/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@
384384
frominfluxdb_client.client.labels_apiimportLabelsApi
385385
frominfluxdb_client.client.organizations_apiimportOrganizationsApi
386386
frominfluxdb_client.client.query_apiimportQueryApi
387-
frominfluxdb_client.client.query_sql_apiimportQuerySQLApi
387+
frominfluxdb_client.client.sql_clientimportSQLClient
388388
frominfluxdb_client.client.tasks_apiimportTasksApi
389389
frominfluxdb_client.client.users_apiimportUsersApi
390390
frominfluxdb_client.client.write_apiimportWriteApi,WriteOptions

‎influxdb_client/client/_base.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,12 @@ def _prepare_predicate_request(self, start, stop, predicate):
502502
returnpredicate_request
503503

504504

505+
# noinspection PyMethodMayBeStatic
506+
class_BaseSQLClient(object):
507+
def__init__(self,influxdb_client):
508+
self._influxdb_client=influxdb_client
509+
510+
505511
class_Configuration(Configuration):
506512
def__init__(self):
507513
Configuration.__init__(self)

‎influxdb_client/client/influxdb_client.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
frominfluxdb_client.client.labels_apiimportLabelsApi
1515
frominfluxdb_client.client.organizations_apiimportOrganizationsApi
1616
frominfluxdb_client.client.query_apiimportQueryApi,QueryOptions
17-
frominfluxdb_client.client.query_sql_apiimportQuerySQLApi
17+
frominfluxdb_client.client.sql_clientimportSQLClient
1818
frominfluxdb_client.client.tasks_apiimportTasksApi
1919
frominfluxdb_client.client.users_apiimportUsersApi
2020
frominfluxdb_client.client.write_apiimportWriteApi,WriteOptions,PointSettings
@@ -302,13 +302,13 @@ def query_api(self, query_options: QueryOptions = QueryOptions()) -> QueryApi:
302302
"""
303303
returnQueryApi(self,query_options)
304304

305-
defquery_sql_api(self)->QuerySQLApi:
305+
defsql_client(self,bucket:str)->SQLClient:
306306
"""
307-
Create anQuerySQLAPI instance.
307+
Create an SQLclient instance.
308308
309-
:return:QuerySQLAPI instance
309+
:return: SQLclient instance
310310
"""
311-
returnQuerySQLApi(self)
311+
returnSQLClient(self,bucket)
312312

313313
definvokable_scripts_api(self)->InvokableScriptsApi:
314314
"""

‎influxdb_client/client/query_sql_api.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎influxdb_client/client/sql_client.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""InfluxDB SQL Client."""
2+
fromurllib.parseimporturlparse
3+
4+
frominfluxdb_client.client._baseimport_BaseSQLClient
5+
6+
7+
classSQLClient(_BaseSQLClient):
8+
"""
9+
Implementation for gRPC+TLS client for SQL.
10+
11+
This class provides basic operations for interacting with InfluxDB via SQL.
12+
"""
13+
14+
def__init__(self,influxdb_client,bucket):
15+
"""
16+
Initialize SQL client.
17+
18+
Unlike the previous APIs, this client is is produced for a specific
19+
bucket to query against. Queries to different buckets require different
20+
clients.
21+
22+
To complete SQL requests, a different client is used. The rest of this
23+
client library utilizes REST requests against the published API.
24+
However, for SQL support connections are handled over gRPC+TLS. As such
25+
this client takes the host and client and creates a new client
26+
connection for SQL operations.
27+
28+
:param influxdb_client: influxdb client
29+
"""
30+
super().__init__(influxdb_client=influxdb_client)
31+
32+
fromflightsqlimportFlightSQLClient
33+
self._client=FlightSQLClient(
34+
host=urlparse(self._influxdb_client.url).hostname,
35+
token=self._influxdb_client.token,
36+
metadata={"bucket-name":bucket},
37+
)
38+
39+
defclose(self):
40+
"""Close the client connection."""
41+
self._client.close()
42+
43+
defquery(self,query:str):
44+
"""
45+
Execute synchronous SQL query and return result as an Arrow reader.
46+
47+
:param str, query: the SQL query to execute
48+
:return: PyArrow RecordbatchReader
49+
50+
.. code-block:: python
51+
52+
with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") as client:
53+
# Each connection is specific to a bucket
54+
sql_client = client.sql_client("my-bucket")
55+
56+
# The returned result is a stream of data. For large result-sets users can
57+
# iterate through those one-by-one to avoid using large chunks of memory.
58+
with sql_client.query("select * from cpu") as result:
59+
for r in result:
60+
print(r)
61+
62+
# For smaller results you might want to read the results at once. You
63+
# can do so by using the `read_all()` method.
64+
with sql_client.query("select * from cpu limit 10") as result:
65+
data = result.read_all()
66+
print(data)
67+
68+
# To get you data into a Pandas DataFrame use the following helper function
69+
df = data.to_pandas()
70+
71+
"""# noqa: E501
72+
returnself._get_ticket_info(self._client.execute(query))
73+
74+
defschemas(self):
75+
"""
76+
Returns the schema of the specified bucket.
77+
78+
:return: PyArrow Table
79+
80+
.. code-block:: python
81+
82+
with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") as client:
83+
sql_client = client.sql_client("my-bucket")
84+
print(sql_client.schemas())
85+
86+
"""# noqa: E501
87+
returnself._get_ticket_info(self._client.get_db_schemas()).read_all()
88+
89+
deftables(self):
90+
"""
91+
Return tables available from the specified bucket.
92+
93+
:return: PyArrow Table
94+
95+
.. code-block:: python
96+
97+
with InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org") as client:
98+
sql_client = client.sql_client("my-bucket")
99+
print(sql_client.tables())
100+
101+
"""# noqa: E501
102+
returnself._get_ticket_info(self._client.get_table_types()).read_all()
103+
104+
def_get_ticket_info(self,flightInfo):
105+
"""Helper function to collect results from FlightInfo."""
106+
iflen(flightInfo.endpoints)==0:
107+
raiseValueError("no endpoints received")
108+
returnself._client.do_get(flightInfo.endpoints[0].ticket).to_reader()

‎setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
]
4545

4646
sql_requires= [
47-
'flightsql-dbapi@git+https://github.com/influxdata/flightsql-dbapi.git@fbc9fc1618528cd442a7e22ea11663856b0ecd5d'
47+
'flightsql-dbapi@git+https://github.com/influxdata/flightsql-dbapi.git@fbc9fc1618528cd442a7e22ea11663856b0ecd5d',
48+
'pandas>=0.25.3',
4849
]
4950

5051
withopen('README.rst','r')asf:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp