edit-icon download-icon

Multiple-row operations

Last Updated: Apr 08, 2018

The Table Store SDK provides the following multi-row operation APIs: BatchGetRow, BatchWriteRow, GetRange, and GetByIterator.

BatchGetRow

Reads several data rows in batches from one or more tables.

The BatchGetRow operation is essentially a set of multiple GetRow operations. Each operation is executed, results are returned, and capacity units are consumed independently.

Compared to the execution of a large number of GetRow operations, the BatchGetRow operation effectively reduces the request response time and increases the data read rate.

API

  1. /**
  2. * Read several data rows in batches from one or more tables.
  3. */
  4. batchGetRow(params, callback)

Example

Read multiple tables and multiple data rows in batches, and retry the operation if an error occurs in a single row.

  1. var client = require('./client');
  2. var TableStore = require('../index.js');
  3. var Long = TableStore.Long;
  4. var params = {
  5. tables: [{
  6. tableName: 'sampleTable',
  7. primaryKey: [
  8. [{ 'gid': Long.fromNumber(20013) }, { 'uid': Long.fromNumber(20013) }],
  9. [{ 'gid': Long.fromNumber(20015) }, { 'uid': Long.fromNumber(20015) }]
  10. ],
  11. startColumn: "col2",
  12. endColumn: "col4"
  13. },
  14. {
  15. tableName: 'notExistTable',
  16. primaryKey: [
  17. [{ 'gid': Long.fromNumber(10001) }, { 'uid': Long.fromNumber(10001) }]
  18. ]
  19. }
  20. ],
  21. };
  22. var maxRetryTimes = 3;
  23. var retryCount = 0;
  24. function batchGetRow(params) {
  25. client.batchGetRow(params, function (err, data) {
  26. if (err) {
  27. console.log('error:', err);
  28. return;
  29. }
  30. var isAllSuccess = true;
  31. var retryRequest = { tables: [] };
  32. for (var i = 0; i < data.tables.length; i++) {
  33. var faildRequest = { tableName: data.tables[i][0].tableName, primaryKey: [] };
  34. for (var j = 0; j < data.tables[i].length; j++) {
  35. if (!data.tables[i][j].isOk && null != data.tables[i][j].primaryKey) {
  36. isAllSuccess = false;
  37. var pks = [];
  38. for (var k in data.tables[i][j].primaryKey) {
  39. var name = data.tables[i][j].primaryKey[k].name;
  40. var value = data.tables[i][j].primaryKey[k].value;
  41. var kp = {};
  42. kp[name] = value;
  43. pks.push(kp);
  44. }
  45. faildRequest.primaryKey.push(pks);
  46. } else {
  47. // get success data
  48. }
  49. }
  50. if (faildRequest.primaryKey.length > 0) {
  51. retryRequest.tables.push(faildRequest);
  52. }
  53. }
  54. if (!isAllSuccess && retryCount++ < maxRetryTimes) {
  55. batchGetRow(retryRequest);
  56. }
  57. console.log('success:', data);
  58. });
  59. }
  60. batchGetRow(params, maxRetryTimes);

Note:

  • BatchGetRow supports filtering using conditional statements.
  • Obtain the full sample codes at BatchGetRow@GitHub.

BatchWriteRow

Inserts, modifies, or deletes several data rows in batches in one or more tables.

The BatchWriteRow operation is essentially a set of multiple PutRow, UpdateRow, and DeleteRow operations. Each operation is executed, results are returned, and capacity units are consumed independently.

API

  1. /**
  2. * Modify data rows in batches.
  3. */
  4. batchWriteRow(params, callback)

Example

Write data rows in batches.

  1. var client = require('./client');
  2. var TableStore = require('../index.js');
  3. var Long = TableStore.Long;
  4. var params = {
  5. tables: [{
  6. tableName: 'sampleTable',
  7. rows: [{
  8. type: 'PUT',
  9. condition: new TableStore.Condition(TableStore.RowExistenceExpectation.IGNORE, null),
  10. primaryKey: [{ 'gid': Long.fromNumber(8) }, { 'uid': Long.fromNumber(80) }],
  11. attributeColumns: [{ 'attrCol1': 'test1' }, { 'attrCol2': 'test2' }],
  12. returnContent: { returnType: TableStore.ReturnType.Primarykey }
  13. }],
  14. }],
  15. };
  16. client.batchWriteRow(params, function (err, data) {
  17. if (err) {
  18. console.log('error:', err);
  19. return;
  20. }
  21. console.log('success:', data);
  22. });

Note:

  • BatchWriteRow supports filtering using conditional statements.
  • Obtain the full sample codes at BatchWriteRow@GitHub.

GetRange

Reads data within the specified primary key range.

API

  1. /**
  2. * Read data within the specified primary key range.
  3. */
  4. getRange(params, callback)

Example

Read data within the specified range.

  1. var Long = TableStore.Long;
  2. var client = require('./client');
  3. var params = {
  4. tableName: "sampleTable",
  5. direction: TableStore.Direction.FORWARD,
  6. inclusiveStartPrimaryKey: [{ "gid": TableStore.INF_MIN }, { "uid": TableStore.INF_MIN }],
  7. exclusiveEndPrimaryKey: [{ "gid": TableStore.INF_MAX }, { "uid": TableStore.INF_MAX }],
  8. limit: 50
  9. };
  10. client.getRange(params, function (err, data) {
  11. if (err) {
  12. console.log('error:', err);
  13. return;
  14. }
  15. //If the value of data.next_start_primary_key is not empty, continue to read the data
  16. if (data.next_start_primary_key) {
  17. }
  18. console.log('success:', data);
  19. });

Note:

  • GetRange supports filtering using conditional statements.
  • When performing the GetRange operation, note that the data may be paged.
  • Obtain the full sample codes at GetRange@GitHub.
Thank you! We've received your feedback.