Create a global table

Updated at:

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 to 1, maximum time deviation set to Integer.MAX_VALUE, and updateFullRow set to true.

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
Note

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.

Important

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 UpdateRow operation 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)

List<Placement>

The replica placement list. Add at least one placement in a destination region.

syncMode (required)

SyncMode

The synchronization mode. Only ROW, which synchronizes data by row, is supported.

serveMode (optional)

ServeMode

The service mode. Valid values:

  • PRIMARY_SECONDARY: Active-passive mode and the default value. The base table is read-write, replica tables are read-only, and data is synchronized from the base table's region to replica regions.
  • PEER_TO_PEER: Multi-active mode. This mode supports multiple writable replicas and resolves write conflicts at the row or column level with last-writer-wins (LWW).

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: cn-hangzhou.

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: cn-beijing.

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 false in active-passive mode. In multi-active mode, set the parameter based on whether the replica must accept writes.

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 getGlobalTableId(). This ID is required to describe, bind, update, or unbind the global table.

status

GlobalTableStatus

Obtain the current status by calling getStatus(). After the creation task starts, call describeGlobalTable until the status becomes ACTIVE.