[PB表][Go SDK]查询表记录总数

1. 接口说明

查询表记录总数

2. 版本要求

无特殊要求,所有版本都提供了该接口。

3. 准备工作

参见准备工作文档,完成使用该接口前的准备工作,并创建如下PB Generic表。game_players表

syntax = "proto3";                      // Specify the version of the protocol buffers language

package tcaplusservice;

import "tcaplusservice.optionv1.proto"; // Use the public definitions of TcaplusDB by importing them.

message game_players {  // Define a TcaplusDB table with message

// Specify the primary keys with the option tcaplusservice.tcaplus_primary_key
// The primary key of a TcaplusDB table has a limit of 4 fields
option(tcaplusservice.tcaplus_primary_key) = "player_id, player_name, player_email";

// Specify the primary key indexes with the option tcaplusservice.tcaplus_index
option(tcaplusservice.tcaplus_index) = "index_1(player_id, player_name)";
option(tcaplusservice.tcaplus_index) = "index_2(player_id, player_email)";


// Value Types supported by TcaplusDB
// int32, int64, uint32, uint64, sint32, sint64, bool, fixed64, sfixed64, double, fixed32, sfixed32, float, string, bytes
// Nested Types Message

// primary key fields
int64 player_id = 1;
string player_name = 2;
string player_email = 3;


// Ordinary fields
int32 game_server_id = 4;
repeated string login_timestamp = 5;
repeated string logout_timestamp = 6;
bool is_online = 7;

payment pay = 8;
}


message payment {

int64 pay_id = 1;
uint64 amount = 2;
int64 method = 3;

}

准备工作完成后,将会获得以下信息,这些信息在使用SDK时会被用到:

  1. 目录服务器地址列表
  2. 业务ID
  3. 业务访问密码
  4. 游戏区ID
  5. 数据表名

4. 示例代码

示例代码的基本执行过程:

  1. 定义表配置参数
  2. 设置日志配置;
  3. 创建客户端;
  4. 发送请求并处理响应;
  5. 销毁客户端。

客户端初始化示例:

package main

import (
    "fmt"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api"
)

// 定义表配置参数
const (
    AppId                = uint64(2)
    ZoneId               = uint32(3)
    DirUrl               = "tcp://x.x.x.x:xxxx"
    Signature            = "xxxxxxxxxxxxx"
    TableName            = "game_players"
)

var client *tcaplus.PBClient

func main() {
    // 创建客户端
    client = tcaplus.NewPBClient()
    // 设置日志配置,logconf.xml文件设置了日志级别
    if err := client.SetLogCfg("./logconf.xml"); err != nil {
      fmt.Println(err.Error())
      return
    }

    //构造Map对象存储对应表格组下所有的表
    zoneList := []uint32{ZoneId}
    zoneTable := make(map[uint32][]string)
    zoneTable[ZoneId] = []string{TableName}

    // 连接TcaplusDB后端
    err := client.Dial(AppId, zoneList, DirUrl, Signature, 30, zoneTable)
    if err != nil {
        fmt.Printf("init failed %v\n", err.Error())
        return
    }
    // 设置默认使用的zone
    client.SetDefaultZoneId(ZoneId)
    fmt.Printf("Dial finish\n")
    getCountExample()

    // 程序退出时调用Close销毁客户端
    client.Close()
}

4.1 同步调用示例(推荐)

该示例同步调用方式比较类似C++接口的调用方式,需要创建请求和解析响应示例目录

package main

import (
    "fmt"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/example/PB/tools"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/logger"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/protocol/cmd"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/terror"
    "time"
)

func getCountExample() {
    // 生成 get 请求
    req, err := client.NewRequest(tools.Zone, "game_players", cmd.TcaplusApiGetTableRecordCountReq)
    if err != nil {
        logger.ERR("NewRequest error:%s", err)
        return
    }

    // 发送请求,接收响应
    resp, err := client.Do(req, 5*time.Second)
    if err != nil {
        logger.ERR("SendRequest error:%s", err)
        return
    }

    // 获取响应结果
    errCode := resp.GetResult()
    if errCode != terror.GEN_ERR_SUC {
        logger.ERR("insert error:%s", terror.GetErrMsg(errCode))
        return
    }
    fmt.Println("table rec count", resp.GetTableRecordCount())
    logger.INFO("get success")
    fmt.Println("get success")
}

4.2 异步调用示例

异步发送可以使用较少的协程实现较大的并发,编码相对复杂,示例目录

package main

import (
    "fmt"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/example/PB/tools"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/logger"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/protocol/cmd"
    "git.woa.com/gcloud_storage_group/tcaplus-go-api/terror"
    "sync"
    "time"
)

func getCountExample() {
    wg := sync.WaitGroup{}
    wg.Add(1)
    // 在另一协程处理响应消息
    go func() {
        defer wg.Done()
        for {
            // resp err 均为 nil 说明响应池中没有任何响应
            resp, err := client.RecvResponse()
            if err != nil {
                logger.ERR("RecvResponse error:%s", err)
                continue
            } else if resp == nil {
                time.Sleep(time.Microsecond * 5)
                continue
            }

            // 获取响应结果
            errCode := resp.GetResult()
            if errCode != terror.GEN_ERR_SUC {
                logger.ERR("insert error:%s", terror.GetErrMsg(errCode))
                return
            }

            fmt.Println("table record count ", resp.GetRecordCount())
        }
    }()

    // 生成 get 请求
    req, err := client.NewRequest(tools.ZoneId, "game_players", cmd.TcaplusApiGetTableRecordCountReq)
    if err != nil {
        logger.ERR("NewRequest error:%s", err)
        return
    }

    // 异步发送请求
    if err := client.SendRequest(req); err != nil {
        fmt.Printf("SendRequest failed %v\n", err.Error())
        return
    }
    wg.Wait()
}

5. 常见问题

详见错误码含义和处理方法

6. 其它参考文档

[PB表][C++ SDK]查询表记录总数接口说明

[PB表][RESTFul API]查询表记录总数接口说明

results matching ""

    No results matching ""