Get a database object (Java)
When using a client, most Data API interactions require you to connect to a database through a DataAPIClient object.
Connecting to a database is an essential component of most Data API client scripts. For more information, see Get started with the Data API (Java).
For more information about the Data API client hierarchy, see Python client internals, TypeScript client internals, Go client internals, Java client internals, and C# client internals.
Parameters
Use the getDatabase method, which belongs to the com.datastax.astra.client.DataAPIClient class.
Method signature
public Database getDatabase(String apiEndpoint, DatabaseOptions dbOptions)
public Database getDatabase(String apiEndpoint, String keyspace)
public Database getDatabase(String apiEndpoint)
public Database getDatabase(UUID databaseId)
public Database getDatabase(UUID databaseId, DatabaseOptions dbOptions)
public Database getDatabase(UUID databaseId, String region, DatabaseOptions dbOptions)
| Name | Type | Summary |
|---|---|---|
|
|
A database API endpoint URL, such as |
|
|
A database ID. You can optionally specify a region for multi-region databases. DataStax recommends using |
|
|
Optional.
Specify the working keyspace.
This keyspace is used for all downstream operations unless you later override it with a method such as Default: default_keyspace |
|
|
The region to use for connecting to the database. The database must be deployed in that region. If you don’t set this parameter and the region cannot be inferred from an API endpoint, an additional request is made to determine the default region and use it in subsequent operations. |
|
The options to use for the database. |
Examples
The following examples demonstrate how to connect to a database.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.options.DataAPIClientOptions;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.databases.DatabaseOptions;
public class Example {
public static void main(String[] args) {
DataAPIClient client = new DataAPIClient(new DataAPIClientOptions());
Database database =
client.getDatabase(
"API_ENDPOINT",
new DatabaseOptions("APPLICATION_TOKEN", new DataAPIClientOptions()));
}
}
Client reference
For more information, see the client reference.