Tablestore provides the PutRow and UpdateRow operations to allow you to write a single row of data, and the BatchWriteRow operation to allow you to write multiple rows of data in a batch.
Prerequisites
- The OTSClient instance is initialized. For more information, see Initialization.
- A data table is created. Data is written to the table.
Insert a single row of data
You can call the PutRow operation to insert a row of data. If the row exists, the PutRow operation deletes all versions of data in all columns from the existing row, and then inserts a new row.
API operations
/**
* Insert data into a specified row. If the row exists, the existing data is overwritten. Otherwise, a new row is added.
*/
putRow(params, callback)
Parameters
Parameter | Description |
---|---|
tableName | The name of the data table. |
primaryKey | The primary key of the row.
Note
|
condition | The condition that you want to configure to perform the PutRow operation. You can
configure a row existence condition or a condition based on column values. For more
information, see Configure conditional update.
Note
|
attributeColumns | The attribute column of the row.
|
returnContent | The content that you want to return.
returnType: You can set the value to TableStore.ReturnType.Primarykey to return the primary key of the row. This parameter is used by the auto-increment primary key column feature. |
Examples
The following code provides an example on how to insert a row of data:
var TableStore = require('../index.js');
var Long = TableStore.Long;
var client = require('./client');
var currentTimeStamp = Date.now();
var params = {
tableName: "sampleTable",
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(20013) }, { 'uid': Long.fromNumber(20013) }],
attributeColumns: [
{ 'col1': 'Tablestore' },
{ 'col2': '2', 'timestamp': currentTimeStamp },
{ 'col3': 3.1 },
{ 'col4': -0.32 },
{ 'col5': Long.fromNumber(123456789) }
],
returnContent: { returnType: TableStore.ReturnType.Primarykey }
};
client.putRow(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
To view the detailed sample code, visit PutRow@GitHub.
Update a single row of data
API operations
/**
* Update the data of the specified row. If the row does not exist, a new row is added. If the row exists, the values of the specified columns are added, modified, or deleted based on the request content.
*/
updateRow(params, callback)
Parameters
Parameter | Description |
---|---|
tableName | The name of the data table. |
primaryKey | The primary key of the row.
Note The number and types of the primary key columns that you specify must be the same
as the actual number and types of primary key columns in the data table.
|
condition | The condition that you want to specify to perform the UpdateRow operation. You can specify a row existence condition or a condition based on column values. For more information, see Configure conditional update. |
updateOfAttributeColumns | The attribute columns you want to update.
|
Examples
The following code provides an example on how to update the data of a specified row:
var TableStore = require('../index.js');
var Long = TableStore.Long;
var client = require('./client');
var params = {
tableName: "sampleTable",
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(9) }, { 'uid': Long.fromNumber(90) }],
updateOfAttributeColumns: [
{ 'PUT': [{ 'col4': Long.fromNumber(4) }, { 'col5': '5' }, { 'col6': Long.fromNumber(6) }] },
{ 'DELETE': [{ 'col1': Long.fromNumber(1496826473186) }] },
{ 'DELETE_ALL': ['col2'] }
]
};
client.updateRow(params,
function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
To view the detailed sample code, visit UpdateRow@GitHub.
Write multiple rows of data in a batch
You can call the BatchWriteRow operation to write multiple rows to one or more tables in a batch. The BatchWriteRow operation is a set of PutRow, UpdateRow, or DeleteRow operations. When you call the BatchWriteRow operation, the process of constructing the PutRow, UpdateRow, or DeleteRow operations is the same as the process of constructing the PutRow, UpdateRow, or DeleteRow operation when you call the PutRow, UpdateRow, or DeleteRow operation. BatchWriteRow supports conditional update.
If you call the BatchWriteRow operation, each PutRow, UpdateRow, or DeleteRow operation is separately performed and the response to each PutRow, UpdateRow, or DeleteRow operation is separately returned.
Usage notes
When you call the BatchWriteRow operation to write multiple rows in a batch, some rows may fail to be written. If this happens, Tablestore does not return exceptions, but returns BatchWriteRowResponse in which the indexes and error messages of the failed rows are included. Therefore, when you call the BatchWriteRow operation, you must check the return values to determine whether the operation on each row is successful. If you do not check the return values, failures of operations on some rows are ignored.
If the server detects that invalid parameters exist in some operations, the BatchWriteRow operation may return an exception about parameter errors before the first operation in the request is performed.
API operations
/**
* Modify multiple rows of data.
*/
batchWriteRow(params, callback)
Parameters
- Hierarchies are created for tables. Multiple tables can be processed in a batch.
You can configure the tables parameter to specify information about tables and rows on which you want to perform the write, update, or delete operations.
- The type parameter is added to distinguish between different operation types.
You can set the operation type to PUT, UPDATE, or DELETE.
- If you set the operation type to PUT or UPDATE, primaryKey and attributeColumns are valid.
- If you set the operation type to DELETE, primaryKey is valid.
Examples
The following code provides an example on how to write multiple rows of data in a batch:
var client = require('./client');
var TableStore = require('../index.js');
var Long = TableStore.Long;
var params = {
tables: [
{
tableName: 'sampleTable',
rows: [
{
type: 'UPDATE',
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(20010) }, { 'uid': Long.fromNumber(20010) }],
attributeColumns: [{ 'PUT': [{ 'col1': 'test3' }, { 'col2': 'test4' }] }],
returnContent: { returnType: 1 }
},
{
type: 'PUT',
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(20020) }, { 'uid': Long.fromNumber(20020) }],
attributeColumns: [{ 'col1': 'test1' }, { 'col2': 'test2' }],
returnContent: { returnType: TableStore.ReturnType.Primarykey }
},
{
type: 'DELETE',
condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
primaryKey: [{ 'gid': Long.fromNumber(20018) }, { 'uid': Long.fromNumber(20018) }],
}
]
}
],
};
client.batchWriteRow(params, function (err, data) {
if (err) {
console.log('error:', err);
return;
}
console.log('success:', data);
});
To view the detailed sample code, visit BatchWriteRow@GitHub.