Este tópico descreve como usar o Tablestore SDK for Go para criar uma tabela de dados.
Observações de uso
Após criar uma tabela de dados, aguarde o carregamento completo antes de executar operações nos dados. Caso contrário, as operações falharão. Esse processo geralmente leva alguns segundos.
Pré-requisitos
Descrição do método
func (tableStoreClient *TableStoreClient) CreateTable(request *CreateTableRequest) (*CreateTableResponse, error)
Código de exemplo
O código de exemplo a seguir cria uma tabela de dados chamada test_table com uma coluna de chave primária do tipo string.
func CreateTableSample(client *tablestore.TableStoreClient) {
// Construct the schema information of the data table
tableMeta := new(tablestore.TableMeta)
tableMeta.TableName = "test_table"
// At least one primary key column is required to create a data table
tableMeta.AddPrimaryKeyColumn("id", tablestore.PrimaryKeyType_STRING)
// Construct the configuration information of the data table
tableOption := new(tablestore.TableOption)
// You must specify the maximum number of versions when you create a data table
tableOption.MaxVersion = 1
// You must specify the TTL when you create a data table. A value of -1 indicates that the data never expires
tableOption.TimeToAlive = -1
// You must set the reserved read and write throughput when you create a data table. The default value is 0 (Only high-performance instances in CU mode support setting the reserved read and write throughput of a data table to a non-zero value)
reservedThroughput := new(tablestore.ReservedThroughput)
reservedThroughput.Readcap = 0
reservedThroughput.Writecap = 0
// Construct the request and send it
createTableRequest := new(tablestore.CreateTableRequest)
createTableRequest.TableMeta = tableMeta
createTableRequest.TableOption = tableOption
createTableRequest.ReservedThroughput = reservedThroughput
_, err := client.CreateTable(createTableRequest)
if err != nil {
fmt.Println("Failed to create table with error:", err)
} else {
fmt.Println("Create table finished.")
}
}
Use os códigos de exemplo a seguir para definir configurações adicionais ao criar uma tabela de dados.
-
Adicionar colunas predefinidas
tableMeta.AddDefinedColumn("name", tablestore.DefinedColumn_STRING) -
Definir o deslocamento máximo de versão
tableOption.DeviationCellVersionInSec = 86400 -
Definir permissão de atualizações
tableOption.AllowUpdate = proto.Bool(false) -
Adicionar um índice secundário
// Construct a secondary index indexMeta := new(tablestore.IndexMeta) indexMeta.IndexName = "test_table_index" // Set the primary key columns of the index indexMeta.AddPrimaryKeyColumn("id") indexMeta.AddPrimaryKeyColumn("name") // Set the index type indexMeta.IndexType = tablestore.IT_LOCAL_INDEX // Add the secondary index createTableRequest.AddIndexMeta(indexMeta) -
Configurar definições do Stream
streamSpec := new(tablestore.StreamSpecification) streamSpec.EnableStream = true streamSpec.ExpirationTime = 168 createTableRequest.StreamSpec = streamSpec -
Ativar transações locais
enableLocalTxn := proto.Bool(true) createTableRequest.EnableLocalTxn = enableLocalTxn -
Definir o tipo de criptografia de dados
-
Criptografia baseada em KMS
sseSpec := new(tablestore.SSESpecification) sseSpec.SetEnable(true) sseSpec.SetKeyType(tablestore.SSE_KMS_SERVICE) createTableRequest.SSESpecification = sseSpec -
Criptografia BYOK
NotaAntes de executar o código, obtenha o ID da CMK e o ARN da função do RAM. Para mais informações, consulte Criptografia BYOK.
sseSpec := new(tablestore.SSESpecification) sseSpec.SetEnable(true) sseSpec.SetKeyType(tablestore.SSE_BYOK) sseSpec.SetKeyId("key-hzz65****************") sseSpec.SetRoleArn("acs:ram::1705************:role/tabletorebyok") createTableRequest.SSESpecification = sseSpec
-