Create a data table based on your business requirements.
Create a table without indexes
The following sample code provides an example on how to create a data table without an index. The primary key column of the data table is the pk column of the STRING type. A maximum of three versions of data can be retained for each attribute column, and the data never expires.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Add a primary key column to the data table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk", PrimaryKeyType.STRING));
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
int timeToLive = -1;
// Specify the maximum number of data versions that can be retained for each column. A value of 1 specifies that only the latest version of data is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table.
int maxVersions = 3;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0))); // Specify the reserved read throughput and reserved write throughout. The reserved read throughput and reserved write throughout can be set only to 0 for data tables in capacity instances. You can set the reserved read throughput and reserved write throughout to a non-zero value for data tables in high-performance instances.
client.createTable(request);
}
Create a table and a global secondary index
The following sample code provides an example on how to create a data table and configure a global secondary index for the data table at the same time. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. The data table also contains two predefined columns: the defcol1 column of the STRING type and the defcol2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires. The primary key columns of the global secondary index are the defcol1, pk1, and pk2 columns. The attribute column of the global secondary index is the defcol2 column.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Add primary key columns to the data table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Add predefined columns to the data table.
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
int timeToLive = -1;
// Specify the maximum number of data versions that can be retained for each column. A value of 1 specifies that only the latest version of data is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// Specify the name of the index table.
IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
// Add a primary key column to the index table.
indexMeta.addPrimaryKeyColumn("defcol1");
// Add an attribute column to the index table.
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); // Create the index table when you create the data table.
client.createTable(request);
}
Create a table and a local secondary index
The following sample code provides an example on how to create a data table and configure a local secondary index for the data table at the same time. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. The data table also contains two predefined columns: the defcol1 column of the STRING type and the defcol2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires. The primary key columns of the local secondary index are the defcol1, pk1, and pk2 columns. The attribute column of the local secondary index is the defcol2 column.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Add primary key columns to the data table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Add predefined columns to the data table.
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol1", DefinedColumnType.STRING));
tableMeta.addDefinedColumn(new DefinedColumnSchema("defcol2", DefinedColumnType.INTEGER));
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
int timeToLive = -1;
// Specify the maximum number of data versions that can be retained for each column. A value of 1 specifies that only the latest version of data is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
ArrayList<IndexMeta> indexMetas = new ArrayList<IndexMeta>();
// Specify the name of the index table.
IndexMeta indexMeta = new IndexMeta("<INDEX_NAME>");
// Set the index type to IT_LOCAL_INDEX.
indexMeta.setIndexType(IT_LOCAL_INDEX);
// Set the index update mode to IUM_SYNC_INDEX. If the indexType parameter is set to IT_LOCAL_INDEX, you must set the indexUpdateMode parameter to IUM_SYNC_INDEX.
indexMeta.setIndexUpdateMode(IUM_SYNC_INDEX);
// Add a primary key column to the index table. The first primary key column of the index table must be the same as the first primary key column of the data table.
indexMeta.addPrimaryKeyColumn("pk1");
// Add a primary key column to the index table.
indexMeta.addPrimaryKeyColumn("defcol1");
// Add an attribute column to the index table.
indexMeta.addDefinedColumn("defcol2");
indexMetas.add(indexMeta);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions, indexMetas); // Create the index table when you create the data table.
client.createTable(request);
}
Create a table with local transaction enabled
The following sample code provides an example on how to enable the local transaction feature when you create a data table. The data table contains two primary key columns: the pk1 column of the STRING type and the pk2 column of the INTEGER type. Only the latest version of data is retained for each attribute column, and the data never expires.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Add primary key columns to the data table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER));
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
int timeToLive = -1;
// Specify the maximum number of data versions that can be retained for each column. A value of 1 specifies that only the latest version of data is retained for each column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
// Enable the local transaction feature. If you enable the auto-increment primary key column feature for the data table, the configurations of the local transaction feature do not take effect.
request.setLocalTxnEnabled(true);
client.createTable(request);
}
Create a table with KMS key-based encryption enabled
The following sample code provides an example on how to enable KMS key-based encryption for a data table when you create the data table. In this example, the data table contains the pk primary key column of the STRING type, only the latest version of data is retained for each attribute column, and the data in the data table never expires.
private static void createTable(SyncClient client) {
// Specify the name of the data table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// If you want to configure data encryption, configure encryption-related parameters in the createTable request. The following encryption modes are supported: KMS key-based encryption and BYOK-based encryption. In this example, KMS key-based encryption is enabled.
SSESpecification sseKms = new SSESpecification(true, SSEKeyType.SSE_KMS_SERVICE);
// Add a primary key column to the data table.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk", PrimaryKeyType.STRING));
// Specify the validity period of data. A value of -1 specifies that the data never expires. Unit: seconds. You must set the timeToLive parameter to -1 for a data table for which you want to create an index table.
int timeToLive = -1;
// Specify the maximum number of data versions that can be retained for each attribute column. A value of 1 specifies that only the latest version of data is retained for each attribute column. You must set the maxVersions parameter to 1 for a data table for which you want to create an index table.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
// Specify the reserved read throughput and reserved write throughput. The values must be set to 0 for data tables in capacity instances and can be set to non-zero values for data tables in high-performance instances.
request.setReservedThroughput(new ReservedThroughput(new CapacityUnit(0, 0)));
request.setSseSpecification(sseKms);
client.createTable(request);
}
Create a table with an auto-increment primary key column
The following sample code provides an example on how to create a table that contains an auto-increment primary key column. The table contains the pk1 primary key column whose data type is STRING and the pk2 primary key column whose data type is INTEGER. The pk1 column is a partition key column, and the pk2 column is an auto-increment primary key column.
private static void createTable(SyncClient client) {
// Specify the name of the table.
TableMeta tableMeta = new TableMeta("<TABLE_NAME>");
// Create the first primary key column. The first primary key column is a partition key column.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk1", PrimaryKeyType.STRING));
// Create the second primary key column whose data type is INTEGER and set the attribute of the column to AUTO_INCREMENT.
tableMeta.addPrimaryKeyColumn(new PrimaryKeySchema("pk2", PrimaryKeyType.INTEGER, PrimaryKeyOption.AUTO_INCREMENT));
// Specify the time to live (TTL) of data. A value of -1 specifies that the data never expires. Unit: seconds.
int timeToLive = -1;
// Specify the maximum number of versions that can be retained for each column. A value of 1 specifies that only the latest version is retained for each column.
int maxVersions = 1;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
CreateTableRequest request = new CreateTableRequest(tableMeta, tableOptions);
client.createTable(request);
}
After you create a data table, you can perform operations to manage the data table. The following table describes the operations.
Query the names of tables
The following sample code provides an example on how to query the names of all tables in an instance:
private static void listTable(SyncClient client) {
ListTableResponse response = client.listTable();
System.out.println("List of tables:");
for (String tableName : response.getTableNames()) {
System.out.println(tableName);
}
}
Update the configurations of a table
The following sample code provides an example on how to update the value of the maxVersions parameter for the example table to 5:
private static void updateTable(SyncClient client) {
// Specify the time to live (TTL) of data. A value of -1 specifies that the data never expires. Unit: seconds.
int timeToLive = -1;
// Update the max versions of the table to five.
int maxVersions = 5;
TableOptions tableOptions = new TableOptions(timeToLive, maxVersions);
// Specify the name of the table.
UpdateTableRequest request = new UpdateTableRequest("<TABLE_NAME>");
request.setTableOptionsForUpdate(tableOptions);
client.updateTable(request);
}
Query the information about a table
The following sample code provides an example on how to query the schema information, optional configurations, reserved read throughput, and reserved write throughput of a table:
private static void describeTable(SyncClient client) {
DescribeTableRequest request = new DescribeTableRequest(TABLE_NAME);
DescribeTableResponse response = client.describeTable(request);
TableMeta tableMeta = response.getTableMeta();
System.out.println("The name of the table:" + tableMeta.getTableName());
System.out.println("The primary key of the table:");
for (PrimaryKeySchema primaryKeySchema : tableMeta.getPrimaryKeyList()) {
System.out.println(primaryKeySchema);
}
TableOptions tableOptions = response.getTableOptions();
System.out.println("The TTL of the table:" + tableOptions.getTimeToLive());
System.out.println("MaxVersions of the table:" + tableOptions.getMaxVersions());
System.out.println("The encryption configuration of the table:" + response.getSseDetails());
ReservedThroughputDetails reservedThroughputDetails = response.getReservedThroughputDetails();
System.out.println("The reserved read throughput of the table:"
+ reservedThroughputDetails.getCapacityUnit().getReadCapacityUnit());
System.out.println("The reserved write throughput of the table:"
+ reservedThroughputDetails.getCapacityUnit().getWriteCapacityUnit());
}
Manage predefined columns
The following sample code provides an example on how to add predefined columns to a table. In this example, the following predefined columns are added: definedColumnName01 (type: STRING), definedColumnName02 (type: INTEGER), and definedColumnName03 (type: STRING).
public static void addDefinedColumnRequest(SyncClient client) {
AddDefinedColumnRequest addDefinedColumnRequest = new AddDefinedColumnRequest();
// Specify the name of the table.
addDefinedColumnRequest.setTableName("<TABLE_NAME>");
// Add predefined columns to the table.
addDefinedColumnRequest.addDefinedColumn("definedColumnName01",DefinedColumnType.STRING);
addDefinedColumnRequest.addDefinedColumn("definedColumnName02",DefinedColumnType.INTEGER);
addDefinedColumnRequest.addDefinedColumn("definedColumnName03",DefinedColumnType.STRING);
client.addDefinedColumn(addDefinedColumnRequest);
}
You can remove the predefined columns that you no longer require.
The following sample code provides an example on how to remove the definedColumnName01 and definedColumnName02 predefined columns from a table:
public static void deleteDefinedColumnRequest(SyncClient client){
DeleteDefinedColumnRequest deleteDefinedColumnRequest = new DeleteDefinedColumnRequest();
// Specify the name of the table.
deleteDefinedColumnRequest.setTableName("<TABLE_NAME>");
// Remove predefined columns from the table.
deleteDefinedColumnRequest.addDefinedColumn("definedColumnName01");
deleteDefinedColumnRequest.addDefinedColumn("definedColumnName02");
client.deleteDefinedColumn(deleteDefinedColumnRequest);
}
Delete a data table
The following sample code provides an example on how to delete the exampletable table:
private static void deleteTable(SyncClient client) {
// Specify the name of the data table.
DeleteTableRequest request = new DeleteTableRequest("<TABLE_NAME>");
client.deleteTable(request);
}