Cette rubrique explique comment créer une table de données dans Tablestore à l'aide du SDK Node.js.
Remarques sur l'utilisation
Après la création d'une table de données, attendez qu'elle soit chargée avant d'effectuer des opérations sur les données, faute de quoi elles échoueront. Ce processus prend généralement quelques secondes.
Prérequis
Description de la méthode
createTable: function createTable(params, callback)
Exemple de code
Créer une table de données
L'exemple de code suivant crée une table test_table contenant une colonne de clé primaire de type 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);
});
Créer une table de données avec des index secondaires
L'exemple de code suivant crée une table de données et configure des index secondaires.
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);
});