Create a collection (Java)

Creates a new collection in a database.

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

Creates a collection with the specified parameters.

Returns a Collection object. You can use this object to work with documents in the collection.

Parameters

You cannot edit a collection’s definition after you create the collection.

Use the createCollection method, which belongs to the com.datastax.astra.client.Database class.

Method signature
Collection<Document> createCollection(String collectionName)
Collection<Document> createCollection(
  String collectionName,
  CollectionDefinition collectionDefinition
)
Collection<Document> createCollection(
  String collectionName,
  CollectionDefinition collectionDefinition,
  CreateCollectionOptions options
)
<T> Collection<T> createCollection(
  String collectionName,
  Class<T> documentClass
)
<T>  Collection<T> createCollection(
  String collectionName,
  CollectionDefinition collectionDefinition,
  Class<T> documentClass
)
<T> Collection<T> createCollection(
  String collectionName,
  CollectionDefinition collectionDefinition,
  Class<T> documentClass,
  CreateCollectionOptions options
)
Name Type Summary

collectionName

String

The name of the new collection.

Collection names must follow these rules:

  • Can contain letters, numbers, and underscores

  • Cannot exceed 48 characters

  • Must be unique within the keyspace

collectionDefinition

CollectionDefinition

Settings for the collection, including vector options, the default ID format, and indexing options. See Methods of CollectionDefinition for more details.

options

CreateCollectionOptions

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

documentClass

Class<T>

Work with specialized beans for the collection instead of the default Document type.

Methods of CollectionDefinition
Method Summary

vector()

Optional. The vector configuration for the collection. This includes things like the vector dimension, similarity metric, and source model.

Required for vector search.

Specifies the following:

  • dimension: The dimension for vector embeddings in the collection. This should match the dimension of the vector that your embedding model produces.

  • metric: Optional. The similarity metric to use for vector search. Can be one of the values in SimilarityMetric: COSINE, DOT_PRODUCT, EUCLIDEAN.

vectorDimension()

An alternative to vector().

vectorSimilarity()

An alternative to vector().

indexingAllow() or indexingDeny()

Optional. The selective indexing configuration for the collection.

You must use & to escape any . or & in field names in the indexing clause. You cannot use & to escape any other characters. Dot notation, which is used to reference nested fields, should not be escaped. For more information, see Work with . and & in field names (Java).

Default: All fields of all documents are indexed.

defaultId()

Optional. Specifies the default ID type for documents in the collection. This is used when you insert a document without an _id field.

Can be one of:

  • CollectionDefaultIdTypes.OBJECTID: Each autogenerated _id value is an objectId as provided by the bson library.

  • CollectionDefaultIdTypes.UUIDV7: Each autogenerated _id value is a version 7 UUID. This is designed as a replacement for version 1 time UUID, and it is recommended for use in new systems.

  • CollectionDefaultIdTypes.UUIDV6: Each autogenerated _id value is a version 6 UUID. This is field-compatible with version 1 time UUIDs, and it supports lexicographical sorting.

  • CollectionDefaultIdTypes.UUID: Each autogenerated _id value is a version 4 UUID. This type is analogous to the uuid type and functions in Apache Cassandra®.

For more information, see Document IDs (Java).

Default: CollectionDefaultIdTypes.UUID

Selected methods of CreateCollectionOptions
Method Parameters Summary

keyspace()

String

Optional if you specified a working keyspace when you created the Database object. The keyspace in which to create 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 create a collection.

Create a collection that is not vector-enabled

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");

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

Create a collection that can store vector embeddings

Collections that are vector-enabled can store vector embeddings in the reserved $vector field and work with vector search.

For optimal vector search results, you should specify the dimension, metric, and source model of your vector embeddings. All vector embeddings in a collection should be generated by the same model with the same dimensions. The source model can be one of: ada002, bert, cohere-v3, gecko, nv-qa-4, openai-v3-large, openai-v3-small, other.

import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.CollectionDefinition;
import com.datastax.astra.client.collections.definition.documents.Document;
import com.datastax.astra.client.core.vector.SimilarityMetric;
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");

    // Create a collection
    CollectionDefinition collectionDefinition =
        new CollectionDefinition().vectorDimension(1024).vectorSimilarity(SimilarityMetric.COSINE);

    Collection<Document> collection =
        database.createCollection("COLLECTION_NAME", collectionDefinition);
  }
}

Create a collection and specify the default ID format

For more information about the default ID format, see Document IDs (Java). For allowed values, see the Parameters.

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.CollectionDefaultIdTypes;
import com.datastax.astra.client.collections.definition.CollectionDefinition;
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
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
    Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");

    // Create a collection
    CollectionDefinition collectionDefinition =
        new CollectionDefinition().defaultId(CollectionDefaultIdTypes.OBJECT_ID);

    Collection<Document> collection =
        database.createCollection("COLLECTION_NAME", collectionDefinition);
  }
}

Create a collection and specify which fields to index

For more information about selective indexing, see Indexes in collections (Java).

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.CollectionDefinition;
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
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
    Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");

    // Create a collection
    CollectionDefinition collectionDefinition =
        new CollectionDefinition().indexingAllow("city", "country");

    Collection<Document> collection =
        database.createCollection("COLLECTION_NAME", collectionDefinition);
  }
}

Create a collection and specify which fields shouldn’t be indexed

For more information about selective indexing, see Indexes in collections (Java).

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.Collection;
import com.datastax.astra.client.collections.definition.CollectionDefinition;
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
    DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
    Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");

    // Create a collection
    CollectionDefinition collectionDefinition =
        new CollectionDefinition().indexingDeny("city", "country");

    Collection<Document> collection =
        database.createCollection("COLLECTION_NAME", collectionDefinition);
  }
}

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