edit-icon download-icon

Single-row operations

Last Updated: Sep 08, 2017

The Table Store SDK provides the following single-row operation APIs: PutRow, GetRow, UpdateRow, and DeleteRow.

PutRow

Inserts data into the specified row.

API

  1. // @param PutRowRequest Encapsulate the parameters required to perform the PutRow operation
  2. // @return PutRowResponse
  3. PutRow(request *PutRowRequest) (*PutRowResponse, error)

Example

  1. putRowRequest := new(tablestore.PutRowRequest)
  2. putRowChange := new(tablestore.PutRowChange)
  3. putRowChange.TableName = tableName
  4. putPk := new(tablestore.PrimaryKey)
  5. putPk.AddPrimaryKeyColumn("pk1", "pk1value1")
  6. putPk.AddPrimaryKeyColumn("pk2", int64(2))
  7. putPk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
  8. putRowChange.PrimaryKey = putPk
  9. putRowChange.AddColumn("col1", "col1data1")
  10. putRowChange.AddColumn("col2", int64(3))
  11. putRowChange.AddColumn("col3", []byte("test"))
  12. putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
  13. putRowRequest.PutRowChange = putRowChange
  14. _, err := client.PutRow(putRowRequest)
  15. if err != nil {
  16. fmt.Println("putrow failed with error:", err)
  17. } else {
  18. fmt.Println("putrow finished")
  19. }
  • RowExistenceExpectation.IGNORE indicates that new data is still inserted no matter whether the specified row exists or not. 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.

Note: Obtain the full sample codes at PutRow@GitHub.

GetRow

This API reads a single data row based on a given primary key.

API

  1. //Return a row of data from a table
  2. //
  3. // @param GetRowRequest Encapsulate the parameters required to perform the GetRow operation
  4. // @return GetRowResponse Content of the response to the GetRow operation
  5. GetRow(request *GetRowRequest) (*GetRowResponse, error)

Example

Read a data row.

  1. getRowRequest := new(tablestore.GetRowRequest)
  2. criteria := new(tablestore.SingleRowQueryCriteria);
  3. putPk := new(tablestore.PrimaryKey)
  4. putPk.AddPrimaryKeyColumn("pk1", "pk1value1")
  5. putPk.AddPrimaryKeyColumn("pk2", int64(2))
  6. putPk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
  7. criteria.PrimaryKey = putPk
  8. getRowRequest.SingleRowQueryCriteria = criteria
  9. getRowRequest.SingleRowQueryCriteria.TableName = tableName
  10. getRowRequest.SingleRowQueryCriteria.MaxVersion = 1
  11. getResp, err := client.GetRow(getRowRequest)
  12. if err != nil {
  13. fmt.Println("getrow failed with error:", err)
  14. } else {
  15. fmt.Println("get row col0 result is ",getResp.Columns[0].ColumnName, getResp.Columns[0].Value,)
  16. }

Note: Obtain the full sample codes at 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. // Update a data row in a table
  2. // @param UpdateRowRequest Encapsulate the parameters required to perform the UpdateRow operation
  3. // @return UpdateRowResponse Content of the response to the UpdateRow operation
  4. UpdateRow(request *UpdateRowRequest) (*UpdateRowResponse, error)

Example

Update a data row.

  1. updateRowRequest := new(tablestore.UpdateRowRequest)
  2. updateRowChange := new(tablestore.UpdateRowChange)
  3. updateRowChange.TableName = tableName
  4. updatePk := new(tablestore.PrimaryKey)
  5. updatePk.AddPrimaryKeyColumn("pk1", "pk1value1")
  6. updatePk.AddPrimaryKeyColumn("pk2", int64(2))
  7. updatePk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
  8. updateRowChange.PrimaryKey = updatePk
  9. updateRowChange.DeleteColumn("col1")
  10. updateRowChange.PutColumn("col2", int64(77))
  11. updateRowChange.PutColumn("col4", "newcol3")
  12. updateRowChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
  13. updateRowRequest.UpdateRowChange = updateRowChange
  14. _, err := client.UpdateRow(updateRowRequest)
  15. if err != nil {
  16. fmt.Println("update failed with error:", err)
  17. } else {
  18. fmt.Println("update row finished")
  19. }

Note: Obtain the full sample codes at UpdateRow@GitHub.

DeleteRow

API

  1. // Delete a data row from a table
  2. // @param DeleteRowRequest Encapsulate the parameters required to perform the DeleteRow operation
  3. // @return DeleteRowResponse Content of the response to the DeleteRow operation
  4. DeleteRow(request *DeleteRowRequest) (*DeleteRowResponse, error)

Example

This API deletes a data row.

  1. deleteRowReq := new(tablestore.DeleteRowRequest)
  2. deleteRowReq.DeleteRowChange = new(tablestore.DeleteRowChange)
  3. deleteRowReq.DeleteRowChange.TableName = tableName
  4. deletePk := new(tablestore.PrimaryKey)
  5. deletePk.AddPrimaryKeyColumn("pk1", "pk1value1")
  6. deletePk.AddPrimaryKeyColumn("pk2", int64(2))
  7. deletePk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
  8. deleteRowReq.DeleteRowChange.PrimaryKey = deletePk
  9. deleteRowReq.DeleteRowChange.SetCondition(tablestore.RowExistenceExpectation_EXPECT_EXIST)
  10. clCondition1 := tablestore.NewSingleColumnCondition("col2", tablestore.CT_EQUAL, int64(3))
  11. deleteRowReq.DeleteRowChange.SetColumnCondition(clCondition1)
  12. _, err := client.DeleteRow(deleteRowReq)
  13. if err != nil {
  14. fmt.Println("delete failed with error:", err)
  15. } else {
  16. fmt.Println("delete row finished")
  17. }

Note: Obtain the full sample codes at DeleteRow@GitHub.

Thank you! We've received your feedback.