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 |
|---|---|---|
|
|
The name of the new collection. Collection names must follow these rules:
|
|
Settings for the collection, including vector options, the default ID format, and indexing options.
See Methods of |
|
|
Optional.
The options for this operation.
See Selected methods of |
|
|
|
Work with specialized beans for the collection instead of the default |
| Method | Summary |
|---|---|
|
Optional. The vector configuration for the collection. This includes things like the vector dimension, similarity metric, and source model. Required for vector search. For an example, see Create a collection that can store vector embeddings. Specifies the following:
|
|
An alternative to |
|
An alternative to |
|
Optional. The selective indexing configuration for the collection. You must use For examples, see Create a collection and specify which fields to index and Create a collection and specify which fields shouldn’t be indexed. Default: All fields of all documents are indexed. |
|
Optional.
Specifies the default ID type for documents in the collection.
This is used when you insert a document without an Can be one of:
For examples, see Create a collection and specify the default ID format. For more information, see Document IDs (Java). Default: |
| Method | Parameters | Summary |
|---|---|---|
|
|
Optional if you specified a working keyspace when you created the Default: The working keyspace set when you created the |
|
|
Optional.
A timeout, in milliseconds, for the underlying HTTP request.
If not provided, the |
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.