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. |
|
|
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 If you don’t set this, you must specify the keyspace in a downstream method before performing an operation that requires a keyspace. |
|
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.DataAPIClients;
import com.datastax.astra.client.databases.Database;
public class Example {
public static void main(String[] args) {
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
}
}
Client reference
For more information, see the client reference.