Get a collection (Python)
Gets a reference to a collection for use with the Data API clients.
|
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
Result
Returns a Collection object that corresponds to the specified collection name.
This method returns a Collection object even for collections that don’t exist.
Unless you specify the document_type parameter, the collection is typed as Collection[dict].
For more information, see Typing support.
Signature
Use the get_collection method, which belongs to the astrapy.Database class.
get_collection(
name: str,
*,
document_type: type[Any],
keyspace: str,
spawn_api_options: APIOptions,
) -> Collection
Alternatively, use the database["COLLECTION_NAME"] or database.COLLECTION_NAME syntax.
Parameters
| Name | Type | Summary |
|---|---|---|
|
|
The name of the collection. |
|
|
Optional.
A formal specifier for the type checker.
If provided, Default: |
|
|
Optional if you specified a working keyspace when you created the Default: The working keyspace set when you created the |
|
|
Optional.
A complete or partial specification of the APIOptions to override the defaults inherited from the |
Examples
The following examples demonstrate how to get a collection.
Get a collection
The following example uses untyped documents or rows, but you can define a client-side type for your collection to help statically catch errors. For examples, see Typing support.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get a database
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
keyspace="KEYSPACE_NAME",
)
# Get a collection
collection = database.get_collection("COLLECTION_NAME")
Errors
For a full list of Data API error codes and their corresponding messages, see Data API errors and error codes.
For more information about how the client handles underlying Data API errors, see Python client error handling.
Client reference
For more information, see the client reference.