Go SDK 可根据完整主键读取宽表模型数据表中的一行数据。
前提条件
安装Tablestore Go SDK并初始化客户端。
功能说明
调用 GetRow 根据完整主键读取一行数据。可以限定返回的属性列、数据版本和版本时间范围,也可以使用过滤器筛选属性列。
func (tableStoreClient *TableStoreClient) GetRow(request *GetRowRequest) (*GetRowResponse, error)
以下示例读取 example_table 中主键值为 row1 的一行数据。
primaryKey := &tablestore.PrimaryKey{}
primaryKey.AddPrimaryKeyColumn("id", "row1")
request := &tablestore.GetRowRequest{
SingleRowQueryCriteria: &tablestore.SingleRowQueryCriteria{
TableName: "example_table",
PrimaryKey: primaryKey,
MaxVersion: 1,
},
}
response, err := client.GetRow(request)
if err != nil {
log.Fatal(err)
}
fmt.Println(response.PrimaryKey)
fmt.Println(response.Columns)
参数说明
GetRowRequest 包含以下参数。
|
名称 |
类型 |
说明 |
|
SingleRowQueryCriteria(必选) |
|
单行读取条件。 |
单行读取条件
SingleRowQueryCriteria 包含以下参数。
|
名称 |
类型 |
说明 |
|
TableName(必选) |
|
数据表名称。 |
|
PrimaryKey(必选) |
|
完整行主键。读取包含自增主键列的行时,也必须提供自增列的实际值。 |
|
MaxVersion(可选) |
|
最多返回的版本数。 |
|
TimeRange(可选) |
|
版本时间范围,包括起始时间、结束时间或指定版本,单位为毫秒。 |
|
ColumnsToGet(可选) |
|
要返回的属性列名称。未设置时返回所有属性列。同时设置 |
|
Filter(可选) |
|
属性列过滤器。更多信息,请参见过滤器。 |
|
StartColumn(可选) |
|
属性列名称范围的起始列,结果包含该列。 |
|
EndColumn(可选) |
|
属性列名称范围的结束列,结果不包含该列。 |
|
TransactionId(可选) |
|
局部事务 ID。仅在局部事务内读取数据时设置。更多信息,请参见局部事务。 |
返回值
GetRowResponse 包含以下业务信息。如果指定行不存在,响应不包含主键和属性列。
|
字段 |
类型 |
说明 |
|
|
|
行主键。 |
|
|
|
返回的属性列及其值和版本号。 |
场景示例
读取指定时间范围内的数据版本
以下示例读取当前时间往前一天内的数据版本。
now := time.Now().UnixMilli()
request.SingleRowQueryCriteria.MaxVersion = 0
request.SingleRowQueryCriteria.TimeRange = &tablestore.TimeRange{
Start: now - 24*60*60*1000,
End: now,
}