|
| 1 | +# Copyright 2016-2024. Couchbase, Inc. |
| 2 | +# All Rights Reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | + |
| 17 | +from datetime import timedelta |
| 18 | + |
| 19 | +# NOTE: anyio is a dependency of acouchbase_analytics |
| 20 | +import anyio |
| 21 | + |
| 22 | +from acouchbase_analytics.cluster import AsyncCluster |
| 23 | +from acouchbase_analytics.credential import Credential |
| 24 | +from acouchbase_analytics.options import ClusterOptions, QueryOptions, TimeoutOptions |
| 25 | + |
| 26 | + |
| 27 | +async def main() -> None: |
| 28 | + # Update this to your cluster |
| 29 | + endpoint = 'https://--your-instance--' |
| 30 | + username = 'username' |
| 31 | + pw = 'Password!123' |
| 32 | + # User Input ends here. |
| 33 | + |
| 34 | + cred = Credential.from_username_and_password(username, pw) |
| 35 | + # NOTE: Only an example on how to use options. Not a recommendation. |
| 36 | + timeout_opts = TimeoutOptions(query_timeout=timedelta(seconds=30)) |
| 37 | + cluster = AsyncCluster.create_instance(endpoint, cred, ClusterOptions(timeout_options=timeout_opts)) |
| 38 | + |
| 39 | + # Execute a query and buffer all result rows in client memory. |
| 40 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline LIMIT 10;' |
| 41 | + res = await cluster.execute_query(statement) |
| 42 | + all_rows = await res.get_all_rows() |
| 43 | + # NOTE: all_rows is a list, _do not_ use `async for` |
| 44 | + for row in all_rows: |
| 45 | + print(f'Found row: {row}') |
| 46 | + print(f'metadata={res.metadata()}') |
| 47 | + |
| 48 | + # Execute a query and process rows as they arrive from server. |
| 49 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline WHERE country="United States" LIMIT 10;' |
| 50 | + res = await cluster.execute_query(statement) |
| 51 | + async for row in res.rows(): |
| 52 | + print(f'Found row: {row}') |
| 53 | + print(f'metadata={res.metadata()}') |
| 54 | + |
| 55 | + # Execute a streaming query with positional arguments. |
| 56 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline WHERE country=$1 LIMIT $2;' |
| 57 | + res = await cluster.execute_query(statement, QueryOptions(positional_parameters=['United States', 10])) |
| 58 | + async for row in res: |
| 59 | + print(f'Found row: {row}') |
| 60 | + print(f'metadata={res.metadata()}') |
| 61 | + |
| 62 | + # Execute a streaming query with named arguments. |
| 63 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline WHERE country=$country LIMIT $limit;' |
| 64 | + res = await cluster.execute_query( |
| 65 | + statement, QueryOptions(named_parameters={'country': 'United States', 'limit': 10}) |
| 66 | + ) |
| 67 | + async for row in res.rows(): |
| 68 | + print(f'Found row: {row}') |
| 69 | + print(f'metadata={res.metadata()}') |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == '__main__': |
| 73 | + anyio.run(main) |
0 commit comments