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 lexical search configuration for the collection. Required to support lexicographical matching. Only collections in databases in the AWS Specifies the following:
For examples, see Create a collection that supports lexicographical matching. Default: Lexical search is enabled and uses the standard Apache Lucene™ analyzer. |
|
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 that supports lexicographical matching
If you want to use lexicographical matching to find documents in your collection, you must create a collection that has lexical enabled.
Your collection must also be in a database in the AWS us-east-2 region.
Lexical is enabled by default when you create a collection in a database in the AWS us-east-2 region, but you can optionally configure the lexical analyzer.
For configuration details about the lexical analyzer, see Configure and use SAI text analyzers with CQL. The following example uses a configuration suitable for English text.
The Java client supports multiple ways to create a collection:
-
You can define the collection parameters in a
CollectionDefinitionobject and then create the collection from theCollectionDefinitionobject. -
You can use a fluent interface to build the collection definition and then create the collection from the definition.
-
CollectionDefinition object
-
Fluent interface
import static com.datastax.astra.client.core.lexical.AnalyzerTypes.STANDARD;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.definition.CollectionDefinition;
import com.datastax.astra.client.core.lexical.Analyzer;
import com.datastax.astra.client.core.lexical.LexicalOptions;
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();
// Lexical Options
Analyzer analyzer =
new Analyzer()
.tokenizer(STANDARD.getValue())
.addFilter("lowercase")
.addFilter("stop")
.addFilter("porterstem")
.addFilter("asciifolding");
LexicalOptions lexicalOptions = new LexicalOptions().enabled(true).analyzer(analyzer);
collectionDefinition.lexical(lexicalOptions);
database.createCollection("COLLECTION_NAME", collectionDefinition);
}
}
import static com.datastax.astra.client.core.lexical.AnalyzerTypes.STANDARD;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.collections.definition.CollectionDefinition;
import com.datastax.astra.client.core.lexical.Analyzer;
import com.datastax.astra.client.core.lexical.LexicalOptions;
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");
database.createCollection(
"COLLECTION_NAME",
new CollectionDefinition()
.lexical(
new LexicalOptions()
.enabled(true)
.analyzer(
new Analyzer()
.tokenizer(STANDARD.getValue())
.addFilter("lowercase")
.addFilter("stop")
.addFilter("porterstem")
.addFilter("asciifolding"))));
}
}
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.