Este tópico descreve como criar uma tabela de dados no Tablestore usando o SDK para .NET.
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
Inicialize um cliente. Para mais informações, consulte Inicializar um cliente do Tablestore.
Descrição do método
public CreateTableResponse CreateTable(CreateTableRequest request)
Método assíncrono:
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request)
Código de exemplo
O código de exemplo a seguir cria uma tabela chamada test_table contendo uma coluna de chave primária do tipo String.
try
{
// At least one primary key column is required to create a data table.
PrimaryKeySchema primaryKeySchema = new PrimaryKeySchema
{
{ "id", ColumnValueType.String }
};
// Construct the schema information of the data table.
TableMeta tableMeta = new TableMeta("test_table", primaryKeySchema);
// You must set the reserved read and write throughput when you create a data table (you can set this parameter to a non-zero value and the setting takes effect only for data tables in high-performance instances in CU mode).
CapacityUnit reservedThroughput = new CapacityUnit(0, 0);
// Construct the request and send it.
CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput);
client.CreateTable(request);
Console.WriteLine("Create table succeeded.");
}
catch (Exception ex)
{
Console.WriteLine($"Create table failed, exception:{ex.Message}");
}
Use também os exemplos abaixo para aplicar configurações adicionais durante a criação da tabela de dados.
-
Adicionar colunas predefinidas
DefinedColumnSchema definedColumnSchema = new DefinedColumnSchema { { "name" , DefinedColumnType.STRING } }; tableMeta.DefinedColumnSchema = definedColumnSchema; -
Especificar o desvio máximo de versão
TableOptions tableOptions = new TableOptions(); tableOptions.DeviationCellVersionInSec = 86400; request.TableOptions = tableOptions; -
Definir permissão de atualizações
TableOptions tableOptions = new TableOptions(); tableOptions.AllowUpdate = false; request.TableOptions = tableOptions; -
Incluir um índice secundário
// Construct a list of secondary indexes. List<IndexMeta> indexMetas = new List<IndexMeta>(); // Construct a secondary index. IndexMeta indexMeta = new IndexMeta("test_table_index"); // Specify the primary key of the index. indexMeta.PrimaryKey = new List<string>() { "id", "name" }; // Specify the index type. indexMeta.IndexType = IndexType.IT_GLOBAL_INDEX; // Specify the index update mode. indexMeta.IndexUpdateModel = IndexUpdateMode.IUM_ASYNC_INDEX; // Add the secondary index. indexMetas.Add(indexMeta); // Construct the request. CreateTableRequest request = new CreateTableRequest(tableMeta, reservedThroughput, indexMetas); -
Configurar informações do Stream
StreamSpecification streamSpecification = new StreamSpecification(true); streamSpecification.ExpirationTime = 168; request.StreamSpecification = streamSpecification;