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

name

string

The name of the collection.

options

CollectionOptions

Optional. The options for this operation. See Selected methods of CollectionOptions for more details.

Selected methods of CollectionOptions
Method Parameters Summary

keyspace()

String

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 default_keyspace unless you set a different working keyspace when you created the Database object.

embeddingAuthProvider()

EmbeddingHeadersProvider

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 embeddingAuthProvider() method must instead use AWSEmbeddingHeadersProvider to pass your access ID and secret ID.

timeout()

long | Duration

Optional. A timeout, in milliseconds, for the underlying HTTP request. If not provided, the Database setting is used.

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.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM