Alias 操作
更新时间:2026-09-01
设置别名
功能介绍
为指定的数据表新增一个别名。C++ SDK 把别名接口挂在 mochow::Database 对象上,库名由 Database 对象自身携带,因此调用时只需要传入表名和别名。
请求示例
C++
1#include <iostream>
2#include <memory>
3#include <string>
4
5#include "mochow/Mochow.h"
6
7int main() {
8 const std::string account = "root";
9 const std::string api_key = "$您的账户API密钥";
10 const std::string endpoint = "$您的实例访问端点"; // 例如:http://127.0.0.1:5287
11
12 mochow::ClientOptions options;
13 options.endpoint = endpoint;
14 options.credentials.account = account;
15 options.credentials.api_key = api_key;
16
17 // create mochow client
18 auto client_result = mochow::MochowClient::Create(options);
19 if (!client_result.IsOk()) {
20 std::cerr << "Fail to init mochow client due to error: "
21 << client_result.GetStatus().Message() << std::endl;
22 return 1;
23 }
24 std::shared_ptr<mochow::MochowClient> client = client_result.MoveValue();
25
26 // create alias
27 mochow::Database database = client->GetDatabase("db_test");
28 mochow::Status status = database.AliasTable("book_vector", "alias_book_vector");
29 if (!status.IsOk()) {
30 std::cerr << "Fail to alias table due to error: " << status.Message()
31 << ", request_id=" << status.RequestId() << std::endl;
32 (void)client->Close();
33 return 1;
34 }
35
36 (void)client->Close();
37 return 0;
38}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| table_name | const std::string& | 是 | 表名。SDK 侧会校验该参数非空,为空时直接返回 InvalidArgument。 |
| alias | const std::string& | 是 | 表的别名。需要注意的是,在一个库的内部,别名和表名都是库级别唯一的。SDK 侧会校验该参数非空,为空时直接返回 InvalidArgument。 |
| options | const mochow::RequestOptions& | 否 | 单次调用的请求配置,可设置 request id、超时时间、幂等键和重试策略。不传时使用客户端级别的默认配置。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| status | mochow::Status | 本次调用的结果。IsOk() 为 true 表示别名设置成功;否则可通过 Code()、Message()、ServerCode() 和 RequestId() 定位失败原因。 |
删除别名
功能介绍
删除指定数据表的指定别名。
请求示例
C++
1#include <iostream>
2#include <memory>
3#include <string>
4
5#include "mochow/Mochow.h"
6
7int main() {
8 const std::string account = "root";
9 const std::string api_key = "$您的账户API密钥";
10 const std::string endpoint = "$您的实例访问端点"; // 例如:http://127.0.0.1:5287
11
12 mochow::ClientOptions options;
13 options.endpoint = endpoint;
14 options.credentials.account = account;
15 options.credentials.api_key = api_key;
16
17 // create mochow client
18 auto client_result = mochow::MochowClient::Create(options);
19 if (!client_result.IsOk()) {
20 std::cerr << "Fail to init mochow client due to error: "
21 << client_result.GetStatus().Message() << std::endl;
22 return 1;
23 }
24 std::shared_ptr<mochow::MochowClient> client = client_result.MoveValue();
25
26 // drop alias
27 mochow::Database database = client->GetDatabase("db_test");
28 mochow::Status status =
29 database.UnaliasTable("book_vector", "alias_book_vector");
30 if (!status.IsOk()) {
31 std::cerr << "Fail to unalias table due to error: " << status.Message()
32 << ", request_id=" << status.RequestId() << std::endl;
33 (void)client->Close();
34 return 1;
35 }
36
37 (void)client->Close();
38 return 0;
39}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| table_name | const std::string& | 是 | 表名。SDK 侧会校验该参数非空,为空时直接返回 InvalidArgument。 |
| alias | const std::string& | 是 | 待删除的表别名。需要注意的是,在一个库的内部,别名和表名都是库级别唯一的。SDK 侧会校验该参数非空,为空时直接返回 InvalidArgument。 |
| options | const mochow::RequestOptions& | 否 | 单次调用的请求配置,可设置 request id、超时时间、幂等键和重试策略。不传时使用客户端级别的默认配置。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| status | mochow::Status | 本次调用的结果。IsOk() 为 true 表示别名删除成功;否则可通过 Code()、Message()、ServerCode() 和 RequestId() 定位失败原因。 |
设置并删除别名的完整示例
功能介绍
别名的设置与删除通常成对出现。下面示例演示为已有数据表设置别名,再将该别名删除,并为单次调用指定 request id,便于在业务日志和服务端日志之间关联请求。
请求示例
C++
1#include <iostream>
2#include <memory>
3#include <string>
4
5#include "mochow/Mochow.h"
6
7int main() {
8 const std::string account = "root";
9 const std::string api_key = "$您的账户API密钥";
10 const std::string endpoint = "$您的实例访问端点"; // 例如:http://127.0.0.1:5287
11
12 mochow::ClientOptions options;
13 options.endpoint = endpoint;
14 options.credentials.account = account;
15 options.credentials.api_key = api_key;
16
17 auto client_result = mochow::MochowClient::Create(options);
18 if (!client_result.IsOk()) {
19 std::cerr << "Fail to init mochow client due to error: "
20 << client_result.GetStatus().Message() << std::endl;
21 return 1;
22 }
23 std::shared_ptr<mochow::MochowClient> client = client_result.MoveValue();
24
25 mochow::Database database = client->GetDatabase("db_test");
26 const std::string table_name = "book_vector";
27 const std::string alias_name = "alias_book_vector";
28
29 mochow::RequestOptions call_options;
30 call_options.WithRequestId("alias-demo-req-001").WithRequestTimeoutMs(5000);
31
32 mochow::Status status =
33 database.AliasTable(table_name, alias_name, call_options);
34 if (!status.IsOk()) {
35 std::cerr << "Fail to alias table due to error: " << status.Message()
36 << ", request_id=" << status.RequestId() << std::endl;
37 (void)client->Close();
38 return 1;
39 }
40 std::cout << "alias created: " << alias_name << std::endl;
41
42 status = database.UnaliasTable(table_name, alias_name, call_options);
43 if (!status.IsOk()) {
44 std::cerr << "Fail to unalias table due to error: " << status.Message()
45 << ", request_id=" << status.RequestId() << std::endl;
46 (void)client->Close();
47 return 1;
48 }
49 std::cout << "alias dropped: " << alias_name << std::endl;
50
51 (void)client->Close();
52 return 0;
53}
请求参数
| 参数 | 参数类型 | 是否必选 | 参数配置 |
|---|---|---|---|
| table_name | const std::string& | 是 | 表名。 |
| alias | const std::string& | 是 | 表的别名。 |
| options | const mochow::RequestOptions& | 否 | 单次调用的请求配置。可用的链式方法:WithRequestId(std::string):指定本次调用的 request id;WithRequestTimeoutMs(uint64_t):指定本次调用的超时时间,单位为毫秒;WithIdempotencyKey(std::string):指定幂等键;WithRetry(mochow::RetryOptions):覆盖客户端级别的重试策略。 |
返回参数
| 参数 | 参数类型 | 参数含义 |
|---|---|---|
| status | mochow::Status | 每次调用的结果。别名接口不返回业务数据,失败时可通过 RequestId() 关联服务端日志。 |
评价此篇文章
