代码拉取完成,页面将自动刷新
华为云关系型数据库(Relational Database Service,简称RDS)是一种基于云计算平台的即开即用、稳定可靠、弹性伸缩、便捷管理的在线关系型数据库服务。 本示例展示如何通过接口调用方式,创建RDS。
2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。
3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥 。
4.已具备开发环境 ,支持Java JDK 1.8及其以上版本。
您可以通过Maven方式获取和安装SDK,首先需要在您的操作系统中下载并安装Maven ,安装完成后您只需要在Java项目的pom.xml文件中加入相应的依赖项即可。
具体的SDK版本号请参见 SDK开发中心 。
<dependency>
<groupId>com.huaweicloud.sdk</groupId>
<artifactId>huaweicloud-sdk-rds</artifactId>
<version>3.1.5</version>
</dependency>
以下代码展示如何使用SDK查询和创建数据库用户:
public class RdsDbUserDemo {
private static final Logger logger = LoggerFactory.getLogger(RdsDbUserDemo.class.getName());
public static void main(String[] args) {
// 认证用的ak和sk直接写到代码中有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
// 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
String ak = System.getenv("HUAWEICLOUD_SDK_AK");
String sk = System.getenv("HUAWEICLOUD_SDK_SK");
String regionId = "<YOUR REGION_ID>";
String instanceId = "<YOUR INSTANCE_ID>";
String name = "<YOUR NAME>";
String password = "<YOUR PASSWORD>";
int page = 1; // <YOUR PAGE>
int limit = 10; // <YOUR LIMIT>
RdsClient client = createClient(ak, sk);
// 查看数据库用户列表
listDatabaseUser(client, instanceId, page, limit);
// 创建数据库用户
createDatabaseUser(client, instanceId, name, password);
// 查看数据库用户列表
listDatabaseUser(client, instanceId, page, limit);
}
private static RdsClient createClient(String ak, String sk) {
ICredential auth = new BasicCredentials().withAk(ak).withSk(sk);
return RdsClient.newBuilder().withCredential(auth).withRegion(RdsRegion.CN_EAST_3).build();
}
private static void listDatabaseUser(RdsClient client, String instanceId, int page, int limit) {
ListPostgresqlDbUserPaginatedRequest request = new ListPostgresqlDbUserPaginatedRequest();
request.withInstanceId(instanceId);
request.withPage(page);
request.withLimit(limit);
try {
ListPostgresqlDbUserPaginatedResponse response = client.listPostgresqlDbUserPaginated(request);
logger.info(response.toString());
} catch (ConnectionException e) {
logger.error("ConnectionException", e);
} catch (RequestTimeoutException e) {
logger.error("RequestTimeoutException", e);
} catch (ServiceResponseException e) {
logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(),
e.getErrorMsg());
}
}
private static void createDatabaseUser(RdsClient client, String instanceId, String name, String password) {
CreatePostgresqlDbUserRequest request = new CreatePostgresqlDbUserRequest();
request.withInstanceId(instanceId);
PostgresqlUserForCreation body = new PostgresqlUserForCreation();
body.withName(name);
body.withPassword(password);
request.withBody(body);
try {
CreatePostgresqlDbUserResponse response = client.createPostgresqlDbUser(request);
logger.info(response.toString());
} catch (ConnectionException e) {
logger.error("ConnectionException", e);
} catch (RequestTimeoutException e) {
logger.error("RequestTimeoutException", e);
} catch (ServiceResponseException e) {
logger.error("httpStatusCode: {}, errorCode: {}, errorMsg: {}", e.getHttpStatusCode(), e.getErrorCode(),
e.getErrorMsg());
}
}
}
{
users: [
{
"name": "user_test_1",
"attributes": {
"rolsuper": false,
"rolinherit": true,
"rolcreaterole": true,
"rolcreatedb": true,
"rolcanlogin": true,
"rolconnlimit": -1,
"rolreplication": true,
"rolbypassrls": false
},
"memberof": ["pg_monitor", "pg_read_all_stats", "pg_stat_scan_tables", "pg_signal_backend"]
}]
"totalCount": 1
}
class CreatePostgresqlDbUserResponse {
"resp": "successful"
}
{
"users": [
{
"name": "user_test_1",
"attributes": {
"rolsuper": false,
"rolinherit": true,
"rolcreaterole": true,
"rolcreatedb": true,
"rolcanlogin": true,
"rolconnlimit": -1,
"rolreplication": true,
"rolbypassrls": false
},
"memberof": ["pg_monitor", "pg_read_all_stats", "pg_stat_scan_tables", "pg_signal_backend"]
},
{
"name": "user_test_2",
"attributes": {
"rolsuper": false,
"rolinherit": true,
"rolcreaterole": true,
"rolcreatedb": true,
"rolcanlogin": true,
"rolconnlimit": -1,
"rolreplication": true,
"rolbypassrls": false
},
"memberof": []
}],
"totalCount": 2
}
请见 查询数据库用户API 您可以在 API Explorer 中直接运行调试该接口。
请见 创建数据库用户API 您可以在 API Explorer 中直接运行调试该接口。
发布日期 | 文档版本 | 修订说明 |
---|---|---|
2022-10-30 | 1.0 | 文档首次发布 |
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。