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 if you specified a working keyspace when you created the Database object. The keyspace that contains the collection.

Default: The working keyspace set when you created the Database object, if one was provided.

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.DataAPIClients;
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 =
        DataAPIClients.clientHCD("USERNAME", "PASSWORD")
            .getDatabase("API_ENDPOINT", "KEYSPACE_NAME");

    // Get a collection
    Collection<Document> collection = database.getCollection("COLLECTION_NAME");
  }
}

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