Skip to content

Commit f991a2c

Browse files
adds sql query method for stream metadata [ch251]
1 parent 90f03bd commit f991a2c

3 files changed

Lines changed: 86 additions & 1 deletion

File tree

btrdb/conn.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import os
1919
import re
20+
import json
2021
import uuid as uuidlib
2122

2223
import grpc
@@ -102,6 +103,43 @@ class BTrDB(object):
102103
def __init__(self, endpoint):
103104
self.ep = endpoint
104105

106+
def query(self, stmt, params=[]):
107+
"""
108+
Performs a SQL query on the database metadata and returns a list of
109+
dictionaries from the resulting cursor.
110+
111+
Parameters
112+
----------
113+
stmt: str
114+
a SQL statement to be executed on the BTrDB metadata. Available
115+
tables are noted below. To sanitize inputs use a `$1` style parameter such as
116+
`select * from streams where name = $1 or name = $2`.
117+
params: list or tuple
118+
a list of parameter values to be sanitized and interpolated into the
119+
SQL statement. Using parameters forces value/type checking and is
120+
considered a best practice at the very least.
121+
122+
Returns
123+
-------
124+
list
125+
a list of dictionary object representing the cursor results.
126+
127+
128+
Notes
129+
-------
130+
Parameters will be inserted into the SQL statement as noted by the
131+
paramter number such as `$1`, `$2`, or `$3`. The `streams` table is
132+
available for `SELECT` statements only.
133+
134+
See https://btrdb.readthedocs.io/en/latest/ for more info.
135+
"""
136+
return [
137+
json.loads(row.decode("utf-8"))
138+
for page in self.ep.sql_query(stmt, params)
139+
for row in page
140+
]
141+
142+
105143
def streams(self, *identifiers, versions=None):
106144
"""
107145
Returns a StreamSet object with BTrDB streams from the supplied

btrdb/endpoint.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,3 +212,9 @@ def generateCSV(self, queryType, start, end, width, depth, includeVersions, *str
212212
for result in self.stub.GenerateCSV(params):
213213
BTrDBError.checkProtoStat(result.stat)
214214
yield result.row
215+
216+
def sql_query(self, stmt, params=[]):
217+
request = btrdb_pb2.SQLQueryParams(query=stmt, params=params)
218+
for page in self.stub.SQLQuery(request):
219+
BTrDBError.checkProtoStat(page.stat)
220+
yield page.SQLQueryRow

docs/source/working/stream-query-manage.rst

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ supplied UUID cannot be found then :code:`None` will be returned.
4444
stream = conn.stream_from_uuid("71466a91-dcfe-42ea-9e88-87c51f847942")
4545
4646
47-
Query by collection
47+
Finding by collection
4848
--------------------------
4949
You can also search for multiple streams by collection using the server object's
5050
:code:`streams_in_collection` method which will return a simple list of
@@ -56,3 +56,44 @@ detail.
5656
5757
conn = btrdb.connect()
5858
streams = conn.streams_in_collection("NORTHEAST/NH")
59+
60+
61+
Querying Metadata
62+
-----------------
63+
Finally, you can query for metadata using standard SQL although at the moment, only the
64+
`streams` table is available. SQL queries can be submitted using the `query`
65+
method which accepts both a `stmt` and `params` argument. The `stmt` should
66+
contain the SQL you'd like executed with parameter placeholders such as `$1` or
67+
`$2` as shown below.
68+
69+
.. code-block:: python
70+
71+
conn = btrdb.connect()
72+
stmt = "select uuid from streams where name = $1 or name = $2"
73+
params = ["Boston_1", "Boston_2"]
74+
75+
for row in conn.query(stmt, params):
76+
print(row)
77+
78+
The SQL query results are returned as a list of dictionaries where each key
79+
matches a column from your SQL projection. You can choose your columns from the
80+
schema of the streams table as follows.
81+
82+
83+
+------------------+------------------------+-----------+
84+
| Column | Type | Nullable |
85+
+==================+========================+===========+
86+
| uuid | uuid | not null |
87+
+------------------+------------------------+-----------+
88+
| collection | character varying(256) | not null |
89+
+------------------+------------------------+-----------+
90+
| name | character varying(256) | not null |
91+
+------------------+------------------------+-----------+
92+
| unit | character varying(256) | not null |
93+
+------------------+------------------------+-----------+
94+
| ingress | character varying(256) | not null |
95+
+------------------+------------------------+-----------+
96+
| property_version | bigint | not null |
97+
+------------------+------------------------+-----------+
98+
| annotations | hstore | |
99+
+------------------+------------------------+-----------+

0 commit comments

Comments
 (0)