2 Star 0 Fork 0

HuaweiCloudDeveloper/huaweicloud-rdsforPostgresql-java

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

1. 介绍

华为云关系型数据库(Relational Database Service,简称RDS)是一种基于云计算平台的即开即用、稳定可靠、弹性伸缩、便捷管理的在线关系型数据库服务。 本示例展示如何通过接口调用方式,创建RDS。

2. 前置条件

1.已 注册 华为云,并完成 实名认证

2.获取华为云开发工具包(SDK),您也可以查看安装JAVA SDK。

3.已获取华为云账号对应的Access Key(AK)和Secret Access Key(SK)。请在华为云控制台“我的凭证 > 访问密钥”页面上创建和查看您的AK/SK。具体请参见 访问密钥

4.已具备开发环境 ,支持Java JDK 1.8及其以上版本。

3. SDK获取和安装

您可以通过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>

4.关键代码片段

以下代码展示如何使用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());
        }
    }

}

5.返回结果示例

  • 查询数据库用户(ListPostgresqlDbUserPaginated)接口的返回值:
{
    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
}
  • 创建数据库用户(createPostgresqlDbUser)接口的返回值:
class CreatePostgresqlDbUserResponse {
    "resp": "successful"
}
  • 查看数据库用户(ListPostgresqlDbUserPaginated)接口的返回值:
{
    "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
}

6.参考链接

请见 查询数据库用户API 您可以在 API Explorer 中直接运行调试该接口。

请见 创建数据库用户API 您可以在 API Explorer 中直接运行调试该接口。

修订记录

发布日期 文档版本 修订说明
2022-10-30 1.0 文档首次发布

空文件

简介

新增 SDK RdsForPostgresql产品代码示例上架申请 数据库相关代码场景 1、查看数据库用户列表 2、创建数据库用户 3、查看数据库用户列表 展开 收起
取消

发行版

暂无发行版

贡献者

全部

语言

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/HuaweiCloudDeveloper/huaweicloud-rdsfor-postgresql-java.git
git@gitee.com:HuaweiCloudDeveloper/huaweicloud-rdsfor-postgresql-java.git
HuaweiCloudDeveloper
huaweicloud-rdsfor-postgresql-java
huaweicloud-rdsforPostgresql-java
master-dev

搜索帮助