Package com.datastax.astra.client
Class Database
java.lang.Object
com.datastax.astra.internal.command.AbstractCommandRunner
com.datastax.astra.client.Database
- All Implemented Interfaces:
CommandRunner
A Data API database. This is the entry-point object for doing database-level
DML, such as creating/deleting collections, and for obtaining Collection
objects themselves. This class has a synchronous interface.
A Database comes with an "API Endpoint", which implies a Database object instance reaches a specific region (relevant point in case of multi-region databases).
-
Field Summary
Fields inherited from class com.datastax.astra.internal.command.AbstractCommandRunner
commandOptions, httpClient
-
Constructor Summary
ConstructorDescriptionInitialization with endpoint and apikey.Initialization with endpoint and apikey.Database
(String apiEndpoint, String token, String namespace, DataAPIOptions options) Initialization with endpoint and apikey. -
Method Summary
Modifier and TypeMethodDescriptionboolean
collectionExists
(String collection) Evaluate if a collection exists.createCollection
(String collectionName) Create a new collection with the given name.createCollection
(String collectionName, int dimension, SimilarityMetric metric) Create a default new collection for vector.<T> Collection
<T> createCollection
(String collectionName, int dimension, SimilarityMetric metric, Class<T> documentClass) Create a default new collection for vector.createCollection
(String collectionName, CollectionOptions collectionOptions) Create a new collection with the given name.createCollection
(String collectionName, CollectionOptions collectionOptions, CommandOptions<?> commandOptions) Create a new collection with the given name.<T> Collection
<T> createCollection
(String collectionName, CollectionOptions collectionOptions, CommandOptions<?> commandOptions, Class<T> documentClass) Create a new collection with the selected options<T> Collection
<T> createCollection
(String collectionName, CollectionOptions collectionOptions, Class<T> documentClass) Create a new collection with the given name.<T> Collection
<T> createCollection
(String collectionName, Class<T> documentClass) Create a new collection with the given name.void
deleteListener
(String name) Register a listener to execute commands on the collection.void
drop()
Drops this namespacevoid
dropCollection
(String collectionName) Delete a collection.protected String
The subclass should provide the endpoint, url to post request.getCollection
(String collectionName) Gets a collection.<T> Collection
<T> getCollection
(String collectionName, CommandOptions<?> commandOptions, @NonNull Class<T> documentClass) Gets a collection, with a specific default document class.<T> Collection
<T> getCollection
(String collectionName, @NonNull Class<T> documentClass) Gets a collection, with a specific default document class.Access a database Admin client from the databasegetDatabaseAdmin
(String superUserToken) Gets the name of the database.Gets the names of all the collections in this database.Finds all the collections in this database.void
registerListener
(String logger, CommandObserver commandObserver) Register a listener to execute commands on the collection.useNamespace
(String namespace) This mutates the namespace to be used.Methods inherited from class com.datastax.astra.internal.command.AbstractCommandRunner
mapAsDocument, runCommand, runCommand
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.datastax.astra.client.model.CommandRunner
runCommand, runCommand
-
Constructor Details
-
Database
Initialization with endpoint and apikey.- Parameters:
apiEndpoint
- api endpointtoken
- api token
-
Database
Initialization with endpoint and apikey.- Parameters:
apiEndpoint
- api endpointtoken
- api tokennamespace
- namespace
-
Database
Initialization with endpoint and apikey.- Parameters:
apiEndpoint
- api endpointtoken
- api tokennamespace
- namespaceoptions
- setup of the clients with options
-
-
Method Details
-
useNamespace
This mutates the namespace to be used.- Parameters:
namespace
- current namespace- Returns:
- the database
-
getDatabaseAdmin
Access a database Admin client from the database- Returns:
- database admin
-
getDatabaseAdmin
Gets the name of the database.- Parameters:
superUserToken
- provide a token with a super-user role- Returns:
- the database name
-
listCollectionNames
Gets the names of all the collections in this database.- Returns:
- a stream containing all the names of all the collections in this database
-
listCollections
Finds all the collections in this database.- Returns:
- list of collection definitions
-
collectionExists
Evaluate if a collection exists.- Parameters:
collection
- namespace name.- Returns:
- if namespace exists
-
getCollection
Gets a collection.- Parameters:
collectionName
- the name of the collection to return- Returns:
- the collection
- Throws:
IllegalArgumentException
- if collectionName is invalid
-
getCollection
public <T> Collection<T> getCollection(String collectionName, @NonNull @NonNull Class<T> documentClass) Gets a collection, with a specific default document class.- Type Parameters:
T
- the type of the class to use instead ofDocument
.- Parameters:
collectionName
- the name of the collection to returndocumentClass
- the default class to cast any documents returned from the database into.- Returns:
- the collection
-
getCollection
public <T> Collection<T> getCollection(String collectionName, CommandOptions<?> commandOptions, @NonNull @NonNull Class<T> documentClass) Gets a collection, with a specific default document class.- Type Parameters:
T
- the type of the class to use instead ofDocument
.- Parameters:
collectionName
- the name of the collection to returncommandOptions
- options to use when using this collectiondocumentClass
- the default class to cast any documents returned from the database into.- Returns:
- the collection
-
drop
public void drop()Drops this namespace -
createCollection
Create a new collection with the given name.- Parameters:
collectionName
- the name for the new collection to create- Returns:
- the instance of collection
-
createCollection
public Collection<Document> createCollection(String collectionName, int dimension, SimilarityMetric metric) Create a default new collection for vector.- Parameters:
collectionName
- collection namedimension
- vector dimensionmetric
- vector metric- Returns:
- the instance of collection
-
createCollection
public <T> Collection<T> createCollection(String collectionName, int dimension, SimilarityMetric metric, Class<T> documentClass) Create a default new collection for vector.- Type Parameters:
T
- working class for the document- Parameters:
collectionName
- collection namedimension
- vector dimensionmetric
- vector metricdocumentClass
- class of document to return- Returns:
- the instance of collection
-
createCollection
Create a new collection with the given name.- Type Parameters:
T
- working class for the document- Parameters:
collectionName
- the name for the new collection to createdocumentClass
- class of document to return- Returns:
- the collection
-
createCollection
public Collection<Document> createCollection(String collectionName, CollectionOptions collectionOptions) Create a new collection with the given name.- Parameters:
collectionName
- the name for the new collection to createcollectionOptions
- various options for creating the collection- Returns:
- the collection
-
createCollection
public <T> Collection<T> createCollection(String collectionName, CollectionOptions collectionOptions, Class<T> documentClass) Create a new collection with the given name.- Type Parameters:
T
- working object for the document- Parameters:
collectionName
- collection namecollectionOptions
- collection optionsdocumentClass
- document class- Returns:
- the collection created
-
createCollection
public Collection<Document> createCollection(String collectionName, CollectionOptions collectionOptions, CommandOptions<?> commandOptions) Create a new collection with the given name.- Parameters:
collectionName
- the name for the new collection to createcollectionOptions
- various options for creating the collectioncommandOptions
- options to use when using this collection- Returns:
- the collection
-
createCollection
public <T> Collection<T> createCollection(String collectionName, CollectionOptions collectionOptions, CommandOptions<?> commandOptions, Class<T> documentClass) Create a new collection with the selected options- Type Parameters:
T
- working class for the document- Parameters:
collectionName
- the name for the new collection to createcollectionOptions
- various options for creating the collectioncommandOptions
- options to use when using this collectiondocumentClass
- the default class to cast any documents returned from the database into.- Returns:
- the collection
-
dropCollection
Delete a collection.- Parameters:
collectionName
- collection name
-
getApiEndpoint
The subclass should provide the endpoint, url to post request.- Specified by:
getApiEndpoint
in classAbstractCommandRunner
- Returns:
- url on which to post the request
-
registerListener
Register a listener to execute commands on the collection. Please now useCommandOptions
.- Parameters:
logger
- name for the loggercommandObserver
- class for the logger
-
deleteListener
Register a listener to execute commands on the collection. Please now useCommandOptions
.- Parameters:
name
- name for the observer
-