Este tópico descreve como criar uma tabela de dados no Tablestore usando o SDK do .NET.
Observações de uso
Após criar uma tabela de dados, aguarde o carregamento da tabela antes de executar operações nos dados. Caso contrário, as operações falharão. Esse processo geralmente leva alguns segundos.
Pré-requisitos
É necessário ter um cliente inicializado. Para mais informações, consulte Initialize a Tablestore client.
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}");
}
Consulte também o código de exemplo a seguir e configure definições adicionais ao criar uma tabela de dados.
-
Adicione colunas predefinidas
DefinedColumnSchema definedColumnSchema = new DefinedColumnSchema { { "name" , DefinedColumnType.STRING } }; tableMeta.DefinedColumnSchema = definedColumnSchema; -
especifique o desvio máximo de versão
TableOptions tableOptions = new TableOptions(); tableOptions.DeviationCellVersionInSec = 86400; request.TableOptions = tableOptions; -
especifique se as atualizações são permitidas
TableOptions tableOptions = new TableOptions(); tableOptions.AllowUpdate = false; request.TableOptions = tableOptions; -
Adicione 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); -
especifique as informações do Stream
StreamSpecification streamSpecification = new StreamSpecification(true); streamSpecification.ExpirationTime = 168; request.StreamSpecification = streamSpecification;