【TcaplusDB知识库】TDR表GOSDK示例代码-查询数据
使用工具TdrCodeGen将service_info.xml转为go代码
func getExample() {
req, err := client.NewRequest(ZoneId, TableName, cmd.TcaplusApiGetReq)
if err != nil {
fmt.Printf("getExample NewRequest TcaplusApiGetReq failed %v\n", err.Error())
return
}
fmt.Printf("getExample NewRequest TcaplusApiGetReq finish\n")
//为request添加一条记录,(index只有在list表中支持,generic不校验)
rec, err := req.AddRecord(0)
if err != nil {
fmt.Printf("getExample AddRecord failed %v\n", err.Error())
return
}
fmt.Printf("getExample AddRecord finish\n")
//申请tdr结构体并赋值Key,最好调用tdr pkg的NewXXX函数,会将成员初始化为tdr定义的tdr默认值,
// 不要自己new,自己new,某些结构体未初始化,存在panic的风险
data := service_info.NewService_Info()
data.Gameid = "dev"
data.Envdata = "oa"
data.Name = "com"
//将tdr的数据设置到请求的记录中
if err := rec.SetData(data); err != nil {
fmt.Printf("SetData failed %v\n", err.Error())
return
}
fmt.Printf("getExample SetData finish\n")
if resp, err := client.Do(req, time.Duration(2*time.Second)); err != nil {
fmt.Printf("recv err %s\n", err.Error())
return
} else {
tcapluserr := resp.GetResult()
if tcapluserr != 0 {
fmt.Printf("response ret errCode: %d, errMsg: %s", tcapluserr, terror.GetErrMsg(tcapluserr))
return
}
//获取同步请求Seq
fmt.Printf("request Seq %d\n", req.GetSeq())
//获取回应消息的序列号
fmt.Printf("respond seq: %d \n", resp.GetSeq())
fmt.Printf("getExample response success record count %d\n", resp.GetRecordCount())
for i := 0; i < resp.GetRecordCount(); i++ {
record, err := resp.FetchRecord()
if err != nil {
fmt.Printf("FetchRecord failed %s\n", err.Error())
return
}
//通过GetData获取记录
data := service_info.NewService_Info()
if err := record.GetData(data); err != nil {
fmt.Printf("record.GetData failed %s\n", err.Error())
return
}
fmt.Printf("getExample response record data %+v, route: %s\n",
data, string(data.Routeinfo[0:data.Routeinfo_Len]))
}
}
fmt.Printf("getExample send finish")
}
TcaplusDB是腾讯出品的分布式NoSQL数据库,存储和调度的代码完全自研。具备缓存+落地融合架构、PB级存储、毫秒级时延、无损水平扩展和复杂数据结构等特性。同时具备丰富的生态、便捷的迁移、极低的运维成本和五个九高可用等特点。客户覆盖游戏、互联网、政务、金融、制造和物联网等领域。