Alter a table (Java)
Alters a table by doing one of the following:
-
Adding one or more columns to a table
-
Dropping one or more columns from a table
You cannot change a column’s type. Instead, you must drop the column and add a new column.
After you add a column, you should index the column if you want to filter or sort on the column. For more information, see Create an index (Java) and Create a vector index (Java).
|
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
Adds or drops columns.
Returns a Table<T> instance that represents the table after the schema change.
Although the Table instance that you used to perform the alteration will still work, it will not reflect the updated typing if you added or dropped columns.
To reflect the new typing, use the rowClass parameter.
Parameters
Use the alter method, which belongs to the com.datastax.astra.client.tables.Table class.
Method signature
Table<T> alter(AlterTableOperation operation)
Table<T> alter(
AlterTableOperation operation,
AlterTableOptions options
)
<R> Table<R> alter(
AlterTableOperation operation,
AlterTableOptions options,
Class<R> clazz
)
| Name | Type | Summary |
|---|---|---|
|
|
The alter operation to perform. Can be one of the following:
|
|
Optional. The options for this operation, including the timeout. |
|
|
|
Optional. A specification of the class of the table’s row object. Default: |
Examples
The following examples demonstrate how to alter a table.
Add columns to a table
When you add columns, the columns are defined in the same way as they are when you create a table.
After you add a column, you should index the column if you want to filter or sort on the column. For more information, see Create an index (Java) and Create a vector index (Java).
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.AlterTableAddColumns;
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");
// Add columns
AlterTableAddColumns alterOperation =
new AlterTableAddColumns()
.addColumnBoolean("is_summer_reading")
.addColumnText("library_branch");
table.alter(alterOperation);
}
}
Add vector columns to a table
After you add a vector column, you should index the column if you want run vector searches on the column. For more information, Create a vector index (Java).
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.AlterTableAddColumns;
import com.datastax.astra.client.tables.definition.columns.TableColumnDefinitionVector;
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");
// Add a vector column
AlterTableAddColumns alterOperation =
new AlterTableAddColumns()
.addColumnVector("example_vector", new TableColumnDefinitionVector().dimension(1024));
table.alter(alterOperation);
}
}
Drop columns from a table
Dropping columns produces tombstones. Excessive tombstones can impact query performance.
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.AlterTableDropColumns;
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");
// Drop columns
AlterTableDropColumns alterOperation =
new AlterTableDropColumns("is_summer_reading", "library_branch");
table.alter(alterOperation);
}
}
Client reference
For more information, see the client reference.