Get a collection (Java)
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<T> object that corresponds to the specified collection name.
This method returns a Collection<T> object even for collections that don’t exist.
A Collection is typed as Collection<T>, where T defaults to Document which can be seen as a generic map of String to Object.
Providing the specific T type enables stronger typing for collection operations. (eg: Collecion<MyBean>)
For more information see the client reference.
Parameters
Use the getCollection method, which belongs to the com.datastax.astra.client.Database class.
Method signature
Collection<Document> getCollection(String collectionName)
Collection<Document> getCollection(
String collectionName,
CollectionOptions options
)
<T> Collection<T> getCollection(
String collectionName,
Class<T> documentClass
)
<T> Collection<T> getCollection(
String collectionName,
Class<T> documentClass
CollectionOptions options,
)
| Name | Type | Summary |
|---|---|---|
|
|
The name of the collection. |
|
Optional.
The options for this operation.
See Selected methods of |
| Method | Parameters | Summary |
|---|---|---|
|
|
Optional. The keyspace that contains the collection. For an example, see Get a collection and specify the keyspace. Default: The working keyspace for the database.
This is |
|
|
Optional. This only applies to collections with a vectorize embedding provider integration. Use this option to provide the embedding provider API key directly with headers instead of using an API key in the Astra DB KMS. The API key is sent to the Data API for every operation on the collection. It is useful when a vectorize integration is configured but no credentials are stored, or when you want to override the stored credentials. For more information, see Manage embedding provider integrations for vectorize. If you use an AWS embedding provider, the |
|
|
Optional.
A timeout, in milliseconds, for the underlying HTTP request.
If not provided, the |
Examples
The following examples demonstrate how to get a collection.
Get a collection
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.databases.Database;
public class Example {
public static void main(String[] args) {
// Get a database
Database database = new DataAPIClient("APPLICATION_TOKEN").getDatabase("API_ENDPOINT");
// Get a collection
Collection<Document> collection = database.getCollection("COLLECTION_NAME");
}
}
Get a collection and specify the keyspace
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.CollectionOptions;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.databases.Database;
public class Example {
public static void main(String[] args) {
// Get a database
Database database = new DataAPIClient("APPLICATION_TOKEN").getDatabase("API_ENDPOINT");
// Get a collection
CollectionOptions options = new CollectionOptions().keyspace("KEYSPACE_NAME");
Collection<Document> collection = database.getCollection("COLLECTION_NAME", options);
}
}
Client reference
For more information, see the client reference.