Get a table (Java)
|
Tables with the Data API are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
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 if you specified a working keyspace when you created the Default: The working keyspace set when you created the |
|
|
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.
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.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
}
}
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.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME", Book.class);
}
}
Client reference
For more information, see the client reference.