Este tópico descreve como criar uma tabela de dados no Tablestore com o SDK para Node.js.
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
createTable: function createTable(params, callback)
Código de exemplo
Criar uma tabela de dados
O código de exemplo a seguir cria uma tabela test_table com uma coluna de chave primária do tipo String.
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);
});
Criar uma tabela de dados com índices secundários
O código de exemplo abaixo cria uma tabela de dados e configura índices secundários.
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);
});