Bu konuda, Node.js SDK kullanarak Tablestore'da nasıl veri tablosu oluşturulacağı açıklanmaktadır.
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.
Önkoşullar
Yöntem açıklaması
createTable: function createTable(params, callback)
Örnek kod
Veri tablosu oluşturma
Aşağıdaki örnek kod, String türünde tek bir birincil anahtar sütunu içeren test_table adlı bir tablo oluşturur.
var params = {
// The schema information of the data table
tableMeta: {
tableName: 'test_table',
// At least one primary key column is required to create a data table
primaryKey: [
{
name: 'id',
type: 'STRING'
}
],
// (Optional) Add predefined columns
definedColumn: [
{
name: 'name',
type: TableStore.DefinedColumnType.DCT_STRING
}
]
},
// The configuration information of the data table
tableOptions: {
// When you create a data table, you must specify the TTL. A value of -1 indicates that data never expires
timeToLive: -1,
// When you create a data table, you must specify the maximum number of versions
maxVersions: 1,
// (Optional) Set the maximum version drift
maxTimeDeviation: 86400,
// (Optional) Specify whether to allow updates
allowUpdate: true
},
// When you create a data table, you must set the reserved read and write throughput (only high-performance instances in CU mode support setting non-zero values for reserved read and write throughput)
reservedThroughput: {
capacityUnit: {
read: 0,
write: 0
}
},
// (Optional) Configure Stream information
streamSpecification: {
enableStream: true,
expirationTime: 168
}
};
client.createTable(params, function (err, data) {
if (err) {
console.error('error:', err);
return;
}
console.log('success:', data);
});
İkincil dizinlerle veri tablosu oluşturma
Aşağıdaki örnek kod bir veri tablosu oluşturur ve ikincil dizinleri yapılandırır.
var params = {
// The schema information of the data table
tableMeta: {
tableName: 'test_table',
// Primary key
primaryKey: [
{
name: 'id',
type: 'STRING'
}
],
// Predefined columns
definedColumn: [
{
name: 'name',
type: TableStore.DefinedColumnType.DCT_STRING
}
]
},
// The configuration information of the data table
tableOptions: {
// TTL
timeToLive: -1,
// Maximum number of versions
maxVersions: 1
},
// Reserved read and write throughput (only high-performance instances in CU mode support setting non-zero values for reserved read and write throughput)
reservedThroughput: {
capacityUnit: {
read: 0,
write: 0
}
},
// Secondary index information
indexMetas: [
{
name: 'test_table_index',
// Index primary key
primaryKey: ['id'],
// Index predefined columns
definedColumn: ['name'],
// Index type
indexType: TableStore.IndexType.IT_LOCAL_INDEX,
// Index update mode
indexUpdateMode: TableStore.IndexUpdateMode.IUM_SYNC_INDEX
}
]
};
client.createTable(params, function (err, data) {
if (err) {
console.error('error:', err);
return;
}
console.log('success:', data);
});