[tcaplus DB knowledge base] Tcaplus Go SDK manual (I)
1 preparation for use
This API is the Go package of Tcaplus API and supports the addition, deletion, modification and query of Generic table
1.1 list of codes
- pack is a packaged script
- Example is an example
- aurotest is a testing tool
- Other directories are the source code of Tcaplus Go API
- The vendor is the code that depends on the library. You need to use git submodule init and git submodule update to pull
1.2 compiling example
example/generic_table shows the service_ insert get replace update delete operation of info table
- Go environment installation https://golang.org/
- Add service_info.xml join Tcaplus
- Modify main AppId ZoneId DirUrl Signature at the beginning of go is the corresponding Tcaplus configuration information
- Execute after make
1.3 packaging script
pack/pack.sh shows the packaging of source code and dependent libraries, which is convenient for users to migrate to scenarios where go mod cannot be used
- cd pack && sh pack.sh
2 use of API
2.1 vendor usage
The dependent libraries and source code of Tcaplus API are in the src/vendor directory after packaging. Users only need to put the vendor into their own project directory to use the interface of Tcaplus Go API
Introduction to vendor dependency:
- github. COM / tencentyun / tcaplusdb go SDK / TDR is the source code of Tcaplus Go API
- github.com/tencentyun/tsf4g/TdrCodeGen is a tdr tool that can convert tdr xml into go source code
- github.com/tencentyun/tsf4g/tdrcom is a dependency library for unpacking tdr go source code
- go.uber.org/zap is the logstore
- github.com/natefinch/lumberjack is the log file cutting library
2.2 mod mode
The mod mode needs to be used in an intranet and public network environment
- Establish go in the project mod
- Enable module mode
- Execute the command go clean --modcache
- Execute the command go mod Edit - require = "GitHub. COM / tencentyun / tcaplusdb go SDK"/ tdr@v0.1.0 "
3 interface usage steps
There are two sets of interfaces for the operation of record s in the table:
- One set uses the SetKey SetValue interface to assign value to record, and the user specifies the contents of the key field and value field. The response message can only be read through the GetKey and GetValue interfaces
- The other set uses the SetData interface to assign value to record, the user assigns value to Tdr structure, SetData assigns value to record through reflection, and the response message can only be read through the GetData interface
3.1 SetKey and SetValue usage
1 via tcaplus Newclient() creates a tcaplus client pointer
client := tcaplus.NewClient()
2. Specify AppId, ZoneIdList, DirUrl, Signature, timeout (seconds) of the operation table to connect to tcaplus
err := client.Dial(2, []uint32{3,4}, "tcp://x.x.x.x:9999", "xxxx",60) if err != nil { log.ERR("dial failed %s", err.Error()) return }
3 specify zoneId, table name, command word, client Newrequest creates a request
req, err := client.NewRequest(3, "service_info", cmd.TcaplusApiInsertReq) if err != nil { log.ERR("NewRequest TcaplusApiInsertReq failed %s", err.Error()) return }
4 req.AddRecord adds a record record to the request (index is the number of the record in the list table. generic does not support setting it to 0)
rec, err := req.AddRecord(0) if err != nil { log.ERR("AddRecord failed %s", err.Error()) return }
5 assign values to records through the SetKey and SetValue interfaces of record
err := rec.SetKeyInt8("keyName", int8(1)) if err != nil { log.ERR("SetKeyInt8 failed %s", err.Error()) return } err := rec.SetValueInt8("valueName", int8(1)) if err != nil { log.ERR("SetKeyInt8 failed %s", err.Error()) return }
6 client.SendRequest sends the request
if err := client.SendRequest(req); err != nil { log.ERR("SendRequest failed %s", err.Error()) return }
7 client.RecvResponse is an interface for asynchronously receiving request response. The received response can be blocked in the following ways
func recvResponse(client *tcaplus.Client) (response.TcaplusResponse, error){ //5s timeout timeOutChan := time.After(5 * time.Second) for { select { case <-timeOutChan: return nil, errors.New("5s timeout") default: resp,err := client.RecvResponse() if err != nil { return nil, err } else if resp == nil { time.Sleep(time.Microsecond * 1) } else { return resp, nil } } } }
8. Operate GetResult of response to obtain the response result, getrecordcount and fetchrecord to obtain the record record in the response message, and obtain the field information of the response record through the GetKey and GetValue interfaces of record
tcapluserr := resp.GetResult() if tcapluserr != 0 { fmt.Printf("response ret errCode: %d, errMsg: %s", tcapluserr, terror.GetErrMsg(tcapluserr)) return } for i := 0; i < resp.GetRecordCount(); i++ { record, err := resp.FetchRecord() if err != nil { log.ERR("FetchRecord failed %s", err.Error()) return } keyNameData, err := rec.GetKeyInt8("keyName") if err != nil { log.ERR("GetKeyInt8 failed %s", err.Error()) return } valueNameData, err := rec.GetValueInt8("valueName") if err != nil { log.ERR("GetValueInt8 failed %s", err.Error()) return } }
3.2 use of TDR SetData mode
1. Convert the tdr table xml to GO source code
cd vendor/github.com/tencentyun/tsf4g/TdrCodeGen/ python tdr.py table.xml Get the corresponding table go Source directory table/table.go take table Put it in your own go The project directory can be used
2 through tcaplus Newclient() creates a tcaplus client pointer
client := tcaplus.NewClient()
3. Specify AppId, ZoneIdList, DirUrl, Signature, timeout (seconds) of the operation table to connect to tcaplus
err := client.Dial(2, []uint32{3,4}, "tcp://x.x.x.x:9999", "xxxx",60) if err != nil { log.ERR("dial failed %s", err.Error()) return }
4 specify zoneId, table name, command word, client Newrequest creates a request
req, err := client.NewRequest(3, "service_info", cmd.TcaplusApiInsertReq) if err != nil { log.ERR("NewRequest TcaplusApiInsertReq failed %s", err.Error()) return }
5 req.AddRecord adds a record record to the request (index is the number of the record in the list table. generic does not support setting it to 0)
rec, err := req.AddRecord(0) if err != nil { log.ERR("AddRecord failed %s", err.Error()) return }
6 create a structure and assign values through the New interface of tdr's go source code in the first step, and assign values to record through the SetData interface
data := service_info.NewService_Info() data.Gameid = "dev" data.Envdata = "oa" data.Name = "com" data.Filterdata = time.Now().Format("2006-01-02T15:04:05.000000Z") data.Updatetime = uint64(time.Now().UnixNano()) data.Inst_Max_Num = 2 data.Inst_Min_Num = 3 //The array type is slice, and the exact assignment length is required, which is consistent with refer ence data.Routeinfo_Len = uint32(len(route)) data.Routeinfo = []byte("test") //Set the data of tdr to the requested record if err := rec.SetData(data); err != nil { log.ERR("SetData failed %v", err.Error()) return }
7 client.SendRequest sends the request
if err := client.SendRequest(req); err != nil { log.ERR("SendRequest failed %s", err.Error()) return }
8 client.RecvResponse is an interface for asynchronously receiving request response. The received response can be blocked in the following ways
func recvResponse(client *tcaplus.Client) (response.TcaplusResponse, error){ //5s timeout timeOutChan := time.After(5 * time.Second) for { select { case <-timeOutChan: return nil, errors.New("5s timeout") default: resp,err := client.RecvResponse() if err != nil { return nil, err } else if resp == nil { time.Sleep(time.Microsecond * 1) } else { return resp, nil } } } }
9. Operate GetResult of response to obtain the response result, getrecordcount and fetchrecord to obtain the record in the response message, and obtain the response record through the GetData interface of record
tcapluserr := resp.GetResult() if tcapluserr != 0 { fmt.Printf("response ret errCode: %d, errMsg: %s", tcapluserr, terror.GetErrMsg(tcapluserr)) return } for i := 0; i < resp.GetRecordCount(); i++ { record, err := resp.FetchRecord() if err != nil { log.ERR("FetchRecord failed %s", err.Error()) return } //Get response record through GetData data := service_info.NewService_Info() if err := record.GetData(data); err != nil { log.ERR("record.GetData failed %s", err.Error()) return } }
Tcallusdb is a distributed NoSQL database produced by Tencent. The storage and scheduling code is completely self-developed. It has the characteristics of cache + floor fusion architecture, PB level storage, millisecond delay, lossless horizontal expansion and complex data structure. At the same time, it has the characteristics of rich ecology, convenient migration, extremely low operation and maintenance cost and five nine high availability. Customers cover games, Internet, government affairs, finance, manufacturing, Internet of things and other fields.