Class Table<T>
- Type Parameters:
T
- the type of the table entity, representing the data model or schema
- All Implemented Interfaces:
CommandRunner
The Table
class is designed to work with table entities of type T
, where
T
represents the data model or schema associated with the table.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final DataAPISerializer
Avoid duplicating for each operation if not override.Working class representing rows of the table.Fields inherited from class com.datastax.astra.internal.command.AbstractCommandRunner
apiEndpoint, ARG_CLAZZ, ARG_DATABASE, ARG_OPTIONS, ARG_UPDATE, httpClient, INPUT_INCLUDE_SIMILARITY, INPUT_INCLUDE_SORT_VECTOR, INPUT_ORDERED, INPUT_PAGE_STATE, INPUT_RETURN_DOCUMENT, INPUT_RETURN_DOCUMENT_RESPONSES, INPUT_UPSERT, options, RESULT_COUNT, RESULT_DELETED_COUNT, RESULT_INSERTED_IDS, RESULT_MATCHED_COUNT, RESULT_MODIFIED_COUNT, RESULT_MORE_DATA, RESULT_UPSERTED_ID
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionalter
(AlterTableOperation operation) Performs an alteration operation on the table with default options.alter
(AlterTableOperation operation, AlterTableOptions options) Performs an alteration operation on the table with the specified options.final <R> Table
<R> alter
(AlterTableOperation operation, AlterTableOptions options, Class<R> clazz) Performs an alteration operation on the table with the specified options and row class.int
countRows
(int upperBound) Counts the number of rows in the table.int
Implementation of the @seecountRows(Filter, int, CountRowsOptions)
method with default options.int
countRows
(Filter filter, int upperBound, CountRowsOptions options) Counts the number of rows in the table with a filter.void
createIndex
(String idxName, TableIndexDefinition idxDefinition, CreateIndexOptions idxOptions) Create a new index with the given description.void
createIndex
(String idxName, String columnName) Create a simple index on the given column with no special optionsvoid
createIndex
(String idxName, String columnName, CreateIndexOptions idxOptions) Create a simple index on the given column with no special optionsvoid
createVectorIndex
(String idxName, TableVectorIndexDefinition idxDefinition) Create a new index with the given description.void
createVectorIndex
(String idxName, TableVectorIndexDefinition idxDefinition, CreateVectorIndexOptions idxOptions) Create a new index with the given description.void
createVectorIndex
(String idxName, String columnName) Create a new index with the given description.void
Removes all rows from the table that match the given query filter.void
deleteMany
(Filter filter) Removes all rows from the table that match the given query filter.void
deleteMany
(Filter filter, TableDeleteManyOptions options) Removes all rows from the tables that match the given query filter.void
Removes at most one rows from the table that matches the given filter.void
deleteOne
(Filter filter, TableDeleteOneOptions deleteOneOptions) Removes at most one row from the table that matches the given filter.<R> List
<R> Return a list of distinct values for the given field name.<R> List
<R> distinct
(String fieldName, Filter filter, Class<R> resultClass, TableDistinctOptions options) Return a list of distinct values for the given field name.void
drop()
Delete the current tablelong
Calling an estimatedRowCount with default options.long
Executes the "estimatedRowCount" command to estimate the number of rowse in a table.Finds all rows in the table.find
(Filter filter, TableFindOptions options) Finds all rows in the table.<R> TableCursor
<T, R> find
(Filter filter, TableFindOptions options, Class<R> newRowType) Finds all rows in the table.find
(TableFindOptions options) Finds all rows in the table.findAll()
Retrieves all rows in the table.Retrieves a single row from the table that matches the specified filter criteria.findOne
(Filter filter, TableFindOneOptions findOneOptions) Retrieves a single row from the table that matches the specified filter criteria.<R> Optional
<R> findOne
(Filter filter, TableFindOneOptions findOneOptions, Class<R> newRowClass) Retrieves a single row from the table that matches the specified filter criteria.<R> Optional
<R> Retrieves a single row from the table that matches the specified filter criteria.findOne
(TableFindOneOptions findOneOptions) Retrieves a single row from the table that matches the specified filter criteria.findPage
(Filter filter, TableFindOptions options) Executes a paginated 'find' query on the table using the specified filter and find options.Retrieves the full definition of the table, encompassing both its name and its configuration options.Retrieves the name of the parent keyspace associated with this table.getName()
Retrieves the name of the table.insertMany
(List<? extends T> rows) Inserts multiple rows into the table.insertMany
(List<? extends T> rows, TableInsertManyOptions insertManyOptions) Inserts multiple rows into the table with the specified options.final TableInsertManyResult
insertMany
(T... rows) Inserts multiple rows into the table.final TableInsertOneResult
Inserts a single row into the table.final TableInsertOneResult
insertOne
(T row, TableInsertOneOptions insertOneOptions) Inserts a single row into the table with the specified options.Finds all the indices in the selected keyspace.listIndexes
(ListIndexesOptions listIndexesOptions) Finds all the indices in the selected keyspace.Retrieves the names of all indices in the keyspace with default options.listIndexesNames
(ListIndexesOptions listIndexesOptions) Retrieves the names of all indices in the keyspace with default options.void
registerListener
(String logger, CommandObserver commandObserver) Register a listener to execute commands on the table.void
updateOne
(Filter filter, TableUpdateOperation update) Update a single row in the table according to the specified arguments.void
updateOne
(Filter filter, TableUpdateOperation update, TableUpdateOneOptions updateOptions) Update a single document in the collection according to the specified arguments.Methods inherited from class com.datastax.astra.internal.command.AbstractCommandRunner
assertIsAstra, getApiEndpoint, getOptions, getSerializer, runCommand, runCommand, unmarshall
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.datastax.astra.client.core.commands.CommandRunner
runCommand, runCommand
-
Field Details
-
DEFAULT_TABLE_SERIALIZER
Avoid duplicating for each operation if not override. -
rowClass
Working class representing rows of the table. The default value isRow
.
-
-
Constructor Details
-
Table
Constructs an instance of a table within the specified database. This constructor initializes the table with a given name and associates it with a specific class type that represents the schema of rows within the table. This setup is designed for CRUD (Create, Read, Update, Delete) operations.- Parameters:
db
- TheDatabase
instance representing the client's keyspace for HTTP communication with the database. It encapsulates the configuration and management of the database connection, ensuring that operations on this table are executed within the context of this database.tableName
- AString
that uniquely identifies the table within the database. This name is used to route operations to the correct table and should adhere to the database's naming conventions.tableOptions
- the options to apply to the command operation. If left blank the default tableExample usage:
// Given a client DataAPIClient client = new DataAPIClient("token"); // Given a database Database myDb = client.getDatabase("myDb"); // Initialize a table with a working class Table<MyRowClass> myTable = new Table<>(myDb, "myTableName", MyRowClass.class);
rowClass
- TheClass<DOC>
object that represents the model for rows within this table. This class is used for serialization and deserialization of rows to and from the database. It ensures type safety and facilitates the mapping of database rows to Java objects.
-
-
Method Details
-
getKeyspaceName
Retrieves the name of the parent keyspace associated with this table. A keyspace in this context typically refers to a higher-level categorization or grouping mechanism within the database that encompasses one or more tables. This method allows for identifying the broader context in which this table exists, which can be useful for operations requiring knowledge of the database structure or for dynamic database interaction patterns.- Returns:
- A
String
representing the name of the parent keyspace of the current table. This name serves as an identifier for the keyspace and can be used to navigate or query the database structure.Example usage:
Table myTable = ... // assume myTable is already initialized String keyspaceName = myTable.getKeyspaceName(); System.out.println("The table belongs to the keyspace: " + keyspaceName);
-
getDefinition
Retrieves the full definition of the table, encompassing both its name and its configuration options. This comprehensive information is encapsulated in aTableDefinition
object, providing access to the table's metadata and settings.The returned
TableDefinition
includes details such as the table's name, which serves as its unique identifier within the database, and a set of options that describe its configuration. These options may cover aspects like indexing preferences, read/write permissions, and other customizable settings that were specified at the time of table creation.Example usage:
// Given a table Table<Row> table; // Access its Definition TableDefinition definition = table.getDefinition(); System.out.println("Name=" + definition.getName()); if (options != null) { // Operations based on table options }
- Returns:
- A
TableDefinition
object containing the full definition of the table, including its name and configuration options. This object provides a comprehensive view of the table's settings and identity within the database.
-
getName
Retrieves the name of the table. This name serves as a unique identifier within the database and is used to reference the table in database operations such as queries, updates, and deletions. The table name is defined at the time of table creation and is immutable.- Returns:
- A
String
representing the name of the table. This is the same name that was specified when the table was created or initialized.
-
alter
Performs an alteration operation on the table with default options.This method delegates to
alter(AlterTableOperation, AlterTableOptions)
withoptions
set tonull
.- Parameters:
operation
- the alteration operation to be performed; must not benull
.- Returns:
- a new
Table
instance representing the altered table.
-
alter
Performs an alteration operation on the table with the specified options.This method delegates to
alter(AlterTableOperation, AlterTableOptions, Class)
using the row class of the current table.- Parameters:
operation
- the alteration operation to be performed; must not benull
.options
- the options for the alteration operation; may benull
.- Returns:
- a new
Table
instance representing the altered table.
-
alter
public final <R> Table<R> alter(AlterTableOperation operation, AlterTableOptions options, Class<R> clazz) Performs an alteration operation on the table with the specified options and row class.This is the most granular method for altering a table. It builds and executes the command to perform the specified alteration operation, with optional parameters and custom row class.
- Type Parameters:
R
- the type of the rows in the altered table.- Parameters:
operation
- the alteration operation to be performed; must not benull
.options
- the options for the alteration operation; may benull
.clazz
- the class representing the row type for the altered table; must not benull
.- Returns:
- a new
Table
instance of the specified row class representing the altered table. - Throws:
NullPointerException
- ifoperation
orclazz
isnull
.
-
createIndex
Create a simple index on the given column with no special options- Parameters:
idxName
- name of the indexcolumnName
- column on which is the index
-
createIndex
Create a simple index on the given column with no special options- Parameters:
idxName
- name of the indexcolumnName
- column on which is the indexidxOptions
- index options
-
createIndex
public void createIndex(String idxName, TableIndexDefinition idxDefinition, CreateIndexOptions idxOptions) Create a new index with the given description.- Parameters:
idxName
- name of the indexidxDefinition
- definition of the indexidxOptions
- index options
-
createVectorIndex
Create a new index with the given description.- Parameters:
idxName
- name of the indexcolumnName
- name of the column
-
createVectorIndex
Create a new index with the given description.- Parameters:
idxName
- name of the indexidxDefinition
- definition of the index
-
createVectorIndex
public void createVectorIndex(String idxName, TableVectorIndexDefinition idxDefinition, CreateVectorIndexOptions idxOptions) Create a new index with the given description.- Parameters:
idxName
- index nameidxDefinition
- definition of the indexidxOptions
- index options
-
insertOne
Inserts a single row into the table.- Parameters:
row
- the row to be inserted; must not benull
.- Returns:
- a
TableInsertOneResult
object representing the result of the insertion operation.
-
insertOne
Inserts a single row into the table with the specified options.- Parameters:
row
- the row to be inserted; must not benull
.insertOneOptions
- the options for the insertion operation; may benull
.- Returns:
- a
TableInsertOneResult
object representing the result of the insertion operation.
-
insertMany
Inserts multiple rows into the table.- Parameters:
rows
- the list of rows to be inserted; must not benull
or empty.- Returns:
- a
TableInsertManyResult
object representing the result of the insertion operation.
-
insertMany
public TableInsertManyResult insertMany(List<? extends T> rows, TableInsertManyOptions insertManyOptions) Inserts multiple rows into the table with the specified options.- Parameters:
rows
- the list of rows to be inserted; must not benull
or empty.insertManyOptions
- the options for the insertion operation; must not benull
.- Returns:
- a
TableInsertManyResult
object representing the result of the insertion operation.
-
insertMany
Inserts multiple rows into the table.- Parameters:
rows
- the list of rows to be inserted; must not benull
or empty.- Returns:
- a
TableInsertManyResult
object representing the result of the insertion operation.
-
findOne
public <R> Optional<R> findOne(Filter filter, TableFindOneOptions findOneOptions, Class<R> newRowClass) Retrieves a single row from the table that matches the specified filter criteria.- Type Parameters:
R
- the type of the row in the result.- Parameters:
filter
- the filter criteria used to select the row; may benull
.findOneOptions
- options for the find one operationnewRowClass
- the class representing the row type for the result; must not benull
.- Returns:
- an
Optional
containing the row that matches the filter, or an emptyOptional
if no match is found.
-
findOne
Retrieves a single row from the table that matches the specified filter criteria.- Type Parameters:
R
- the type of the row in the result.- Parameters:
filter
- the filter criteria used to select the row; may benull
.newRowClass
- the class representing the row type for the result; must not benull
.- Returns:
- an
Optional
containing the row that matches the filter, or an emptyOptional
if no match is found.
-
findOne
Retrieves a single row from the table that matches the specified filter criteria. -
findOne
Retrieves a single row from the table that matches the specified filter criteria. -
findOne
Retrieves a single row from the table that matches the specified filter criteria. -
find
Finds all rows in the table.- Parameters:
filter
- the query filteroptions
- options of find one- Returns:
- the Cursor to iterate over the results
-
find
Finds all rows in the table.- Type Parameters:
R
- the type of the row in the result.- Parameters:
filter
- the query filteroptions
- options of find onenewRowType
- the class representing the row type for the result; must not benull
.- Returns:
- the Cursor to iterate over the results
-
find
Finds all rows in the table.- Parameters:
filter
- the query filter- Returns:
- the Cursor to iterate over the results
-
find
Finds all rows in the table.- Parameters:
options
- options of find one- Returns:
- the Cursor to iterate over the results
-
findAll
Retrieves all rows in the table.This method returns an iterable interface that allows iterating over all rows in the table, without applying any filters. It leverages the default
TableFindOptions
for query execution.- Returns:
- A
TableCursor
for iterating over all rows in the table.
-
findPage
Executes a paginated 'find' query on the table using the specified filter and find options.This method constructs and executes a command to fetch a specific page of rows from the table that match the provided filter criteria. It allows for detailed control over the query through
FindOptions
, such as sorting, projection, pagination, and more. The result is wrapped in aPage
object, which includes the rows found, the page size, and the state for fetching subsequent pages.Pagination is facilitated by the
skip
,limit
, andpageState
parameters withinFindOptions
, enabling efficient data retrieval in scenarios where the total dataset is too large to be fetched in a single request. Optionally, similarity scoring can be included ifincludeSimilarity
is set, which is useful for vector-based search queries.The method processes the command's response, mapping each rows to the specified row class and collecting them into a list. This list, along with the maximum page size and the next page state, is used to construct the
Page
object returned by the method.- Parameters:
filter
- The filter criteria used to select rows from the table.options
- TheCollectionFindOptions
providing additional query parameters, such as sorting and pagination.- Returns:
- A
Page
object containing the rows that match the query, along with pagination information.
-
distinct
Return a list of distinct values for the given field name.- Type Parameters:
R
- type of the result- Parameters:
fieldName
- name of the fieldfilter
- filter to applyresultClass
- class of the result- Returns:
- list of distinct values
-
distinct
public <R> List<R> distinct(String fieldName, Filter filter, Class<R> resultClass, TableDistinctOptions options) Return a list of distinct values for the given field name.- Type Parameters:
R
- type of the result- Parameters:
fieldName
- name of the fieldfilter
- filter to applyresultClass
- class of the resultoptions
- options to apply to the operation- Returns:
- list of distinct values
-
updateOne
Update a single row in the table according to the specified arguments.- Parameters:
filter
- a row describing the query filter, which may not be null.update
- a row describing the update, which may not be null. The update to apply must include at least one update operator.
-
updateOne
public void updateOne(Filter filter, TableUpdateOperation update, TableUpdateOneOptions updateOptions) Update a single document in the collection according to the specified arguments.- Parameters:
filter
- a document describing the query filter, which may not be null.update
- a document describing the update, which may not be null. The update to apply must include at least one update operator.updateOptions
- the options to apply to the update operation
-
deleteOne
Removes at most one rows from the table that matches the given filter. If no rows match, the table is not modified.- Parameters:
filter
- the query filter to apply the delete operation
-
deleteOne
Removes at most one row from the table that matches the given filter. If no rows match, the table is not modified.- Parameters:
filter
- the query filter to apply the delete operationdeleteOneOptions
- the option to driver the deletes (here sort)
-
deleteMany
Removes all rows from the tables that match the given query filter. If no rows match, the table is not modified.- Parameters:
filter
- the query filter to apply the delete operationoptions
- the options to apply to the operation
-
deleteMany
Removes all rows from the table that match the given query filter. If no rows match, the table is not modified.- Parameters:
filter
- the query filter to apply the delete operation
-
deleteAll
public void deleteAll()Removes all rows from the table that match the given query filter. If no rows match, the table is not modified. -
drop
public void drop()Delete the current table -
estimatedRowCount
public long estimatedRowCount()Calling an estimatedRowCount with default options. @seeestimatedRowCount(EstimatedCountRowsOptions)
- Returns:
- the estimated number of rows in the table.
-
estimatedRowCount
Executes the "estimatedRowCount" command to estimate the number of rowse in a table.This method sends a command to estimate the row count and parses the count from the command's response. It handles the execution of the command and the extraction of the row count from the response.
- Parameters:
options
- the options to apply to the operation- Returns:
- the estimated number of rows in the table.
-
countRows
Counts the number of rows in the table.Takes in a `upperBound` option which dictates the maximum number of rows that may be present before a
TooManyRowsToCountException
is thrown. If the limit is higher than the highest limit accepted by the Data API, aTooManyRowsToCountException
will be thrown anyway (i.e. `1000`).Count operations are expensive: for this reason, the best practice is to provide a reasonable `upperBound` according to the caller expectations. Moreover, indiscriminate usage of count operations for sizeable amounts of rows (i.e. in the thousands and more) is discouraged in favor of alternative application-specific solutions. Keep in mind that the Data API has a hard upper limit on the amount of rows it will count, and that an exception will be thrown by this method if this limit is encountered.
- Parameters:
upperBound
- The maximum number of rows to count.- Returns:
- The number of rows in the tables.
- Throws:
TooManyRowsToCountException
- If the number of rows counted exceeds the provided limit.
-
countRows
public int countRows(Filter filter, int upperBound, CountRowsOptions options) throws TooManyRowsToCountException Counts the number of rows in the table with a filter.Takes in a `upperBound` option which dictates the maximum number of rows that may be present before a
TooManyRowsToCountException
is thrown. If the limit is higher than the highest limit accepted by the Data API, aTooManyRowsToCountException
will be thrown anyway (i.e. `1000`).Count operations are expensive: for this reason, the best practice is to provide a reasonable `upperBound` according to the caller expectations. Moreover, indiscriminate usage of count operations for sizeable amounts of rows (i.e. in the thousands and more) is discouraged in favor of alternative application-specific solutions. Keep in mind that the Data API has a hard upper limit on the amount of rows it will count, and that an exception will be thrown by this method if this limit is encountered.
- Parameters:
filter
- A filter to select the row to count. If not provided, all rows will be counted.upperBound
- The maximum number of rows to count.options
- overriding options for the count operation.- Returns:
- The number of rows in the table.
- Throws:
TooManyRowsToCountException
- If the number of rows counted exceeds the provided limit.
-
countRows
Implementation of the @seecountRows(Filter, int, CountRowsOptions)
method with default options.- Parameters:
filter
- filter to countupperBound
- The maximum number of rows to count. It must be lower than the maximum limit accepted by the Data API.- Returns:
- The number of rows in the table.
- Throws:
TooManyRowsToCountException
- If the number of rows counted exceeds the provided limit.
-
listIndexesNames
Retrieves the names of all indices in the keyspace with default options.- Returns:
- A list of all indices names in the database.
Example usage:
List<String> indicesNames = listIndexesNames();
-
listIndexesNames
Retrieves the names of all indices in the keyspace with default options.- Parameters:
listIndexesOptions
- Options for filtering or configuring the indices listing operation.- Returns:
- A list of all indices names in the database.
Example usage:
ListIndexesOptions options = new ListIndexesOptions(); List<String> indicesNames = listIndexesNames(options);
-
listIndexes
Finds all the indices in the selected keyspace.- Returns:
- list of table definitions
-
listIndexes
Finds all the indices in the selected keyspace.- Parameters:
listIndexesOptions
- options for the list indexes operation- Returns:
- list of table definitions
-
registerListener
Register a listener to execute commands on the table. Please now useBaseOptions
.- Parameters:
logger
- name for the loggercommandObserver
- class for the logger
-