Crie uma tabela global no Tablestore para replicar dados entre regiões, garantindo alta disponibilidade e acesso com baixa latência.
Observações de uso
Escolha um modo de serviço antes de criar a tabela global:
Modo ativo-passivo (
PRIMARY_SECONDARY): A região primária processa leituras e gravações, enquanto as secundárias operam em somente leitura. Os dados fluem da região primária para as secundárias. Use este modo para leituras locais, recuperação de desastres e standby ativo.Modo multiativo (
PEER_TO_PEER): Todas as regiões processam leituras e gravações. O sistema resolve conflitos no nível de linha ou coluna pela estratégia last-writer-wins (LWW), mantendo a versão com o timestamp mais recente. Adote este modo para operações de leitura e escrita com baixa latência em várias regiões.
Após o sucesso da solicitação de criação, o sistema replica os dados entre regiões de forma assíncrona. Chame DescribeGlobalTable para monitorar o status. A tabela global estará pronta quando o status mudar para active.
Pré-requisitos
Antes de começar, verifique se você possui:
Instâncias criadas nas regiões de source e de destino
-
Uma tabela na região de source com a seguinte configuração: tempo de vida (TTL) definido como
-1(dados nunca expiram), máximo de versões definido como1, deslocamento máximo de versão ilimitado (Integer.MAX_VALUE) e versionamento de linhas ativado (updateFullRowdefinido comoTrue)ImportanteAtivar o versionamento de linhas impõe os seguintes limites:
Uma única linha pode conter no máximo 256 colunas
O sistema gera os números de versão automaticamente; não é possível especificá-los manualmente durante a gravação de dados
Cada operação
Updatelê primeiro a versão atual da linha, o que aumenta ligeiramente a carga de leitura
-
Importante
Se você inicializar o cliente com a AccessKey de um usuário do Resource Access Management (RAM), esse usuário precisará ter permissão para criar tabelas globais. Para consultar as definições necessárias de
Action, veja Operações da API do Tablestore.
Método
public CreateGlobalTableResponse createGlobalTable(CreateGlobalTableRequest createGlobalTableRequest) throws TableStoreException, ClientException
Exemplos
Criar uma tabela global no modo ativo-passivo
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. Only ROW (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 creates the table automatically.
"i-dest-test",
// false = read-only secondary in active-passive mode.
false
));
// Set the service mode to active-passive (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());
}
Criar uma tabela global no modo multiativo
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. Only ROW (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 creates the table automatically.
"i-dest-test",
// true = all regions accept writes in multi-active mode.
true
));
// Set the service mode to multi-active (PEER_TO_PEER).
req.setServeMode(GlobalTableTypes.ServeMode.PEER_TO_PEER);
// 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());
}