Single-row operations

Last Updated: May 19, 2017

The Table Store SDK provides the following single-row operation interfaces:

  • PutRow
  • GetRow
  • UpdateRow
  • DeleteRow

PutRow

Inserts data into the specified row.

API

  1. """
  2. Description: This operation writes a single row of data. CapacityUnit consumed by the operation is returned.
  3. ``table_name`` is the name of the table.
  4. ``condition`` indicates the conditions to check before executing the operation. If the conditions are met, the operation is executed. This is an instance of the ots2.metadata.Condition class.
  5. At present, this function only supports row existence checks. Check conditions include: 'IGNORE', 'EXPECT_EXIST', and 'EXPECT_NOT_EXIST'.
  6. ``primary_key`` indicates the primary key; type: dict.
  7. ``attribute_columns`` indicates the attribute columns; type: dict.
  8. Return: The CapacityUnits consumed by this operation.
  9. ``consumed`` indicates the CapacityUnits consumed. This is an instance of the ots2.metadata.CapacityUnit class.
  10. """
  11. def put_row(self, table_name, condition, primary_key, attribute_columns):

Example

Insert a data row.

  1. # The primary key column values of the data to be inserted: pk0(1) and pk1(101)
  2. # The attribute columns: col0(100),col1(200),col2('hangzhou')
  3. primary_key = {'pk0':1, 'pk1':101}
  4. attribute_columns = {'col0':100, 'col1':200, 'col2':'hangzhou'}
  5. # The insert conditions; the row is only inserted if no current row exists
  6. condition = Condition('EXPECT_NOT_EXIST')
  7. try:
  8. ots_client.put_row('SampleTable', condition, primary_key, attribute_columns)
  9. # If no exception is thrown, the operation is successful
  10. print "put row succeeded"
  11. except Exception:
  12. # If an exception is thrown, the operation fails
  13. print "put row failed."
  • RowExistenceExpectation.IGNORE indicates that new data is still inserted even when the specified row does not exist. If the inserted data is the same as the existing data, the existing data is overwritten.

  • RowExistenceExpectation.EXPECT_EXIST indicates that new data is inserted only when the specified row exists. The existing data is overwritten.

  • RowExistenceExpectation.EXPECT_NOT_EXIST indicates that data is inserted only when the specified row does not exist.

  • For code details, refer to PutRow@GitHub.

GetRow

Reads a single data row based on a given primary key.

API

  1. """
  2. Description: This operation reads a single row of data.
  3. ``table_name`` is the name of the table.
  4. ``primary_key`` indicates the primary key; type: dict.
  5. ``columns_to_get`` is an optional parameter that indicates the names of the columns to read in list format. If not entered, all columns will be read.
  6. Return: The CapacityUnits consumed by this operation, as well as the primary key columns and attribute columns.
  7. ``consumed`` indicates the CapacityUnits consumed. This is an instance of the ots2.metadata.CapacityUnit class.
  8. ``primary_key_columns`` indicates the primary key columns; type: dict. For example: {'pk0':value0, 'pk1':value1}.
  9. ``attribute_columns`` indicates the attribute columns; type: dict. For example: {'col0':value0, 'col1':value1}.
  10. """

Example

Read a data row.

  1. # Construct the primary key column for the query row
  2. primary_key = {'pk0':1, 'pk1':101}
  3. # Only query columns col0 and col1
  4. columns_to_get = ['col0', 'col1']
  5. # Call the get_row API to query data
  6. try:
  7. consumed, primary_key_columns, attribute_columns = ots_client.get_row('SampleTable', primary_key, columns_to_get)
  8. # If no error is thrown, execution is successful and data are printed
  9. print 'get row succeeded'
  10. print 'col0:%s' % attribute_columns.get('col0')
  11. print 'col1:%s' % attribute_columns.get('col1')
  12. except Exception:
  13. # If an exception is thrown, the operation fails
  14. print "get row failed."
  • If you query a data row, the system returns the data in all columns of the row. You can use the columns_to_get parameter to read the data in specified columns. For example, the system only returns the data in col0 and col1 if col0 and col1 are inserted into columns_to_get.

  • Code details: GetRow@GitHub.

UpdateRow

Updates 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.

API

  1. """
  2. Description: This operation updates a single row of data.
  3. ``table_name`` is the name of the table.
  4. ``condition`` indicates the conditions to check before executing the operation. If the conditions are met, the operation is executed. This is an instance of the ots2.metadata.Condition class.
  5. At present, this function only supports row existence checks. Check conditions include: 'IGNORE', 'EXPECT_EXIST', and 'EXPECT_NOT_EXIST'.
  6. ``primary_key`` indicates the primary key; type: dict.
  7. ``update_of_attribute_columns`` indicates the attribute columns; type: dict. This can include put and delete operations. Here, put is of dict type and indicates the attribute columns to write.
  8. Delete is of list type and indicates the names of the attribute columns to delete. This is shown in the example below.
  9. Return: The CapacityUnits consumed by this operation.
  10. ``consumed`` indicates the CapacityUnits consumed. This is an instance of the ots2.metadata.CapacityUnit class.
  11. """
  12. def update_row(self, table_name, condition, primary_key, update_of_attribute_columns):

Example

Update the data of the specified row.

  1. # Construct the primary key column, with unique values for each row of data
  2. primary_key = {'pk0':1, 'pk1':101}
  3. # Update the values of columns col0 and col2, and delete column col1
  4. update_of_attribute_columns = {'put' : {'col0':100, 'col2':'chengdu'}, 'delete' : ['col1'],
  5. }
  6. # The update operation is executed only if the row exists
  7. condition = Condition('EXPECT_EXIST')
  8. try:
  9. # Call the update_row API to update a row
  10. ots_client.update_row('myTable', condition, primary_key, update_of_attribute_columns)
  11. # If no exception is thrown, execution is successful
  12. print "update row succeeded"
  13. except Exception:
  14. # When execution fails, an exception will be thrown
  15. print "update row failed"

For code details, refer to UpdateRow@GitHub.

DeleteRow

API

  1. """
  2. Description: This operation deletes a single row of data.
  3. ``table_name`` is the name of the table.
  4. ``condition`` indicates the conditions to check before executing the operation. If the conditions are met, the operation is executed. This is an instance of the ots2.metadata.Condition class.
  5. At present, this function only supports row existence checks. Check conditions include: 'IGNORE', 'EXPECT_EXIST', and 'EXPECT_NOT_EXIST'.
  6. ``primary_key`` indicates the primary key; type: dict.
  7. Return: The CapacityUnits consumed by this operation.
  8. ``consumed`` indicates the CapacityUnits consumed. This is an instance of the ots2.metadata.CapacityUnit class.
  9. """
  10. def delete_row(self, table_name, condition, primary_key):

Example

Delete a data row.

  1. # Construct the primary key column values of the row to delete
  2. primary_key = {'pk0':1, 'pk1':101}
  3. # The delete operation is executed whether or not the row exists
  4. condition = Condition('IGNORE')
  5. try:
  6. ots_client.delete_row('SampleTable', condition, primary_key)
  7. # If no exception is thrown, the operation is successful
  8. print "delete row succeeded"
  9. except Exception:
  10. # If an exception is thrown, the operation fails
  11. print "delete row failed"

For code details, refer to DeleteRow@GitHub.

Thank you! We've received your feedback.