Tablestore SDK for Go provides multi-row operations such as BatchGetRow, BatchWriteRow, GetRange, and GetByIterator.

BatchGetRow

You can call this operation to read several rows of data from one or more tables.

The BatchGetRow operation is a set of multiple GetRow operations. The execution, returning of results, and capacity unit (CU) consumption of each operation are carried out independently.

Compared with the execution of a large number of GetRow operations, the use of the BatchGetRow operation can effectively reduce the request response time and increase the data read rate.

Operation

    // Return multiple rows of data from a table.
    //
    // @param BatchGetRowRequest             Encapsulate the parameters required to perform the BatchGetRow operation.
    // @return  BatchGetRowResponse          The content of the response to the BatchGetRow operation.
    BatchGetRow(request *BatchGetRowRequest) (*BatchGetRowResponse, error)
			

Examples

The following code provides an example on how to read 10 rows of data at a time:

batchGetReq := &tablestore.BatchGetRowRequest{}
mqCriteria := &tablestore.MultiRowQueryCriteria{}

for i := 0; i < 10; i++ {
    pkToGet := new(tablestore.PrimaryKey)
    pkToGet.AddPrimaryKeyColumn("pk1", "pk1value1")
    pkToGet.AddPrimaryKeyColumn("pk2", int64(i))
    pkToGet.AddPrimaryKeyColumn("pk3", []byte("pk3"))
    mqCriteria.AddRow(pkToGet)
    mqCriteria.MaxVersion = 1
}
mqCriteria.TableName = tableName
batchGetReq.MultiRowQueryCriteria = append(batchGetReq.MultiRowQueryCriteria, mqCriteria)
batchGetResponse, err := client.BatchGetRow(batchGetReq)

if err != nil {
    fmt.Println("batachget failed with error:", err)
} else {
    fmt.Println("batchget finished")
}
			
Note
  • You can use conditional statements to filter the results returned by a BatchGetRow operation.
  • You can obtain the complete sample code at BatchGetRow@GitHub.

BatchWriteRow

You can call this operation to insert, modify, or delete several rows of data in one or more tables.

The BatchWriteRow operation is a set of multiple PutRow, UpdateRow, and DeleteRow operations. The execution, returning of results, and CU consumption of each operation are carried out independently.

Operation

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

Examples

The following code provides an example on how to write 100 rows of data:

    batchWriteReq := &tablestore.BatchWriteRowRequest{}
    for i := 0; i < 100; i++ {
        putRowChange := new(tablestore.PutRowChange)
        putRowChange.TableName = tableName
        putPk := new(tablestore.PrimaryKey)
        putPk.AddPrimaryKeyColumn("pk1", "pk1value1")
        putPk.AddPrimaryKeyColumn("pk2", int64(i))
        putPk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
        putRowChange.PrimaryKey = putPk
        putRowChange.AddColumn("col1", "fixvalue")
        putRowChange.SetCondition(tablestore.RowExistenceExpectation_IGNORE)
        batchWriteReq.AddRowChange(putRowChange)
    }

    response, err := client.BatchWriteRow(batchWriteReq)
    if err != nil {
        fmt.Println("batch request failed with:", response)
    } else {
        fmt.Println("batch write row finished")
    }
			
Note
  • You can use conditional statements to filter the results returned by a BatchWriteRow operation.
  • You can obtain the complete sample code at BatchWriteRow@GitHub.

GetRange

You can call this operation to read data within the specified primary key range.

Operation
    // Read multiple rows of data within the specified range in a table.
    //
    // @param GetRangeRequest            Encapsulate the parameters required to perform the GetRange operation.
    // @return GetRangeResponse          The content of the response to the GetRange operation.
    GetRange(request *GetRangeRequest) (*GetRangeResponse,error)
			
Examples

The following code provides an example on how to read data within the specified range:

getRangeRequest := &tablestore.GetRangeRequest{}
    rangeRowQueryCriteria := &tablestore.RangeRowQueryCriteria{}
    rangeRowQueryCriteria.TableName = tableName

    startPK := new(tablestore.PrimaryKey)
    startPK.AddPrimaryKeyColumnWithMinValue("pk1")
    startPK.AddPrimaryKeyColumnWithMinValue("pk2")
    startPK.AddPrimaryKeyColumnWithMinValue("pk3")
    endPK := new(tablestore.PrimaryKey)
    endPK.AddPrimaryKeyColumnWithMaxValue("pk1")
    endPK.AddPrimaryKeyColumnWithMaxValue("pk2")
    endPK.AddPrimaryKeyColumnWithMaxValue("pk3")
    rangeRowQueryCriteria.StartPrimaryKey = startPK
    rangeRowQueryCriteria.EndPrimaryKey = endPK
    rangeRowQueryCriteria.Direction = tablestore.FORWARD
    rangeRowQueryCriteria.MaxVersion = 1
    rangeRowQueryCriteria.Limit = 10
    getRangeRequest.RangeRowQueryCriteria = rangeRowQueryCriteria

    getRangeResp, err := client.GetRange(getRangeRequest)

    fmt.Println("get range result is " ,getRangeResp)

    for {
        if err != nil {
            fmt.Println("get range failed with error:", err)
        }
        for _, row := range getRangeResp.Rows {
            fmt.Println("range get row with key", row.PrimaryKey.PrimaryKeys[0].Value, row.PrimaryKey.PrimaryKeys[1].Value, row.PrimaryKey.PrimaryKeys[2].Value)
        }
        if getRangeResp.NextStartPrimaryKey == nil {
            break
        } else {
            fmt.Println("next pk is :", getRangeResp.NextStartPrimaryKey.PrimaryKeys[0].Value, getRangeResp.NextStartPrimaryKey.PrimaryKeys[1].Value, getRangeResp.NextStartPrimaryKey.PrimaryKeys[2].Value)
            getRangeRequest.RangeRowQueryCriteria.StartPrimaryKey = getRangeResp.NextStartPrimaryKey
            getRangeResp, err = client.GetRange(getRangeRequest)
        }
        fmt.Println("continue to query rows")
    }
    fmt.Println("putrow finished")
Note
  • You can use conditional statements to filter the read data returned from the specified range.
  • Note that the data may be paged when you perform the GetRange operation.
  • You can obtain the complete sample code at GetRange@GitHub.