Create a global table
The Tablestore SDK for Java can create an active-passive or multi-active global table to synchronize data across regions.
Prerequisites
Before you begin, make sure that you have:
-
Install the Tablestore SDK for Java and initialize the client. Version 5.17.7 or later is required.
-
Tablestore instances in both the source and destination regions.
-
A base table in the source region with the time to live (TTL) set to
-1, maximum versions set to1, maximum time deviation set toInteger.MAX_VALUE, andupdateFullRowset totrue.
Feature description
Call createGlobalTable to create a cross-region global table from an existing table. A global table supports active-passive and multi-active modes. During creation, Tablestore automatically creates a replica table with the same name as the base table in each destination instance. Therefore, a destination instance cannot already contain a table with that name.
public CreateGlobalTableResponse createGlobalTable(CreateGlobalTableRequest createGlobalTableRequest) throws TableStoreException, ClientException
createGlobalTable is asynchronous. A successful response indicates only that the task has started. Call describeGlobalTable and wait until status becomes ACTIVE before you use the global table.
Enabling whole-row updates by setting updateFullRow to true applies the following limits:
-
A row can contain up to 256 columns.
-
Tablestore generates version numbers. You cannot specify a version number when you write data.
-
An
UpdateRowoperation reads the current row before the update and increases the read load.
The following examples create global tables in active-passive and multi-active modes.
Active-passive
The following example creates a global table in active-passive mode. The base table is read-write, and the replica table is read-only. If you omit serveMode, Tablestore uses the default value PRIMARY_SECONDARY.
GlobalTableTypes.BaseTable baseTable = new GlobalTableTypes.BaseTable(
"cn-hangzhou", "source-instance", "example_table");
CreateGlobalTableRequest request = new CreateGlobalTableRequest(
baseTable, GlobalTableTypes.SyncMode.ROW);
request.addPlacement(new GlobalTableTypes.Placement(
"cn-beijing", "replica-instance", false));
CreateGlobalTableResponse response = client.createGlobalTable(request);
System.out.println("GlobalTableId: " + response.getGlobalTableId());
System.out.println("Status: " + response.getStatus());
Multi-active
The following example creates a global table in multi-active mode. Set the destination replica to writable and set serveMode to PEER_TO_PEER so that both the base table and the replica table accept writes.
GlobalTableTypes.BaseTable baseTable = new GlobalTableTypes.BaseTable(
"cn-hangzhou", "source-instance", "example_table");
CreateGlobalTableRequest request = new CreateGlobalTableRequest(
baseTable, GlobalTableTypes.SyncMode.ROW);
request.addPlacement(new GlobalTableTypes.Placement(
"cn-beijing", "replica-instance", true));
request.setServeMode(GlobalTableTypes.ServeMode.PEER_TO_PEER);
CreateGlobalTableResponse response = client.createGlobalTable(request);
System.out.println("GlobalTableId: " + response.getGlobalTableId());
System.out.println("Status: " + response.getStatus());
Parameters
CreateGlobalTableRequest contains the following parameters.
|
Name |
Type |
Description |
|
baseTable (required) |
BaseTable |
The base table. The base table is the existing table from which you create the global table. |
|
placements (required) |
|
The replica placement list. Add at least one placement in a destination region. |
|
syncMode (required) |
SyncMode |
The synchronization mode. Only |
|
serveMode (optional) |
ServeMode |
The service mode. Valid values:
|
Base table
baseTable is a BaseTable object that contains the following parameters.
|
Name |
Type |
Description |
|
regionId (required) |
String |
The ID of the region that contains the base table. Example: |
|
instanceName (required) |
String |
The name of the instance that contains the base table. |
|
tableName (required) |
String |
The base table name, which is also the global table name. |
Replica placement
Each element in placements[] is a Placement object that contains the following parameters.
|
Name |
Type |
Description |
|
regionId (required) |
String |
The ID of the region that contains the replica. Example: |
|
instanceName (required) |
String |
The name of the instance that contains the replica. |
|
writable (required) |
boolean |
Specifies whether the replica accepts writes. Set this parameter to |
Response
CreateGlobalTableResponse contains the following fields that are specific to the creation result.
|
Field |
Type |
Description |
|
globalTableId |
String |
Obtain the global table ID by calling |
|
status |
GlobalTableStatus |
Obtain the current status by calling |