Tcaplus使用总结

Posted 流子

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tcaplus使用总结相关的知识,希望对你有一定的参考价值。

一、Tcaplus是什么?

游戏数据库 TcaplusDB 是专为游戏设计的 NoSQL 分布式数据存储服务,支持 Protobuf 接口访问,Tcaplus 将 Cache 与硬盘结合,追求高性能的同时,也节省成本,很好地支持全区全服和分区分服,并针对游戏爆发增长和长尾运维特点提供不停机扩缩容、备份容灾、快速回档等全套解决方案,安全可信赖。目前应用于《王者荣耀》、《穿越火线》、《火影忍者》等数百款流行游戏。

二、使用步骤

特别要注意的是,在购买时,连接协议里选择TDR,这样就支持xml表定义了。

如何定义表

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<metalib name="demo_table" tagsetversion="1" version="1">

	<struct name="user" version="1" primarykey="user_id,server_id" splittablekey="user_id">
    	<entry name="user_id" type="string" size="450" desc="用户ID"/>
    	<entry name="server_id" type="uint64" desc="服务器ID" />
    	<entry name="nick_name" type="string" size="50" desc="昵称"/>
    	<entry name="desc" type="string" size="1024" desc="描述信息"/>
    	<entry name="state" type="Tinyuint" defaultvalue="0" desc="用户状态 0 : AVALIABLE, 1 DELETED"/>
    	<index name="index1" column="user_id"/>
		<index name="index2" column="user_id,server_id"/>
	</struct>

	 <struct name="mail" version="1" primarykey="user_id,server_id,mail_id" splittablekey="user_id" >
		<entry name="user_id" type="string" size="50"  desc="用户ID"/>
		<entry name="server_id" type="uint64" desc="服务器ID" />
		<entry name="mail_id" type="uint64" desc="邮件ID" />
		<entry name="state" type="Tinyuint" defaultvalue="0" desc="邮件状态(0 未读/1 已读)"/>
		<entry name="from" type="string" size="50" desc="邮件发送方名字"/>
		<entry name="subject" type="string" size="100" desc="邮件标题"/>
		<entry name="content" type="string" size="1024" desc="邮件正文"/>
		<index name="index1" column="user_id"/>
		<index name="index2" column="user_id,server_id"/>
		<index name="index3" column="user_id,mail_id"/>
	</struct>

	<struct name="server" version="1" primarykey="server_id" splittablekey="server_id" >
		<entry name="server_id" type="uint64" desc="服务器ID" />
		<entry name="server_name" type="string" size="50" desc="服务器名称"/>
		<entry name="capacity" type="uint64"  desc="服务器最大承载容量"/>
    	<entry name="desc" type="string" size="1024" desc="描述信息"/>
    	<entry name="state" type="Tinyuint" defaultvalue="0" desc="server状态 0 : AVALIABLE, 1 DELETED"/>
    	<index name="index1" column="server_id"/>
	</struct>
	
</metalib>

通过 client 工具访问 TcaplusDB 数据

1.tcaplus_client 是一个 TcaplusDB 表访问的客户端工具,可通过下表中的下载链接进行下载,Linux x86_64 平台的 TcaplusServiceAPI 发布包包含64位 Linux 版本的 tcaplus_client 工具:

版本发布时间操作系统下载包名
3.46.0.1990332020/12/28Linux x86_64下载

2.下载完成 TcaplusServiceApi 安装包后,将其 通过上传工具 上传至与 TcaplusDB 集群同一VPC,同一子网的云服务器中,然后解压。

tar -xf TcaplusPbApi3.46.0.199033.x86_64_release_20201210.tar.gz -C tcaplus

解压完成后,进入至 tcaplus 的 bin 目录中,并赋予可执行权限:

cd tcaplus/release/x86_64/bin
chmod +x tcaplus_client

直接执行./tcaplus_client命令,会提示连接数据库所需的参数信息,用户可以根据自己的集群信息进行填写

参数说明是否必填
-a接入 ID
-z表格组 ID
-s集群密码
-d集群 IP 地址及端口
-e需要执行的 SQL 语句,需要双引号包含
./tcaplus_client -a 15 -z 1 -s "ooxx" -d 172.16.0.7:9999

返回结果

====== Welcome to use tcaplus_client, use "help" to show usage ======

tcaplus> help;
--------------------------------------------------------------------------------
    help: show usage of commands, example: "help select;".
    show: get server status related information. executing "help show;" for details.
    exit/quit: exit the client.
    count: print record number in the database.
    desc: print table field name and type.
    select: query records from database.
    insert: insert a new record into database.
   replace: replace a record into the database.
   update: update a record in the database.
   delete: delete record(s) from database.
   dump: dump records from database.
   load: load records into the database.
--------------------------------------------------------------------------------

查看数据库集群状态

tcaplus> show status;

------------------------------------------------------------------
Access_id(app_id) = 15
------------------------------------------------------------------
TableGroup_id(zone_id) = 1 
------------------------------------------------------------------
[dir_server_url]
    dir_server_url[0]: 172.16.0.7:9999
------------------------------------------------------------------
[Proxys]
    proxy[0]:tcp://172.16.0.7:10001 CONNECTED Authened 
    proxy[1]:tcp://172.16.0.7:10002 CONNECTED Authened 
------------------------------------------------------------------
[The connected dir server]
    tcp://172.16.0.7:9999
------------------------------------------------------------------

查看所有表

tcaplus> show tables;

----------------------------------------------------------
| Table Name                         Type      Protocol  |
----------------------------------------------------------
| server                             GENERIC   TDR       |
| user                               GENERIC   TDR       |
| mail                               GENERIC   TDR       |
----------------------------------------------------------

查看表结构

tcaplus> desc mail;
Table Structure From: Tcaplus System
TableName: mail
TableType: GENERIC, IdlType: TDRXML
SvrTdrCurrentVersion: 1
+---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+
|Field    |Type  |Key     |Index|Size|Version|DefaultValue|Count|Select|Desc                   |
+---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+
|user_id  |string|PRI,SPLT|-    |50  |1      |-           |1    |-     |用户ID                 |
+---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+
|server_id|uint64|PRI     |-    |8   |1      |-           |1    |-     |服务器ID               |
+---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+
|mail_id  |uint64|PRI     |-    |8   |1      |-           |1    |-     |邮件ID                 |
+---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+
|state    |uint8 |-       |-    |1   |1      |0           |1    |-     |邮件状态(0 未读/1 已读)|
+---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+
|from     |string|-       |-    |50  |1      |-           |1    |-     |邮件发送方名字         |
+---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+
|subject  |string|-       |-    |100 |1      |-           |1    |-     |邮件标题               |
+---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+
|content  |string|-       |-    |1024|1      |-           |1    |-     |邮件正文               |
+---------+------+--------+-----+----+-------+------------+-----+------+-----------------------+
7 rows in set (113 us)

插入:

tcaplus> insert into mail (user_id, server_id, mail_id, state,from,subject,content) values ("10001",1,202106301221,0,"系统","新手邮件","欢迎你,来到我们的虚拟世界");
insert success
insert time: 6427 us

查询表格的记录总数

tcaplus> count mail;
--------------------------------------------------------------------------------
| TableName                                 Count                              |
--------------------------------------------------------------------------------
| mail                                      1                                  |
--------------------------------------------------------------------------------

查询:

tcaplus> select * from mail where user_id = "10001" and server_id = 1;
+-------+---------+------------+-----+------+----------+----------------------------+
|user_id|server_id|mail_id     |state|from  |subject   |content                     |
+-------+---------+------------+-----+------+----------+----------------------------+
|"10001"|1        |202106301221|0    |"系统"|"新手邮件"|"欢迎你,来到我们的虚拟世界"|
+-------+---------+------------+-----+------+----------+----------------------------+
1 records selected, select time: 41306 us

如果要列显示:

tcaplus> select * from mail where user_id = "10001" \\G;
user_id: "10001"
server_id: 1
mail_id: 202106301221
state: 0
from: "系统"
subject: "新手邮件"
content: "欢迎你,来到我们的虚拟世界"

1 records selected, select time: 5450 us

更新:

tcaplus> update mail set state = 1, from = "系统2"  where user_id = "10001" and server_id = 1 and mail_id=202106301221;
update success
update time: 7497 us

删除:

tcaplus> delete from mail where user_id = "10001" and server_id = 1 and mail_id=202106301221;                                                                        
delete success
delete time: 5367 us

如果要通过部分key 删除符合条件的记录,如下使用:

tcaplus> delete from mail where user_id = "10001" and server_id = 1 by partkey;
delete success
delete time: 8567 us

导出

tcaplus> dump * into mail.txt from mail limit 10;  

会导出到本目录下,打开后,内容如下:

[root@VM_0_13_centos bin]# cat mail.txt 
user_id,server_id,mail_id,state,from,subject,content
"10001",1,202106301221,0,"系统","新手邮件","欢迎你,来到我们的虚拟世界"

导入

tcaplus> load mail infile mail.txt;
loaded 1 records successful

索引查询
当开启全局索引功能后,TcaplusDB 将支持按照普通字段查询,查询条件中的字段必须是建立了全局索引的字段。
如果是聚合查询,那么聚合查询的字段也必须是建立了全局索引的字段。
一个索引查询请求,最多返回3000条记录。

支持的查询语句
条件查询
支持=, >, >=, <, <=, !=, between, in, not in, like, not like, and, or
聚合查询
当前支持的聚合查询包括:sum, count, max, min, avg,如:

以下两个指令要等到3.51.0才能支持
设置生命周期
毫秒级别

tcaplus> setttl  mail ttl=60000  where user_id = "10001" and server_id = 1 and mail_id="202106301221";   

读取生命周期

tcaplus> getttl from mail where user_id = "10001" and server_id = 1 and mail_id="202106301221";   

退出

tcaplus> exit

当你不知道某个指令如何使用时,可以用help 前缀看帮助内容:

help select

参考链接:游戏数据库TcaplusDB

总结

TcaplusDB是腾讯出品的分布式NoSQL数据库,存储和调度的代码完全自研。具备缓存+落地融合架构、PB级存储、毫秒级时延、无损水平扩展和复杂数据结构等特性。同时具备丰富的生态、便捷的迁移、极低的运维成本和五个九高可用等特点。客户覆盖游戏、互联网、政务、金融、制造和物联网等领域。

以上是关于Tcaplus使用总结的主要内容,如果未能解决你的问题,请参考以下文章

python常用代码片段总结

BootStrap有用代码片段(持续总结)

BootStrap实用代码片段(持续总结)

回归 | js实用代码片段的封装与总结(持续更新中...)

查看发票组代码后的总结和有感

Tailwind.css 体验总结