Tablestore provides multiple operations for you to read data. You can call the GetRow operation to read a single row of data, the BatchGetRow operation to read multiple rows of data at a time, and the GetRange operation to read data whose primary key values are in the specified range.
Query methods
Tablestore provides the GetRow, BatchGetRow, and GetRange operations to allow you to read data. Before you read data, select the appropriate query method based on the actual query scenario.
If you want to read data from a table that contains an auto-increment primary key column, make sure that you have queried the values of all primary key columns that include the values of the auto-increment primary key column. For more information, see Configure an auto-increment primary key column. If no value is recorded for the auto-increment primary key column, you can call the GetRange operation to specify the range within which data is read based on primary key values from the first primary key column.
Query method | Description | Scenario |
You can call the GetRow operation to read a single row of data. | This method is applicable to scenarios in which all primary key columns of a table can be determined and the number of rows to be read is small. | |
You can call the BatchGetRow operation to read multiple rows of data from one or more tables at a time. The BatchGetRow operation consists of multiple GetRow operations. The process of constructing a suboperation is the same as the process of calling the GetRow operation. | This method is applicable to scenarios in which all primary key columns of a table can be determined and the number of rows to be read is large or data is to be read from multiple tables. | |
Read data whose primary key values are within a specific range | You can call the GetRange operation to read data whose primary key values are in the specified range. The GetRange operation allows you to read data whose primary key values are in the specified range in a forward or backward direction. You can also specify the number of rows to read. If the range is large and the number of scanned rows or the volume of scanned data exceeds the upper limit, the scan stops, and the rows that are read and information about the primary key of the next row are returned. You can initiate a request to start from where the last operation left off and read the remaining rows based on the information about the primary key of the next row returned by the previous operation. | This method is applicable to scenarios in which the range of all primary key columns of a table or the prefix of primary key columns can be determined. Important If you cannot determine the prefix of primary key columns, you can specify the start primary key column whose data is of the INF_MIN type and the end primary key column whose data is of the INF_MAX type to determine the range of all primary key columns of a table. This operation scans all data in the table but consumes a large amount of computing resources. Proceed with caution. |
Prerequisites
- The OTSClient instance is initialized. For more information, see Initialize an OTSClient instance.
- A data table is created. Data is written to the table.
Read a single row of data
You can call the GetRow operation to read a single row of data. After you call the GetRow operation, one of the following results may be returned:
If the row exists, the primary key columns and attribute columns of the row are returned.
If the row does not exist, no row is returned and no error is reported.
API operation
// Return a row of data in a table.
// @param GetRowRequest Encapsulate the parameters required to call the GetRow operation.
// @return GetRowResponse The content of the response to the GetRow operation.
GetRow(request *GetRowRequest) (*GetRowResponse, error)
Parameters
Parameter | Description |
TableName | The name of the table. |
PrimaryKey | The primary key information of the row. The primary key information consists of the primary key column name, primary key type, and primary key value. Important The number and types of primary key columns that you specify must be the same as the actual number and types of primary key columns in the table. |
ColumnsToGet | The columns that you want to read. You can specify the names of primary key columns or attribute columns.
Note
|
MaxVersion | The maximum number of data versions that you can read. Important You must specify at least one of the MaxVersion and TimeRange parameters.
|
TimeRange | The time range of versions or a specific version that you want to read. For more information, see TimeRange. Important You must specify at least one of the MaxVersion and TimeRange parameters.
Only one of specific_time and Valid values of the TimeRange parameter: 0 to INT64.MAX. Unit: millisecond. |
Filter | The filter that you want to use to filter the query results on the server side. Only rows that meet the filter conditions are returned. For more information, see Configure filter. Note If you specify both the ColumnsToGet and Filter parameters, Tablestore queries the columns that are specified by the ColumnsToGet parameter, and then returns the rows that meet the filter conditions. |
Sample code
The following sample code provides an example on how to read a row of data:
getRowRequest := new(tablestore.GetRowRequest)
criteria := new(tablestore.SingleRowQueryCriteria);
putPk := new(tablestore.PrimaryKey)
putPk.AddPrimaryKeyColumn("pk1", "pk1value1")
putPk.AddPrimaryKeyColumn("pk2", int64(2))
putPk.AddPrimaryKeyColumn("pk3", []byte("pk3"))
criteria.PrimaryKey = putPk
getRowRequest.SingleRowQueryCriteria = criteria
getRowRequest.SingleRowQueryCriteria.TableName = tableName
getRowRequest.SingleRowQueryCriteria.MaxVersion = 1
getResp, err := client.GetRow(getRowRequest)
if err != nil {
fmt.Println("getrow failed with error:", err)
} else {
fmt.Println("get row col0 result is ",getResp.Columns[0].ColumnName, getResp.Columns[0].Value,)
}
To view the detailed sample code, visit GetRow@GitHub.
Read multiple rows of data at a time
You can call the BatchGetRow operation to read multiple rows of data from one or more tables at a time. The BatchGetRow operation consists of multiple GetRow operations. When you call the BatchGetRow operation, the process of constructing each GetRow operation is the same as the process of constructing the GetRow operation when you call the GetRow operation.
If you call the BatchGetRow operation, each GetRow operation is separately performed, and Tablestore separately returns the response to each GetRow operation.
Usage notes
When you call the BatchGetRow operation to read multiple rows at a time, some rows may fail to be read. If this happens, Tablestore does not return exceptions, but returns BatchGetRowResponse in which the information about the failed rows are included. Therefore, when you call the BatchGetRow operation, you must check the return values to determine whether data is successfully read from each row.
The BatchGetRow operation uses the same parameter settings for all rows. For example, if the
ColumnsToGet
parameter is set to [colA], only the value of the colA column is read from all rows.You can call the BatchGetRow operation to read a maximum of 100 rows at a time.
Parameters
For more information about parameters, see the Parameters table of the "Read a single row of data" section.
API operation
// Return multiple rows of data from a table.
// @param BatchGetRowRequest Encapsulate the parameters required to call the BatchGetRow operation.
// @return BatchGetRowResponse The content of the response to the BatchGetRow operation.
BatchGetRow(request *BatchGetRowRequest) (*BatchGetRowResponse, error)
Sample code
The following sample 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("batchget failed with error:", err)
} else {
fmt.Println("batchget finished")
}
To view the detailed sample code, visit BatchGetRow@GitHub.
Read data whose primary key values are within a specific range
You can call the GetRange operation to read data whose primary key values are in the specified range.
The GetRange operation allows you to read data whose primary key values are in the specified range in a forward or backward direction. You can also specify the number of rows to read. If the range is large and the number of scanned rows or the volume of scanned data exceeds the upper limit, the scan stops, and the rows that are read and information about the primary key of the next row are returned. You can initiate a request to start from where the last operation left off and read the remaining rows based on the information about the primary key of the next row returned by the previous operation.
In Tablestore tables, all rows are sorted by the primary key. The primary key of a table sequentially consists of all primary key columns. Therefore, the rows are not sorted based on a specific primary key column.Tablestore
Usage notes
The GetRange operation follows the leftmost matching principle. Tablestore compares values in sequence from the first primary key column to the last primary key column to read data whose primary key values are in the specified range. For example, the primary key of a data table consists of the following primary key columns: PK1, PK2, and PK3. When data is read, Tablestore first determines whether the PK1 value of a row is in the range that is specified for the first primary key column. If the PK1 value of a row is in the range, Tablestore stops determining whether the values of other primary key columns of the row are in the ranges that are specified for each primary key column and returns the row. If the PK1 value of a row is not in the range, Tablestore continues to determine whether the values of other primary key columns of the row are in the ranges that are specified for each primary key column in the same manner as PK1.
If one of the following conditions is met, the GetRange operation may stop and return data:
The amount of scanned data reaches 4 MB.
The number of scanned rows reaches 5,000.
The number of returned rows reaches the upper limit.
The read throughput is insufficient to read the next row of data because all reserved read throughput is consumed.
API operation
// Query multiple rows of data whose primary key values are in the specified range in a table.
// @param GetRangeRequest Encapsulate the parameters required to call the GetRange operation.
// @return GetRangeResponse The content of the response to the GetRange operation.
GetRange(request *GetRangeRequest) (*GetRangeResponse,error)
Parameters
Parameter | Description |
TableName | The name of the table. |
Direction | The order in which you want to sort the rows in the response.
For example, a table has two primary key values A and B, and Value A is smaller than Value B. If you set the Direction parameter to FORWARD and specify a |
StartPrimaryKey | The start primary key information and end primary key information of the range that you want to read. The start primary key column and end primary key column must be valid primary key columns or virtual columns whose data is of the INF_MIN type and INF_MAX type. The number of columns in the range specified by virtual columns must be the same as the number of primary key columns of the specified table. INF_MIN indicates an infinitely small value. All values of other types are greater than a value of the INF_MIN type. INF_MAX indicates an infinitely great value. All values of other types are smaller than a value of the INF_MAX type.
The rows in the table are sorted in ascending order based on primary key values. The range that is used to read data is a left-closed, right-open interval. If data is read in the forward direction, the rows whose primary key values are greater than or equal to the start primary key value but smaller than the end primary key value are returned. |
EndPrimaryKey | |
Limit | The maximum number of rows that can be returned. The value of this parameter must be greater than 0. Tablestore stops an operation after the maximum number of rows that can be returned in the forward or backward direction is reached, even if some rows in the specified range are not returned. You can use the value of the NextStartPrimaryKey parameter returned in the response to read data in the next request. |
ColumnsToGet | The columns that you want to read. You can specify the names of primary key columns or attribute columns.
Note
|
MaxVersions | The maximum number of data versions that you can read. Important You must specify at least one of the MaxVersion and TimeRange parameters.
|
TimeRange | The time range of versions or a specific version that you want to read. For more information, see TimeRange. Important You must specify at least one of the MaxVersion and TimeRange parameters.
Only one of specific_time and Valid values of the TimeRange parameter: 0 to |
Filter | The filter that you want to use to filter the query results on the server side. Only rows that meet the filter conditions are returned. For more information, see Configure filter. Note If you specify both the ColumnsToGet and Filter parameters, Tablestore queries the columns that are specified by the ColumnsToGet parameter, and then returns the rows that meet the filter conditions. |
NextStartPrimaryKey | The start primary key information of the next read request. The value of the NextStartPrimaryKey parameter can be used to determine whether all data is read.
|
Sample code
The following sample code provides an example on how to read data whose primary key values are in 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("range get row finished")
To view the detailed sample code, visit GetRange@GitHub.