Get a table (Java)
Gets a reference to a table for use with the Data API clients.
|
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
Returns a Table<T> object that corresponds to the specified table name.
This method returns a Table object even for tables that don’t exist.
Unless you specify the rowClass parameter, the table is typed as Table<Row>.
Parameters
Use the getTable method, which belongs to the com.datastax.astra.client.databases.Database class.
Method signature
Table<Row> getTable(String tableName)
Table<Row> getTable(
String tableName,
TableOptions tableOptions
)
<T> Table<T> getTable(Class<T> rowClass)
<T> Table<T> getTable(
String tableName,
Class<T> rowClass
)
<T> Table<T> getTable(
String tableName,
Class<T> rowClass,
TableOptions tableOptions
)
| Name | Type | Summary |
|---|---|---|
|
|
The name of the table. |
|
|
Optional. A specification of the class of the table’s row object. For an example, see Get a table and specify typing. Default: |
|
Optional.
The options for this operation. See Selected methods of |
| Method | Parameters | Summary |
|---|---|---|
|
|
Optional. The keyspace containing the table. For an example, see Get a table and specify the keyspace. Default: The working keyspace for the database.
This is |
|
|
Optional.
A timeout, in milliseconds, for the underlying HTTP requests originating from this |
|
|
Optional. Specialized headers for this table, if needed, that are added to the database headers. |
|
|
Optional. This only applies to tables that have a vector column with a vectorize embedding provider integration. Use this option to provide the embedding provider API key directly with headers instead of using an API key in the Astra DB KMS. The API key is sent to the Data API for every operation on the table. It is useful when a vectorize integration is configured but no credentials are stored, or when you want to override the stored credentials. For more information, see Manage embedding provider integrations for vectorize. You can use this authentication method only if all affected columns use the same embedding provider. If you use an AWS embedding provider, the |
|
Optional.
HTTP configuration overrides for this |
|
|
Optional.
A specialized serializer for this |
Examples
The following examples demonstrate how to get a table.
Get a table
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
}
}
Get a table and specify the keyspace
By default, this method uses the working keyspace of your database. If you want to use a different keyspace, you must specify the keyspace.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.TableOptions;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
TableOptions options = new TableOptions().keyspace("KEYSPACE_NAME");
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME", options);
}
}
Get a table and specify typing
Use the Class parameter to specify typing for the row.
If you don’t specify the Class parameter, the client defaults Table as the type.
In this case, the working object type T is Row.class.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.columns.TableColumnTypes;
import com.datastax.astra.client.tables.mapping.Column;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import lombok.Data;
import lombok.NoArgsConstructor;
public class Example {
@Data
@NoArgsConstructor
public class Book {
@Column(name = "title", type = TableColumnTypes.TEXT)
private String title;
@Column(name = "author", type = TableColumnTypes.TEXT)
private String author;
@Column(name = "number_of_pages", type = TableColumnTypes.INT)
private Integer number_of_pages;
@Column(name = "rating", type = TableColumnTypes.FLOAT)
private Float rating;
@Column(name = "genres", type = TableColumnTypes.SET, valueType = TableColumnTypes.TEXT)
private Set<String> genres;
@Column(
name = "metadata",
type = TableColumnTypes.MAP,
keyType = TableColumnTypes.TEXT,
valueType = TableColumnTypes.TEXT)
private Map<String, String> metadata;
@Column(name = "is_checked_out", type = TableColumnTypes.BOOLEAN)
private Boolean is_checked_out;
@Column(name = "due_date", type = TableColumnTypes.DATE)
private Date due_date;
}
public static void main(String[] args) {
// Get an existing table
Table<Book> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME", Book.class);
}
}
Client reference
For more information, see the client reference.