成功创建:集群,表格组,tb_online表
tb_online表描述文件table_test.proto如下:(tcaplusservice.optionv1.proto为依赖表)
syntax = "proto3"; package myTcaplusTable; //import tcaplusdb extensions import "tcaplusservice.optionv1.proto"; message tb_online { //define primary key option(tcaplusservice.tcaplus_primary_key) = "openid,tconndid,timekey"; //primary key fields int32 openid = 1; //QQ Uin int32 tconndid = 2; string timekey = 3; //non-primary key fields string gamesvrid = 4; int32 logintime = 5 ; repeated int64 lockid = 6; pay_info pay = 7; message pay_info { uint64 total_money = 1; uint64 pay_times = 2; } }
// 目标业务的tcapdir地址 static const char DIR_URL_ARRAY[][TCAPLUS_MAX_STRING_LENGTH] = { "tcp://10.191.***.99:9999" }; // 目标业务的tcapdir 地址个数 static const int32_t DIR_URL_COUNT = 1; // 目标业务的集群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";
//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; }
class CommonCallback : public TcaplusPbCallback { public: CommonCallback() { cout << "Init CommonCallback." << endl; } ~CommonCallback() { cout << "Fini ~CommonCallback." << endl; } int OnRecv(const NS_TCAPLUS_PROTOBUF_API::IndexGetRequest &req, NS_TCAPLUS_PROTOBUF_API::IndexGetResponse *res) { cout << "req " << (void *)&req << endl; g_dwTotalRevNum++; for (size_t idx = 0; idx < res->m_vecMsg.size(); idx++) { tb_online_index* t = dynamic_cast<tb_online_index *>(res->m_vecMsg[idx]); if (NULL == t) { cout << "ERROR: msgs[" << idx << "] not tb_online_index 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_index_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; } cout << "Total: " << res->m_nTotalNum << ", Remain: " << res->m_nRemainNum << endl; //If you does not call Set MESSAGE_OPTION_MESSAGE_AUTO_RELEASE option, you have to release res manually. //delete res; return 0; } int one rror(const std::vector< ::google::protobuf::Message *> &msgs, int errorcode) { g_dwTotalRevNum++; for (size_t idx = 0; idx < msgs.size(); idx++) { tb_online_index* t = dynamic_cast<tb_online_index *>(msgs[idx]); if (NULL == t) { cout << "ERROR: msgs[" << idx << "] not tb_online_index 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 << "openid = [" << t->openid() << "], tconndid = [" << t->tconndid() << "], timekey =[" << t->timekey() << "] failed:%d" << errorcode << endl; } return 0; } int OnTimeout(const std::vector< ::google::protobuf::Message *> &msgs) { for (size_t idx = 0; idx < msgs.size(); idx++) { tb_online_index* t = dynamic_cast<tb_online_index *>(msgs[idx]); if (NULL == t) { cout << "TIMEOUT: msgs[" << idx << "] not tb_online_index 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 ¶m) { cout << "OnFinish: " << param.m_nOperation << " req: " << param.m_vecMsgs.size() << endl; return 0; } };
void SendGetRequest(struct schedule * S, void* arg) { static tb_online_index t; t.set_openid(1); t.set_tconndid(1); cout << "Get By Index INIT: openid= " << t.openid() << ", tconndid= " << t.tconndid() << endl; //Set session resource auto release //g_stAsyncApi.SetMessageOption(t, NS_TCAPLUS_PROTOBUF_API::MESSAGE_OPTION_MESSAGE_AUTO_RELEASE, ""); static CommonCallback cb; static NS_TCAPLUS_PROTOBUF_API::IndexGetRequest req; cout << "req: " << (void *)&req << endl; req.m_strIndexName = "index_openid_tconndid"; req.m_pMsg = &t; req.m_iOffset = 0; req.m_iLimit = 10; int32_t iRet = g_stAsyncApi.Get(req, &cb); if (iRet != TcapErrCode::GEN_ERR_SUC) { cout << "ERROR: openid= " << t.openid() << ", tconndid= " << t.tconndid() << ", timekey= " << t.timekey() << ", Get Error iRet = " << iRet << endl; } }
main.cpp
int main(void) { //初始化API客户端 int ret = InitAsyncPbApi(); if ( ret != 0) { printf("InitAsyncPbApi failed\n"); return -1; } //发送请求 ret = SendListGetRequest(); if (0 != ret) { printf("SendAddRequest failed\n"); return -1; } //接收响应 do { // 更新,接收回包 g_stAsyncApi.UpdateNetwork(); usleep(1000 * 10); } while (g_dwTotalRevNum != 1); return 0; }
TcaplusDB是腾讯出品的分布式NoSQL数据库,存储和调度的代码完全自研。具备缓存+落地融合架构、PB级存储、毫秒级时延、无损水平扩展和复杂数据结构等特性。同时具备丰富的生态、便捷的迁移、极低的运维成本和五个九高可用等特点。客户覆盖游戏、互联网、政务、金融、制造和物联网等领域。