Get a database object
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.
For more information about the Data API client hierarchy, see Python client usage, TypeScript client usage, and Java client usage.
Parameters
-
Python
-
TypeScript
-
Java
Use the get_database
method, which belongs to the astrapy.DataAPIClient
class.
Method signature
get_database(
api_endpoint: str,
*,
token: str | TokenProvider,
keyspace: str,
spawn_api_options: APIOptions,
) -> Database
Name | Type | Summary |
---|---|---|
|
|
A database API endpoint URL, such as |
|
|
Optional.
An application token for a database, in the form of an The token should have sufficient permissions to perform the desired operations. If you specified a token when instantiating the |
|
|
Optional.
Specify the working keyspace.
You can override this in downstream methods such as Default: |
|
|
Optional. A complete or partial specification of the APIOptions. This overrides the defaults inherited from the Data API client. This allows you to customize the interaction with the Data API. For example, you can use this parameter to change the timeout settings. |
Use the db
method, which belongs to the DataAPIClient
class.
Method signature
db(
endpoint: string,
options?: {
logging?: LoggingConfig,
keyspace?: string | null,
token?: string | TokenProvider | null,
dataApiPath?: string,
serdes?: DbSerDesConfig,
timeoutDefaults?: TimeoutDescriptor,
}
): Db
Name | Type | Summary |
---|---|---|
|
|
A database API endpoint URL, such as |
|
Optional.
The options to use for the database.
See the |
Name | Type | Summary |
---|---|---|
|
Optional.
Specify the working keyspace.
You can override this in downstream methods such as Default: |
|
|
Optional.
Whether to monitor commands through Default: false. |
|
|
Optional.
An application token for a database.
Tokens are prefixed with The token should have sufficient permissions to perform the desired operations. If you specified a token when instantiating the |
|
|
Optional. The path to the Data API. Default: |
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 |
|
|
Specify the working keyspace.
You can override this in downstream methods such as Default: |
|
|
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 can’t be inferred from an API endpoint, an additional DevOps API 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.
-
Python
-
TypeScript
-
Java
from astrapy import DataAPIClient
client = DataAPIClient()
database = client.get_database("ASTRA_DB_API_ENDPOINT", token="ASTRA_DB_APPLICATION_TOKEN")
import { DataAPIClient } from '@datastax/astra-db-ts';
const client = new DataAPIClient();
const database = client.db('ASTRA_DB_API_ENDPOINT', {
token: 'ASTRA_DB_APPLICATION_TOKEN',
});
package com.examples;
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 DeleteOne {
public static void main(String[] args) {
DataAPIClient client = new DataAPIClient(new DataAPIClientOptions());
Database database =
client.getDatabase(
"ASTRA_DB_API_ENDPOINT", new DatabaseOptions("ASTRA_DB_APPLICATION_TOKEN", new DataAPIClientOptions()));
}
}
Client reference
-
Python
-
TypeScript
-
Java
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.