edit-icon download-icon

Single-row operations

Last Updated: Mar 20, 2018

Table Store offers interfaces for single-row operations, such as PutRow, GetRow, UpdateRow, and DeleteRow.

Note: The following operation examples are based on synchronous interfaces. See Asynchronous interfaces for more information on how to operate asynchronous interfaces.

Put in a row of data (PutRow)

The PutRow interface is used to insert a row of data. If the row does not exist, this operation inserts a row. If the row exists, this operation overwrites it. (This means that all columns and column values of all versions of the original row are deleted).

Example

  1. PutRowRequest req;
  2. {
  3. RowPutChange& chg = req.mutableRowChange();
  4. chg.mutableTable() = "YourTable";
  5. {
  6. // set primary key of the row to put
  7. PrimaryKey& pkey = chg.mutablePrimaryKey();
  8. pkey.append() = PrimaryKeyColumn(
  9. "pkey",
  10. PrimaryKeyValue::toStr("pkey-value"));
  11. }
  12. {
  13. // set attributes of the row to put
  14. IVector<Attribute>& attrs = chg.mutableAttributes();
  15. attrs.append() = Attribute(
  16. "attr",
  17. AttributeValue::toInteger(123));
  18. }
  19. }
  20. PutRowResponse resp;
  21. Optional<OTSError> res = client.putRow(resp, req);

Get the data of a row (GetRow)

The GetRow interface is used to read the data of a single row.

Specify the table name and the primary key of a row. The two possible reading results are as follows:

  • If this row exists, the GetRowResponse object returns each primary key column and attribute column for the row.

  • If this row does not exist, the GetRowResponse object does not include rows and it does not report errors.

Example

  1. GetRowRequest req;
  2. {
  3. PointQueryCriterion& query = req.mutableQueryCriterion();
  4. query.mutableTable() = YourTable”;
  5. {
  6. PrimaryKey& pkey = query.mutablePrimaryKey();
  7. pkey.append() = PrimaryKeyColumn(
  8. "pkey",
  9. PrimaryKeyValue::toStr("some_key")); // Assume the table has only one primary key column that is of the string type.
  10. }
  11. query.mutableMaxVersions().reset(1);
  12. }
  13. GetRowResponse resp;
  14. Optional<OTSError> res = client.getRow(resp, req);

Update the data of a row (UpdateRow)

The UpdateRow interface is used to update a row of data. If the row does not exist, a new row is written.

The update operation has the following four possible scenarios:

  • Writing a column value without specifying the version number. The Table Store service automatically offers a version number to make sure that the version number is incremented.

  • Writing a column value with a specified version number. If the column does not have the column value of this version, the data is inserted. If the column does have it, the original value is overwritten.

  • Delete the column values of the specified version.

  • Delete the column values of all versions for the entire column.

Example

  1. UpdateRowRequest req;
  2. {
  3. RowUpdateChange& chg = req.mutableRowChange();
  4. chg.mutableTable() = "YourTable";
  5. {
  6. // set primary key of the row to put
  7. PrimaryKey& pkey = chg.mutablePrimaryKey();
  8. pkey.append() = PrimaryKeyColumn(
  9. "pkey",
  10. PrimaryKeyValue::toStr("pkey"));
  11. }
  12. {
  13. // insert a value without specifying version
  14. RowUpdateChange::Update& up = chg.mutableUpdates().append();
  15. up.mutableType() = RowUpdateChange::Update::kPut;
  16. up.mutableAttrName() = "attr0";
  17. up.mutableAttrValue().reset(AttributeValue::toStr("new value without specifying version"));
  18. }
  19. {
  20. // insert a value with version
  21. RowUpdateChange::Update& up = chg.mutableUpdates().append();
  22. up.mutableType() = RowUpdateChange::Update::kPut;
  23. up.mutableAttrName() = "attr1";
  24. up.mutableAttrValue().reset(AttributeValue::toStr("new value with version"));
  25. up.mutableTimestamp().reset(UtcTime::now());
  26. }
  27. {
  28. // delete a value with specific version
  29. RowUpdateChange::Update& up = chg.mutableUpdates().append();
  30. up.mutableType() = RowUpdateChange::Update::kDelete;
  31. up.mutableAttrName() = "attr2";
  32. up.mutableTimestamp().reset(UtcTime::now());
  33. }
  34. {
  35. // delete all values of a attribute column
  36. RowUpdateChange::Update& up = chg.mutableUpdates().append();
  37. up.mutableType() = RowUpdateChange::Update::kDeleteAll;
  38. up.mutableAttrName() = "attr3";
  39. }
  40. }
  41. UpdateRowResponse resp;
  42. Optional<OTSError> res = client.updateRow(resp, req);

Delete the data of a row (DeleteRow)

The DeleteRow interface is used to delete a row. No error is reported, regardless of whether this row exists or not.

Example

  1. DeleteRowRequest req;
  2. {
  3. RowDeleteChange& chg = req.mutableRowChange();
  4. chg.mutableTable() = "YourTable";
  5. {
  6. // set primary key of the row to delete
  7. PrimaryKey& pkey = chg.mutablePrimaryKey();
  8. pkey.append() = PrimaryKeyColumn(
  9. "pkey",
  10. PrimaryKeyValue::toInteger(1));
  11. }
  12. }
  13. DeleteRowResponse resp;
  14. Optional<OTSError> res = client.deleteRow(resp, req);
Thank you! We've received your feedback.