Bu konu, .NET SDK kullanarak Tablestore'da veri tablosunun nasıl oluşturulacağını açıklar.
Kullanım notları
Veri tablosunu oluşturduktan sonra veriler üzerinde işlem yapmadan önce tablonun yüklenmesini bekleyin. Aksi takdirde işlemler başarısız olur. Bu süreç genellikle birkaç saniye sürer.
Ön koşullar
İstemci başlatılmıştır. Daha fazla bilgi için bkz. Tablestore istemcisini başlatma.
Yöntem açıklaması
public CreateTableResponse CreateTable(CreateTableRequest request)
Eşzamansız yöntem:
public Task<CreateTableResponse> CreateTableAsync(CreateTableRequest request)
Örnek kod
Aşağıdaki örnek kod, String türünde bir birincil anahtar sütunu içeren test_table adlı bir tablo oluşturur.
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}");
}
Veri tablosu oluştururken ek ayarları yapılandırmak için aşağıdaki örnek koda da bakabilirsiniz.
-
Önceden tanımlanmış sütun ekleme
DefinedColumnSchema definedColumnSchema = new DefinedColumnSchema { { "name" , DefinedColumnType.STRING } }; tableMeta.DefinedColumnSchema = definedColumnSchema; -
Maksimum sürüm ofsetini belirtme
TableOptions tableOptions = new TableOptions(); tableOptions.DeviationCellVersionInSec = 86400; request.TableOptions = tableOptions; -
Güncellemelere izin verilip verilmeyeceğini belirtme
TableOptions tableOptions = new TableOptions(); tableOptions.AllowUpdate = false; request.TableOptions = tableOptions; -
İkincil dizin ekleme
// 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); -
Stream bilgilerini belirtme
StreamSpecification streamSpecification = new StreamSpecification(true); streamSpecification.ExpirationTime = 168; request.StreamSpecification = streamSpecification;