Este tema describe cómo crear una tabla de datos en Tablestore mediante el SDK de Node.js.
Notas de uso
Después de crear una tabla de datos, espere a que se cargue antes de realizar operaciones sobre los datos. De lo contrario, las operaciones fallarán. Este proceso suele tardar varios segundos.
Requisitos previos
Descripción del método
createTable: function createTable(params, callback)
Código de ejemplo
Crear una tabla de datos
El siguiente código de ejemplo crea una tabla test_table que contiene una columna de clave primaria de 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);
});
Crear una tabla de datos con índices secundarios
El siguiente código de ejemplo crea una tabla de datos y configura índices secundarios.
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);
});