|
1 | 1 | # Couchbase Python Analytics Client |
2 | 2 | Python client for [Couchbase](https://couchbase.com) Analytics. |
| 3 | + |
| 4 | +Currently Python 3.9 - Python 3.13 is supported. |
| 5 | + |
| 6 | +The Analytics SDK supports static typing. Currently only [mypy](https://github.com/python/mypy) is supported. You mileage may vary (YMMV) with the use of other static type checkers (e.g. [pyright](https://github.com/microsoft/pyright)). |
| 7 | + |
| 8 | +# Installing the SDK<a id="installing-the-sdk"></a> |
| 9 | + |
| 10 | +Until a version is available on PyPI, the SDK can be installed via pip with the following command (note the `dev` branch in the url). |
| 11 | + |
| 12 | +Install the SDK via `pip`: |
| 13 | +```console |
| 14 | +python3 -m pip install git+https://github.com/couchbaselabs/analytics-python-client@dev |
| 15 | +``` |
| 16 | + |
| 17 | +# Using the SDK<a id="using-the-sdk"></a> |
| 18 | + |
| 19 | +Some more examples are provided in the [examples directory](https://github.com/couchbaselabs/analytics-python-client/tree/dev/examples). |
| 20 | + |
| 21 | +**Connecting and executing a query** |
| 22 | +```python |
| 23 | +from couchbase_analytics.cluster import Cluster |
| 24 | +from couchbase_analytics.credential import Credential |
| 25 | +from couchbase_analytics.options import QueryOptions |
| 26 | + |
| 27 | + |
| 28 | +def main() -> None: |
| 29 | + # Update this to your cluster |
| 30 | + # IMPORTANT: The appropriate port needs to be specified. The SDK's default ports are 80 (http) and 443 (https). |
| 31 | + # If attempting to connect to Capella, the correct ports are most likely to be 8095 (http) and 18095 (https). |
| 32 | + # Capella example: https://cb.2xg3vwszqgqcrsix.cloud.couchbase.com:18095 |
| 33 | + endpoint = 'https://--your-instance--' |
| 34 | + username = 'username' |
| 35 | + pw = 'password' |
| 36 | + # User Input ends here. |
| 37 | + |
| 38 | + cred = Credential.from_username_and_password(username, pw) |
| 39 | + cluster = Cluster.create_instance(endpoint, cred) |
| 40 | + |
| 41 | + # Execute a query and buffer all result rows in client memory. |
| 42 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline LIMIT 10;' |
| 43 | + res = cluster.execute_query(statement) |
| 44 | + all_rows = res.get_all_rows() |
| 45 | + for row in all_rows: |
| 46 | + print(f'Found row: {row}') |
| 47 | + print(f'metadata={res.metadata()}') |
| 48 | + |
| 49 | + # Execute a query and process rows as they arrive from server. |
| 50 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline WHERE country="United States" LIMIT 10;' |
| 51 | + res = cluster.execute_query(statement) |
| 52 | + for row in res.rows(): |
| 53 | + print(f'Found row: {row}') |
| 54 | + print(f'metadata={res.metadata()}') |
| 55 | + |
| 56 | + # Execute a streaming query with positional arguments. |
| 57 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline WHERE country=$1 LIMIT $2;' |
| 58 | + res = cluster.execute_query(statement, QueryOptions(positional_parameters=['United States', 10])) |
| 59 | + for row in res: |
| 60 | + print(f'Found row: {row}') |
| 61 | + print(f'metadata={res.metadata()}') |
| 62 | + |
| 63 | + # Execute a streaming query with named arguments. |
| 64 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline WHERE country=$country LIMIT $limit;' |
| 65 | + res = cluster.execute_query(statement, QueryOptions(named_parameters={'country': 'United States', |
| 66 | + 'limit': 10})) |
| 67 | + for row in res.rows(): |
| 68 | + print(f'Found row: {row}') |
| 69 | + print(f'metadata={res.metadata()}') |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == '__main__': |
| 73 | + main() |
| 74 | + |
| 75 | +``` |
| 76 | + |
| 77 | +## Using the async API |
| 78 | +```python |
| 79 | +import asyncio |
| 80 | + |
| 81 | +from acouchbase_analytics.cluster import AsyncCluster |
| 82 | +from acouchbase_analytics.credential import Credential |
| 83 | +from acouchbase_analytics.options import QueryOptions |
| 84 | + |
| 85 | + |
| 86 | +async def main() -> None: |
| 87 | + # Update this to your cluster |
| 88 | + # IMPORTANT: The appropriate port needs to be specified. The SDK's default ports are 80 (http) and 443 (https). |
| 89 | + # If attempting to connect to Capella, the correct ports are most likely to be 8095 (http) and 18095 (https). |
| 90 | + # Capella example: https://cb.2xg3vwszqgqcrsix.cloud.couchbase.com:18095 |
| 91 | + endpoint = 'https://--your-instance--' |
| 92 | + username = 'username' |
| 93 | + pw = 'password' |
| 94 | + # User Input ends here. |
| 95 | + |
| 96 | + cred = Credential.from_username_and_password(username, pw) |
| 97 | + cluster = AsyncCluster.create_instance(endpoint, cred) |
| 98 | + |
| 99 | + # Execute a query and buffer all result rows in client memory. |
| 100 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline LIMIT 10;' |
| 101 | + res = await cluster.execute_query(statement) |
| 102 | + all_rows = await res.get_all_rows() |
| 103 | + # NOTE: all_rows is a list, _do not_ use `async for` |
| 104 | + for row in all_rows: |
| 105 | + print(f'Found row: {row}') |
| 106 | + print(f'metadata={res.metadata()}') |
| 107 | + |
| 108 | + # Execute a query and process rows as they arrive from server. |
| 109 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline WHERE country="United States" LIMIT 10;' |
| 110 | + res = await cluster.execute_query(statement) |
| 111 | + async for row in res.rows(): |
| 112 | + print(f'Found row: {row}') |
| 113 | + print(f'metadata={res.metadata()}') |
| 114 | + |
| 115 | + # Execute a streaming query with positional arguments. |
| 116 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline WHERE country=$1 LIMIT $2;' |
| 117 | + res = await cluster.execute_query(statement, QueryOptions(positional_parameters=['United States', 10])) |
| 118 | + async for row in res: |
| 119 | + print(f'Found row: {row}') |
| 120 | + print(f'metadata={res.metadata()}') |
| 121 | + |
| 122 | + # Execute a streaming query with named arguments. |
| 123 | + statement = 'SELECT * FROM `travel-sample`.inventory.airline WHERE country=$country LIMIT $limit;' |
| 124 | + res = await cluster.execute_query(statement, QueryOptions(named_parameters={'country': 'United States', |
| 125 | + 'limit': 10})) |
| 126 | + async for row in res.rows(): |
| 127 | + print(f'Found row: {row}') |
| 128 | + print(f'metadata={res.metadata()}') |
| 129 | + |
| 130 | +if __name__ == '__main__': |
| 131 | + asyncio.run(main()) |
| 132 | + |
| 133 | +``` |
0 commit comments