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 can be viewed as 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. //Return multiple data rows in a table
  2. //
  3. // @param BatchGetRowRequest Encapsulate the parameters required to perform the BatchGetRow operation
  4. // @return BatchGetRowResponse Content of the response to the BatchGetRow operation
  5. BatchGetRow(request *BatchGetRowRequest) (*BatchGetRowResponse, error)

Example

Read 10 data rows in batches.

  1. batchGetReq := &tablestore.BatchGetRowRequest{}
  2. mqCriteria := &tablestore.MultiRowQueryCriteria{}
  3. for i := 0; i < 10; i++ {
  4. pkToGet := new(tablestore.PrimaryKey)
  5. pkToGet.AddPrimaryKeyColumn("pk1", "pk1value1")
  6. pkToGet.AddPrimaryKeyColumn("pk2", int64(i))
  7. pkToGet.AddPrimaryKeyColumn("pk3", []byte("pk3"))
  8. mqCriteria.AddRow(pkToGet)
  9. mqCriteria.MaxVersion = 1
  10. }
  11. mqCriteria.TableName = tableName
  12. batchGetReq.MultiRowQueryCriteria = append(batchGetReq.MultiRowQueryCriteria, mqCriteria)
  13. batchGetResponse, err := client.BatchGetRow(batchGetReq)
  14. if err != nil {
  15. fmt.Println("batachget failed with error:", err)
  16. } else {
  17. fmt.Println("batchget finished")
  18. }

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. It is essentially a set of multiple PutRow, UpdateRow, and DeleteRow operations. Each operation is executed, results are returned independently, and capacity units are consumed independently.

API

  1. // Add, delete, or modify multiple data rows in multiple tables
  2. //
  3. // @param BatchWriteRowRequest Encapsulate the parameters required to perform the BatchWriteRow operation
  4. // @return BatchWriteRowResponse Content of the response to the BatchWriteRow operation
  5. BatchWriteRow(request *BatchWriteRowRequest) (*BatchWriteRowResponse,error)

Example

Write 100 data rows in batches.

  1. batchWriteReq := &tablestore.BatchWriteRowRequest{}
  2. for i := 0; i < 100; i++ {
  3. putRowChange := new(tablestore.PutRowChange)
  4. putRowChange.TableName = tableName
  5. putPk := new(tablestore.PrimaryKey)
  6. putPk.AddPrimaryKeyColumn("pk1", "pk1value1")
  7. putPk.AddPrimaryKeyColumn("pk2", int64(i))
  8. putPk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
  9. putRowChange.PrimaryKey = putPk
  10. putRowChange.AddColumn("col1", "fixvalue")
  11. putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
  12. batchWriteReq.AddRowChange(putRowChange)
  13. }
  14. response, err := client.BatchWriteRow(batchWriteReq)
  15. if err != nil {
  16. fmt.Println("batch request failed with:", response)
  17. } else {
  18. fmt.Println("batch write row finished")
  19. }

Note:

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

GetRange

This API reads data within the specified primary key range.

API

  1. // Read multiple data rows within the specified range in a table.
  2. //
  3. // @param GetRangeRequest Encapsulate the parameters required to perform the GetRange operation
  4. // @return GetRangeResponse Content of the response to the GetRange operation
  5. GetRange(request *GetRangeRequest) (*GetRangeResponse,error)

Example

Read data within the specified range.

  1. getRangeRequest := &tablestore.GetRangeRequest{}
  2. rangeRowQueryCriteria := &tablestore.RangeRowQueryCriteria{}
  3. rangeRowQueryCriteria.TableName = tableName
  4. startPK := new(tablestore.PrimaryKey)
  5. startPK.AddPrimaryKeyColumnWithMinValue("pk1")
  6. startPK.AddPrimaryKeyColumnWithMinValue("pk2")
  7. startPK.AddPrimaryKeyColumnWithMinValue("pk3")
  8. endPK := new(tablestore.PrimaryKey)
  9. endPK.AddPrimaryKeyColumnWithMaxValue("pk1")
  10. endPK.AddPrimaryKeyColumnWithMaxValue("pk2")
  11. endPK.AddPrimaryKeyColumnWithMaxValue("pk3")
  12. rangeRowQueryCriteria.StartPrimaryKey = startPK
  13. rangeRowQueryCriteria.EndPrimaryKey = endPK
  14. rangeRowQueryCriteria.Direction = tablestore.FORWARD
  15. rangeRowQueryCriteria.MaxVersion = 1
  16. rangeRowQueryCriteria.Limit = 10
  17. getRangeRequest.RangeRowQueryCriteria = rangeRowQueryCriteria
  18. getRangeResp, err := client.GetRange(getRangeRequest)
  19. fmt.Println("get range result is " ,getRangeResp)
  20. for ; ; {
  21. if err != nil {
  22. fmt.Println("get range failed with error:", err)
  23. }
  24. if (len(getRangeResp.Rows) > 0) {
  25. for _, row := range getRangeResp.Rows {
  26. fmt.Println("range get row with key", row.PrimaryKey.PrimaryKeys[0].Value, row.PrimaryKey.PrimaryKeys[1].Value, row.PrimaryKey.PrimaryKeys[2].Value)
  27. }
  28. if getRangeResp.NextStartPrimaryKey == nil {
  29. break
  30. } else {
  31. fmt.Println("next pk is :", getRangeResp.NextStartPrimaryKey.PrimaryKeys[0].Value, getRangeResp.NextStartPrimaryKey.PrimaryKeys[1].Value, getRangeResp.NextStartPrimaryKey.PrimaryKeys[2].Value)
  32. getRangeRequest.RangeRowQueryCriteria.StartPrimaryKey = getRangeResp.NextStartPrimaryKey
  33. getRangeResp, err = client.GetRange(getRangeRequest)
  34. }
  35. } else {
  36. break
  37. }
  38. fmt.Println("continue to query rows")
  39. }
  40. fmt.Println("putrow finished")

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.