[PB] Traverse2.0
Supported version: 3.50.0
Brief Introduction
The interface is used to traverse the full table data. It supports the Limit parameter to control the number of records returned for each traversal. At the same time, the returned response package contains the TraveseSession field and Complete field. The traversal conditions are as follows:
- Limit: control the number of records returned per traversal
- TraveseSession: string, initial value: 0.0: used to determine the traversal position for each traversal, similar to the offset function. The Session value returned this time is transferred to the Data parameter of the next traversal
- Complete: bool. Value range: False or True, used to determine whether the record traversal is completed
POST http://{Tcaplus_REST_URL}
Request Syntax
Http Request
#Tencent Cloud Console RESTful Endpoint, ip: 80, port: 80 by default
http://172.17.0.22
#TcaplusDB Local docker RESTful Endpoint, ip: 31001, port: 31001 by default
http://9.135.8.93:31001
Http Head
| Name | Required or not | Limitations | Description |
|---|---|---|---|
| x-tcaplus-target | Yes | None | Tcaplus.Traverse |
| x-tcaplus-version | Yes | None | Tcaplus3.50.0 |
| x-tcaplus-app-id | Yes | None | Correspond to the app id number(aka, cluster access id) |
| x-tcaplus-zone-id | Yes | None | Correspond to the zone number (aka, table group id) |
| x-tcaplus-protocol-version | Yes | None | Correspond to the protocol version number, default 2.0 |
| x-tcaplus-table-name | Yes | None | Correspond to the table name |
| x-tcaplus-pwd-md5 | Yes | None | App password (aka, cluster access password), that is, the md5 value after the calculation |
| x-tcaplus-idl-type | Yes | None | protobuf |
| x-tcaplus-result-flag | No | None | 0: No data will be returned after successful operation; 1: After the operation is successful, the data that is consistent with the request will be returned; 2: After the operation is successful, the data after this update operation will be returned; 3: After the operation is successful, the data before the tcapsvr operation will be returned |
| x-tcaplus-data-version-check | No | 1: Detect the record version number. Only when the version number is the same as the server version number, the version number will automatically increase; 2: Do not detect the record version number, but force the client record version number to be written to the server; 3: Do not detect the record version number, and automatically increase the server version number | |
| x-tcaplus-data-version | No | Version number |
Example:
x-tcaplus-target:Tcaplus.Traverse
x-tcaplus-app-id:3
x-tcaplus-zone-id:1
x-tcaplus-protocol-version:2.0
x-tcaplus-table-name:game_players
x-tcaplus-pwd-md5:4e81984efccfb4982333aeb1ff7968d5
x-tcaplus-result-flag:2
x-tcaplus-version:Tcaplus3.50.0
x-tcaplus-data-version-check: 3
x-tcaplus-idl-type:protobuf
Data
Use json format to represent the record related information. Parameter description:
- Limit: Required. It limits the number of data returned per traversal
- ReturnValues: Optional. It is a user cache used for the transfer of some global objects to avoid memory usage management
- TraveseSession: Required. It is used to judge the state of the traversal cycle Session, similar to the offset function. It is in the TraveseSession of the response package
Example:
{
"Limit": 1000,
"TraverseSession": "0.0",
"ReturnValues": "user buffers, to build a bridge between request and response, use case: global object that do not want to save in memory"
}
Complete Request Example
One-time Traversal Example
Limit is set to a large value to return all records at one time. It is suitable for scenarios with small table records
curl -i -XPOST -H 'x-tcaplus-target: Tcaplus.Traverse' -H 'x-tcaplus-app-id: 70' -H 'x-tcaplus-zone-id: 1' -H 'x-tcaplus-protocol-version: 2.0' -H 'x-tcaplus-table-name: game_players' -H 'x-tcaplus-pwd-md5: 0972ad76decf4d11a69e2e0d9af335da' -H 'x-tcaplus-result-flag: 2' -H 'x-tcaplus-version: Tcaplus3.50.0' -H 'x-tcaplus-data-version-check: 1' -H 'x-tcaplus-idl-type: protobuf' http://172.17.32.17 -d '{
"Limit": 1000,
"TraveseSession":"0.0",
"ReturnValues": "user buffers, to build a bridge between request and response, use case: global object that do not want to save in memory"
}'
Loop Traversal Example
Limit is sett to a moderate value, and it combines TraveseSession and Complete for loop traversal judgment. Take the Bash script as an example:
#!/bin/bash
PROTO_VERSION="2.0"
TCAPLUS_VERSION="Tcaplus3.50.0"
IDL_TYPE="protobuf"
CHECK_POLICY=3
result_flag=2
rest_url="http://172.17.32.17"
app_id=70
zone_id=1
table_name="game_players"
passwd=""
#caculate the md5 value of password
md5_passwd=$(echo -n $passwd | md5sum | cut -d ' ' -f1)
function build_headers(){
headers=(
-H "x-tcaplus-target: ${1}"
-H "x-tcaplus-app-id: ${app_id}"
-H "x-tcaplus-zone-id: ${zone_id}"
-H "x-tcaplus-protocol-version: ${PROTO_VERSION}"
-H "x-tcaplus-table-name: ${table_name}"
-H "x-tcaplus-pwd-md5: ${md5_passwd}"
-H "x-tcaplus-result-flag: ${result_flag}"
-H "x-tcaplus-version: ${TCAPLUS_VERSION}"
-H "x-tcaplus-data-version-check: ${CHECK_POLICY}"
-H "x-tcaplus-idl-type: ${IDL_TYPE}"
)
}
function build_traverse_loop_data(){
cat <<EOF
{
"Limit": 1,
"TraveseSession": "$1",
"ReturnValues": "user buffers"
}
EOF
}
function traverse_loop() {
target="Tcaplus.Traverse"
build_headers "$target"
complete="False"
limit=1
traverse_session="0.0"
total_num=0
while [ "$complete" == "False" ];do
ret=$(curl -i -XPOST "${headers[@]}" "$rest_url" -d "$(build_traverse_loop_data $traverse_session)" | grep -v HTTP | grep -v "json-1.0" | grep -v "content-length" | grep -v "^$" )
echo "$ret"
#Get the complete value, False or true, False: means that the data traversal has not yet ended, and true: means that the traversal has ended. Use the Json of Python 3
complete=$( echo "$ret" | python3 -c "import sys, json; print(json.load(sys.stdin)['Complete'])" )
# Get the value of TraveseSession, which is used for the location of the next traversal, similar to Offset.
traverse_session=$( echo "$ret" | python3 -c "import sys, json; print(json.load(sys.stdin)['TraveseSession'])" )
if [ "$complete" == "False" ];then
total_num=$(($total_num+1))
fi
done
echo "TotalRecords:$total_num"
}
declare -a headers
#traverse
traverse_loop
Return Syntax
Return Parameter Description
| Parameter name | Description |
|---|---|
| ErrorCode | Return code |
| ErrorMsg | Return message |
| ReturnValues | The reserved data set by the user arrives at tcaplus with the request and is returned in the original form by the response |
| SucceedRecords | Data returned of successful records in json format. For details, refer to Data section |
| SucceedNum | Number of successful records |
| FailedRecords | Data returned of failed records in json format. For details, refer to Data section |
| FailedNum | Number of failed records |
Return Example
Judge whether the Complete parameter is true. If it is false, continue the request through loop traversal.
{
"ErrorCode": 0,
"ErrorMsg": "Succeed",
"ReturnValues": "user buffers, to build a bridge between request and response, use case: global object that do not want to save in memory",
"MultiRecords": [{
"RecordVersion": 1,
"Record": {
"player_id": 3,
"player_name": "1",
"player_email": "15",
"game_server_id": 1,
"login_timestamp": [],
"logout_timestamp": [],
"is_online": false,
"pay": {
"pay_id": 0,
"amount": 0,
"method": 0
}
}
}, {
"RecordVersion": 1,
"Record": {
"player_id": 4,
"player_name": "44",
"player_email": "44",
"game_server_id": 44,
"login_timestamp": ["123456", "234"],
"logout_timestamp": ["123456", "234"],
"is_online": true,
"pay": {
"pay_id": 44,
"amount": 44,
"method": 44
}
}
}, {
"RecordVersion": 1,
"Record": {
"player_id": 54,
"player_name": "54",
"player_email": "54",
"game_server_id": 54,
"login_timestamp": [],
"logout_timestamp": [],
"is_online": false,
"pay": {
"pay_id": 54,
"amount": 54,
"method": 54
}
}
}, {
"RecordVersion": 3,
"Record": {
"player_id": 4,
"player_name": "4",
"player_email": "4",
"game_server_id": 4,
"login_timestamp": ["123456", "234"],
"logout_timestamp": ["123456", "234"],
"is_online": true,
"pay": {
"pay_id": 4,
"amount": 4,
"method": 4
}
}
}, {
"RecordVersion": 7,
"Record": {
"player_id": 5,
"player_name": "5",
"player_email": "5",
"game_server_id": 6,
"login_timestamp": ["123456", "234"],
"logout_timestamp": ["123456", "234"],
"is_online": false,
"pay": {
"pay_id": 5,
"amount": 5,
"method": 5
}
}
}, {
"RecordVersion": 1,
"Record": {
"player_id": 5,
"player_name": "5",
"player_email": "55",
"game_server_id": 55,
"login_timestamp": [],
"logout_timestamp": [],
"is_online": false,
"pay": {
"pay_id": 55,
"amount": 55,
"method": 55
}
}
}, {
"RecordVersion": 5,
"Record": {
"player_id": 6,
"player_name": "6",
"player_email": "6",
"game_server_id": 6,
"login_timestamp": ["123456", "234"],
"logout_timestamp": ["123456", "234"],
"is_online": true,
"pay": {
"pay_id": 6,
"amount": 6,
"method": 6
}
}
}],
"TotalNum": 7,
"Complete": true,
"TraveseSession": "80614.48569856"
}
Error code
Refer to Common Error Codes
Other Reference Documents
[[PB Generic Table][C++ SDK] Interface Description for Traversing Table Data](../../01C++_SDK/02Interface_Documents/13[Generic_Table]Traverse_Table_Data.md
[[PB Generic Table] [Go SDK] Interface Description for Traversing Table Data](../../02Go_SDK/02Interface_Documents/16[Generic_Table]Traverse_Table_Data.md