This topic describes how to create a global table in Tablestore using the Java SDK.
Precautions
After the creation operation is successful, the system asynchronously synchronizes the data. You can call the `DescribeGlobalTable` operation to check the status of the global table. The global table is created when its status changes to `active`.
Prerequisites
Create instances in the source and destination regions.
Create a data table in the source region. The table must have the following configurations: time to live (TTL) is set to -1 (data never expires), max versions is set to 1, max version drift is set to unlimited (`Integer.MAX_VALUE`), and row versioning is enabled (`updateFullRow` is `True`).
ImportantThe following limits apply after you enable row versioning:
A single row can contain a maximum of 256 columns.
You cannot manually specify version numbers when you write data. The system generates them automatically.
When you perform an `Update` operation, the system first reads the current row version. This action results in a minor increase in read load.
Method description
public CreateGlobalTableResponse createGlobalTable(CreateGlobalTableRequest createGlobalTableRequest) throws TableStoreException, ClientExceptionExamples
Create a global table in active-passive mode.
private static void createGlobalTableExample(SyncClient client){
// Construct the request.
CreateGlobalTableRequest req = new CreateGlobalTableRequest(
// Configure the base table. The table must already exist.
new GlobalTableTypes.BaseTable(
// The region ID of the base table.
"cn-hangzhou",
// The instance name of the base table.
"i-gt-test",
// The name of the base table. This is also the name of the global table.
"t-gt-test-1"
),
// The synchronization mode of the global table. Currently, only the ROW mode (row-level synchronization) is supported.
GlobalTableTypes.SyncMode.ROW
);
// Add associated tables. You can add multiple tables.
req.addPlacement(new GlobalTableTypes.Placement(
// The region ID of the associated table.
"cn-shanghai",
// The instance name of the associated table. The instance must already exist.
// The instance cannot contain a table with the same name as the base table. The system automatically creates the table.
"i-dest-test",
// Specifies whether the table is writable. false indicates read-only. In active-passive mode, this can only be set to false.
false
));
// Set the service mode to primary/secondary architecture (PRIMARY_SECONDARY).
req.setServeMode(GlobalTableTypes.ServeMode.PRIMARY_SECONDARY);
// Send the request.
CreateGlobalTableResponse createResp = client.createGlobalTable(req);
// Print the response.
System.out.println("Global table creation initiated.");
System.out.println("RequestId: " + createResp.getRequestId());
System.out.println("GlobalTableId: " + createResp.getGlobalTableId());
}