tencent cloud

游戏数据库 TcaplusDB

产品动态
产品简介
产品概述
产品优势
应用场景
产品架构
地域介绍
购买指南
产品定价
欠费说明
快速入门
了解基本概念
创建集群
创建表格组
创建表格
获取连接信息
访问 TcaplusDB
操作指南
管理集群与表格组
管理表格
监控与告警
访问管理
标签
任务列表
使用 TcaplusDB client
使用 client 工具访问数据
client 工具命令参考
使用 TcaplusDB SDK
变更历史
SDK 下载
SDK for C++ 接口说明
TcaplusDB 错误码
SDK 安装
PB 表 C++SDK 操作方法
TDR 表 C++SDK 操作方法
使用 TcaplusDB RESTful API
RESTful API 接口说明
Go RESTful API 接口说明
Java RESTful API 接口说明
PHP RESTful API 接口说明
Python RESTful API 接口说明
RESTful API 各语言示例下载
实践教程
表结构设计
数据库交互
常见问题
信息咨询类
数据库使用类
数据库原理类
API 文档
History
Introduction
API Category
Making API Requests
Table Group APIs
Other APIs
Table APIs
Cluster APIs
Data Types
Error Codes
相关协议
服务等级协议
服务条款
词汇表
联系我们

写入数据

PDF
聚焦模式
字号
最后更新时间: 2024-12-04 10:02:31

前提

成功创建:集群,表格组,tb_online 表
tb_online 表描述文件 table_test.proto 如下:(tcaplusservice.optionv1.proto 为依赖表)
syntax = "proto2";

package myTcaplusTable;

import "tcaplusservice.optionv1.proto";

message tb_online {
option(tcaplusservice.tcaplus_primary_key) = "openid,tconndid,timekey";

required int32 openid = 1; //QQ Uin
required int32 tconndid = 2;
required string timekey = 3;
required string gamesvrid = 4;
optional int32 logintime = 5 [default = 1];
repeated int64 lockid = 6 [packed = true]; //repeated 类型字段使用 packed 关键字修饰
optional pay_info pay = 7;

message pay_info {
optional uint64 total_money = 1;
optional uint64 pay_times = 2;
}
}

步骤1:定义配置参数

// 目标集群的访问地址
static const char DIR_URL_ARRAY[][TCAPLUS_MAX_STRING_LENGTH] =
{
"tcp://10.191.***.99:9999",
"tcp://10.191.***.88:9999"
};
// 目标集群的地址个数
static const int32_t DIR_URL_COUNT = 2;
// 目标业务的集群 ID
static const int32_t APP_ID = 3;
// 目标业务的表格组 ID
static const int32_t ZONE_ID = 1;
// 目标业务的业务密码
static const char * SIGNATURE = "*******";
// 目标业务的表名 tb_online
static const char * TABLE_NAME = "tb_online";

步骤2:初始化 TcaplusPB 客户端

//Tcaplus PB API 客户端
TcaplusAsyncPbApi g_stAsyncApi;
int32_t InitAsyncPbApi()
{
//PB API 配置
ClientOptions cfg;
cfg.app_id = APP_ID;
cfg.zones.push_back(ZONE_ID);
strcpy(cfg.signature, SIGNATURE);
for (int32_t i = 0; i < DIR_URL_COUNT; i++)
{
cfg.dirs.push_back(DIR_URL_ARRAY[i]);
}
//访问的 PB 表
cfg.tables.push_back(TABLE_NAME);
//日志配置
strncpy(cfg.log_cfg, "tlogconf.xml", sizeof(cfg.log_cfg));
//初始化连接超时时间5s
cfg.timeout = 5000;

//初始化连接
int32_t iRet = g_stAsyncApi.Init(cfg);
if (0 != iRet)
{
cout << "ERROR: g_stAsyncApi.Init failed, log cfg: " << cfg.log_cfg << ", iRet: " << iRet << "." << endl;
return iRet;
}
return iRet;
}

步骤3:定义异步回调

//收到响应消息计数
uint32_t g_dwTotalRevNum = 0;
class CommonCallback : public TcaplusPbCallback
{
public:
CommonCallback()
{
cout << "Init CommonCallback." << endl;
}

~CommonCallback()
{
cout << "Fini ~CommonCallback." << endl;
}
//收到响应消息的回调,msgs 为获取的记录的数组
int OnRecv(const std::vector< ::google::protobuf::Message *> &msgs)
{
cout << "OnRecv[" << msgs.size() << endl;
g_dwTotalRevNum++;
for (size_t idx = 0; idx < msgs.size(); idx++)
{
tb_online* t = dynamic_cast<tb_online *>(msgs[idx]);
if (NULL == t)
{
cout << "ERROR: msgs[" << idx << "] not tb_online type." << endl;
return -1;
}

cout << "---------- receive a response----------:" << endl;
cout << "openid=" << t->openid() << endl;
cout << "tconndid=" << t->tconndid() << endl;
cout << "timekey=" << t->timekey() << endl;
cout << "gamesvrid=" << t->gamesvrid() << endl;
cout << "logintime=" << t->logintime() << endl;
for (int32_t i = 0; i < t->lockid_size();i++)
{
cout << "lockid[" << i << "]=" << t->lockid(i) << endl;
}
tb_online_pay_info pay = t->pay();
cout << "pay total_money=" << pay.total_money() << "; pay_times=" << pay.pay_times() << endl;

//获取记录的版本号
std::string version;
int iRet = g_stAsyncApi.GetMessageOption(*t, NS_TCAPLUS_PROTOBUF_API::MESSAGE_OPTION_DATA_VERSION, &version);
cout << "after GetMessageOption iRet= [" << iRet << "] version:" << version.c_str() << " msg:" << t << endl;
}

return 0;
}

//收到错误响应消息的回调
int OnError(const std::vector< ::google::protobuf::Message *> &msgs, int errorcode)
{
cout << "OnError[" << msgs.size() << endl;
g_dwTotalRevNum++;
for (size_t idx = 0; idx < msgs.size(); idx++)
{
tb_online* t = dynamic_cast<tb_online *>(msgs[idx]);
if (NULL == t)
{
cout << "ERROR: msgs[" << idx << "] not tb_online type." << endl;
return -1;
}

if (TcapErrCode::TXHDB_ERR_RECORD_NOT_EXIST == errorcode)
{
cout << "ERROR: openid= " << t->openid() << ", tconndid= " << t->tconndid() << ", timekey= " << t->timekey() << ", record not exists" << endl;
}
cout << "ERROR: openid = [" << t->openid() << "], tconndid = [" << t->tconndid() << "], timekey =[" << t->timekey() << "] failed:%d" << errorcode << endl;
}

return 0;
}

//消息超时的回调
int OnTimeout(const std::vector< ::google::protobuf::Message *> &msgs)
{
cout << "OnTimeout[" << msgs.size() << endl;
for (size_t idx = 0; idx < msgs.size(); idx++)
{
tb_online* t = dynamic_cast<tb_online *>(msgs[idx]);
if (NULL == t)
{
cout << "TIMEOUT: msgs[" << idx << "] not tb_online type!" << endl;
return -1;
}

cout << "TIMEOUT: openid = [" << t->openid() << "], tconndid = [" << t->tconndid() << "], timekey =[" << t->timekey() << "] timeout" << endl;
}

return 0;
}

int OnFinish(const NS_TCAPLUS_PROTOBUF_API::MsgParam &param)
{
cout << "OnFinish: " << param.m_nOperation << " req: " << param.m_vecMsgs.size()<< endl;
return 0;
}
};

步骤4:发送 add 请求

int SendAddRequest()
{
static tb_online t;
t.set_openid(2);
t.set_tconndid(2);
t.set_timekey("test_tcaplus_2");
t.set_gamesvrid("MyValueStr_2");
t.set_logintime(333);
for (int32_t i = 0; i < TOTAL_V3_ARRAY_NUM;i++)
{
t.add_lockid(i+1);
}

tb_online_pay_info* pay = new tb_online_pay_info();
pay->set_total_money(1024);
pay->set_pay_times(1);
t.set_allocated_pay(pay);

cout << "INIT: openid= " << t.openid() << ", tconndid= " << t.tconndid() << ", timekey= " << t.timekey() << endl;

std::string version = "-1";
g_stAsyncApi.SetMessageOption(t, NS_TCAPLUS_PROTOBUF_API::MESSAGE_OPTION_DATA_VERSION, version);

static CommonCallback cb;
int32_t iRet = g_stAsyncApi.Add(&t, &cb);
if (iRet != TcapErrCode::GEN_ERR_SUC)
{
cout << "ERROR: openid= " << t.openid() << ", tconndid= " << t.tconndid() << ", timekey= " << t.timekey() << ", Set Error iRet = " << iRet << endl;
return -1;
}
return 0;
}

示例

int main(void) {

//初始化 API 客户端
int ret = InitAsyncPbApi();
if ( ret != 0)
{
printf("InitAsyncPbApi failed\\n");
return -1;
}

//发送请求
ret = SendAddRequest();
if (0 != ret)
{
printf("SendAddRequest failed\\n");
return -1;
}

//接收响应
do
{
// 更新,接收回包
g_stAsyncApi.UpdateNetwork();
usleep(1000 * 10);
} while (g_dwTotalRevNum != 1);
return 0;
}

帮助和支持

本页内容是否解决了您的问题?

填写满意度调查问卷,共创更好文档体验。

文档反馈