当前位置:网站首页>[tcapulusdb knowledge base] insert data example (TDR table)
[tcapulusdb knowledge base] insert data example (TDR table)
2022-06-23 22:22:00 【Tcapulus Jun】
【TcaplusDB The knowledge base 】 Insert data sample (TDR surface )
Premise
Successfully created : colony , Table group ,PLAYERONLINECNT surface
PLAYERONLINECNT Table description file table_test.xml as follows :
<?xml version="1.0" encoding="GBK" standalone="yes" ?>
<metalib name="tcaplus_tb" tagsetversion="1" version="1">
<struct name="PLAYERONLINECNT" version="1" primarykey="TimeStamp,GameSvrID" splittablekey="TimeStamp">
<entry name="TimeStamp" type="uint32" desc=" In minutes " />
<entry name="GameSvrID" type="string" size="64" />
<entry name="GameAppID" type="string" size="64" desc="gameapp id" />
<entry name="OnlineCntIOS" type="uint32" defaultvalue="0" desc="ios Number of people online " />
<entry name="OnlineCntAndroid" type="uint32" defaultvalue="0" desc="android Number of people online " />
<entry name="BinaryLen" type="smalluint" defaultvalue="1" desc=" Data source data length ; The length is 0 when , Ignore source check "/>
<entry name="binary" type="tinyint" desc=" Binary system " count= "1000" refer="BinaryLen" />
<entry name="binary2" type="tinyint" desc=" Binary system 2" count= "1000" refer="BinaryLen" />
<entry name="strstr" type="string" size="64" desc=" character string "/>
<index name="index_id" column="TimeStamp"/>
</struct>
</metalib>Define configuration parameters
// Target business tcapdir Address
static const char DIR_URL_ARRAY[][TCAPLUS_MAX_STRING_LENGTH] =
{
"tcp://10.191.***.99:9999",
"tcp://10.191.***.88:9999"
};
// Target business tcapdir Number of addresses
static const int32_t DIR_URL_COUNT = 2;
// Cluster of target business ID
static const int32_t APP_ID = 3;
// Table group of target business ID
static const int32_t ZONE_ID = 1;
// The business password of the target business
static const char * SIGNATURE = "*******";
// Table name of the target business PLAYERONLINECNT
static const char * TABLE_NAME = "PLAYERONLINECNT";initialization TcaplusAPI Log handle
Log profile :tlogconf.xml
//TCaplus service Log class
TcaplusService::TLogger* g_pstTlogger;
LPTLOGCATEGORYINST g_pstLogHandler;
LPTLOGCTX g_pstLogCtx;
int32_t InitLog()
{
// The absolute path to the log configuration file
const char* sLogConfFile = "tlogconf.xml";
// Log class name
const char* sCategoryName = "mytest";
// Initialize log handle from configuration file
g_pstLogCtx = tlog_init_from_file(sLogConfFile);
if (NULL == g_pstLogCtx)
{
fprintf(stderr, "tlog_init_from_file failed.\n");
return -1;
}
// Get log class
g_pstLogHandler = tlog_get_category(g_pstLogCtx, sCategoryName);
if (NULL == g_pstLogHandler)
{
fprintf(stderr, "tlog_get_category(mytest) failed.\n");
return -2;
}
// Initialize log handle
g_pstTlogger = new TcaplusService::TLogger(g_pstLogHandler);
if (NULL == g_pstTlogger)
{
fprintf(stderr, "TcaplusService::TLogger failed.\n");
return -3;
}
return 0;
}initialization TcaplusAPI client
//TCaplus service API Client main class
TcaplusService::TcaplusServer g_stTcapSvr;
// Tabular meta Information
extern unsigned char g_szMetalib_tcaplus_tb[];
LPTDRMETA g_szTableMeta = NULL;
int32_t InitServiceAPI()
{
// initialization
int32_t iRet = g_stTcapSvr.Init(g_pstTlogger, /*module_id*/0, /*app id*/APP_ID, /*zone id*/ZONE_ID, /*signature*/SIGNATURE);
if (0 != iRet)
{
tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.Init failed, iRet: %d.", iRet);
return iRet;
}
// Add directory server
for (int32_t i = 0; i< DIR_URL_COUNT; i++)
{
iRet = g_stTcapSvr.AddDirServerAddress(DIR_URL_ARRAY[i]);
if (0 != iRet)
{
tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.AddDirServerAddress(%s) failed, iRet: %d.", DIR_URL_ARRAY[i], iRet);
return iRet;
}
}
// Get the of the table meta describe
g_szTableMeta = tdr_get_meta_by_name((LPTDRMETALIB)g_szMetalib_tcaplus_tb, TABLE_NAME);
if(NULL == g_szTableMeta)
{
tlog_error(g_pstLogHandler, 0, 0,"tdr_get_meta_by_name(%s) failed.", TABLE_NAME);
return -1;
}
// Registration data table ( Connect dir The server , authentication , Get table route ),10s Overtime
iRet = g_stTcapSvr.RegistTable(TABLE_NAME, g_szTableMeta, /*timeout_ms*/10000);
if(0 != iRet)
{
tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.RegistTable(%s) failed, iRet: %d.", TABLE_NAME, iRet);
return iRet;
}
// Connect all the data corresponding to the table tcaplus proxy The server
iRet = g_stTcapSvr.ConnectAll(/*timeout_ms*/10000, 0);
if(0 != iRet)
{
tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.ConnectAll failed, iRet: %d.", iRet);
return iRet;
}
return 0;
}adopt API send out replace request
It can also be sent TCAPLUS_API_INSERT_REQ insert data , If the data exists, it will return failure
TCAPLUS_API_REPLACE_REQ insert data , If the data exists, it will replace
int32_t SendReplaceRequest()
{
// Request object class
TcaplusService::TcaplusServiceRequest* pstRequest = g_stTcapSvr.GetRequest(TABLE_NAME);
if (NULL == pstRequest)
{
tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.GetRequest(%s) failed.", TABLE_NAME);
return -1;
}
// Initialize the request object
int iRet = pstRequest->Init(TCAPLUS_API_REPLACE_REQ, NULL, 0, 0, 0, 0);
if(0 != iRet)
{
tlog_error(g_pstLogHandler, 0, 0, "pstRequest->Init(TCAPLUS_API_REPLACE_REQ) failed, iRet: %d.", iRet);
return iRet;
}
// Add table record for request
TcaplusService::TcaplusServiceRecord* pstRecord = pstRequest->AddRecord();
if (NULL == pstRecord)
{
tlog_error(g_pstLogHandler, 0, 0, "pstRequest->AddRecord() failed.");
return -1;
}
PLAYERONLINECNT stPLAYERONLINECNT;
memset(&stPLAYERONLINECNT, 0, sizeof(stPLAYERONLINECNT));
// Set up updated key Information
stPLAYERONLINECNT.dwTimeStamp = 1;
snprintf(stPLAYERONLINECNT.szGameSvrID, sizeof(stPLAYERONLINECNT.szGameSvrID), "%s", "mysvrid");
// Set up updated value Information
snprintf(stPLAYERONLINECNT.szGameAppID, sizeof(stPLAYERONLINECNT.szGameAppID), "%s", "myappid");
stPLAYERONLINECNT.dwOnlineCntIOS = 1;
stPLAYERONLINECNT.dwOnlineCntAndroid = 1;
// The settings are based on TDR Describe the settings record data
iRet = pstRecord->SetData(&stPLAYERONLINECNT, sizeof(stPLAYERONLINECNT));
if(0 != iRet)
{
tlog_error(g_pstLogHandler, 0, 0, "pstRecord->SetData() failed, iRet: %d.", iRet);
return iRet;
}
// Get the result of an asynchronous transaction ID
uint64_t asyncid = response->GetAsynID();
// Set user cache , The reply will carry back
iRet = pstRequest->SetUserBuff(&stPLAYERONLINECNT, sizeof(stPLAYERONLINECNT));
if(0 != iRet)
{
tlog_error(g_pstLogHandler, 0, 0, "pstRequest->SetUserBuff() failed, iRet: %d.", iRet);
return iRet;
}
// Send request message packet
iRet= g_stTcapSvr.SendRequest(pstRequest);
if(0 != iRet)
{
tlog_error(g_pstLogHandler, 0, 0, "g_stTcapSvr.SendRequest failed, iRet: %d.", iRet);
return iRet;
}
return 0;
}adopt API Receiving response
int RecvResp(TcaplusServiceResponse*& response)
{
// Packet receiving is blocked here , Every time there's a blockage 1ms, closed 5000 Time
unsigned int sleep_us = 1000;
unsigned int sleep_count = 5000;
do
{
usleep(sleep_us);
response = NULL;
int ret = g_stTcapSvr.RecvResponse(response);
if (ret < 0)
{
tlog_error(g_pstLogHandler, 0, 0, "tcaplus_server.RecvResponse failed. ret:%d", ret);
return ret;
}
// Get the result of an asynchronous transaction ID
uint64_t asyncid = response->GetAsynID();
// Get the user cache information in the request
size_t buf_len = 0;
const char* userbuf = response->GetUserBuff(&buf_len);
// Received a response packet
if (1 == ret)
{
break;
}
} while ((--sleep_count) > 0);
//5s Overtime
if (0 == sleep_count)
{
tlog_error(g_pstLogHandler, 0, 0, "tcaplus_server.RecvResponse wait timeout.");
return -1;
}
return 0;
}Example
main.cpp
int main(void) {
// Initialize log
int ret = InitLog();
if ( ret != 0)
{
printf("init log failed\n");
return -1;
}
// initialization API client
ret = InitServiceAPI();
if ( ret != 0)
{
printf("init InitServiceAPI failed\n");
return -1;
}
// Send a request
ret = SendReplaceRequest();
if (0 != ret)
{
printf("SendReplaceRequest failed\n");
return -1;
}
// Receiving response
TcaplusServiceResponse* response = NULL;
ret = RecvResp(response);
if (0 != ret)
{
printf("RecvResp failed\n");
return -1;
}
// Get the result of the operation ,0 It means success
int32_t result = response->GetResult();
if (0 != result)
{
printf("the result is %d\n", result);
return -1;
}
return 0;
}TcaplusDB It's a distributed product of Tencent NoSQL database , The code for storage and scheduling is completely self-developed . With cache + Landing fusion architecture 、PB Levels of storage 、 Millisecond delay 、 Lossless horizontal expansion and complex data structure . At the same time, it has rich ecological environment 、 Easy migration 、 Extremely low operation and maintenance costs and five nine high availability features . Customer coverage game 、 Internet 、 government affairs 、 Finance 、 Manufacturing and the Internet of things .
边栏推荐
- there can be only one auto column and it must be defined as a key
- Acl2022 | MVR: multi view document representation for open domain retrieval
- MySQL de duplication query only keeps one latest record
- Using nodejs and Tencent cloud API to identify invoices
- Apt attack
- Configuring error sets using MySQL for Ubuntu 20.04.4 LTS
- Advantages of micro service registry Nacos over Eureka
- Go language core 36 lectures (go language practice and application 26) -- learning notes
- 使用 Provider 改造屎一样的代码,代码量降低了2/3!
- Bi SQL constraints
猜你喜欢

University of North China, Berkeley University of California, etc. | Domain Adaptive Text Classification with structural Knowledge from unlabeled data

Pourquoi une seule valeur apparaît - elle sur votre carte de données?

Ten thousand words! Understand the inheritedwidget local refresh mechanism

為什麼你的數據圖譜分析圖上只顯示一個值?

为什么你的数据图谱分析图上只显示一个值?
Performance optimization of database 5- database, table and data migration

万字长文!一文搞懂InheritedWidget 局部刷新机制

Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse

ICML2022 | 基于对比学习的离线元强化学习的鲁棒任务表示

脚本之美│VBS 入门交互实战
随机推荐
CMU博士论文 | 通过记忆的元强化学习,118页pdf
Benchclock: a benchmark for evaluating semantic analysis language models
北大、加州伯克利大学等联合| Domain-Adaptive Text Classification with Structured Knowledge from Unlabeled Data(基于未标记数据的结构化知识的领域自适应文本分类)
In depth understanding of Internet of things device access layer
Important announcement: Tencent cloud es' response strategy to log4j vulnerability
How to select Poe, poe+, and poe++ switches? One article will show you!
WordPress plugin WP guppy 1.1 - WP JSON API sensitive information disclosure
Targeted, real-time audio and video optimization in remote control
Application practice | Apache Doris integrates iceberg + Flink CDC to build a real-time federated query and analysis architecture integrating lake and warehouse
Redis source code analysis -- QuickList of redis list implementation principle
Using the provider to transform the shit like code, the amount of code is reduced by 2/3!
Teach you how to write a delay queue
脚本之美│VBS 入门交互实战
[open source]goravel, a fully functional and extensible golang web application framework
Interpretation of opentelemetry project
How ppt creates a visual chart
How to improve the high concurrency of the server
[log service CLS] one click to start the efficient operation and maintenance journey of Tencent E-Sign
Dart series: smooth as silk, operating files and directories
Go build command (go language compilation command) complete introduction