You need to enable JavaScript to run this app.
导航
代码示例
最近更新时间:2023.12.14 17:56:58首次发布时间:2023.01.17 12:49:01

本文提供 MongoDB Java SDK 的代码示例供您参考。

前提条件

  1. 下载安装 SDK
  2. 初始化客户端

设置请求超时时间

使用 Java SDK 调用 MongoDB 接口时,默认无超时时间设置。
您可以在初始化客户端时,通过 setConnectTimeout(${超时时间})(默认单位:毫秒) 来指定客户端的请求超时时间,若未在该时间内完成,请求即会终止并报超时错误。
例如,您可以通过如下代码将客户端的请求超时时间设置为 5000 毫秒。

public static ApiClient apiClient = new ApiClient()
        .setCredentials(Credentials.getCredentials(ak,sk))
        .setRegion(region).setConnectTimeout(5000);

创建 MongoDB 实例

关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstance

  • 示例 1
    使用如下示例代码创建 1 个分片集群实例,配置如下:

    • 计费方式:包年包月
    • 使用时长:1 个月,且开通自动续费
    • 数据库引擎版本:MongoDB 4.0
    • 实例所在可用区:华北 2(北京)- 可用区 A
    • 存储空间:100GiB
    • 节点规格:
      • 包含 2 个 Mongos 节点,规格均为 1 核 2GiB。
      • 包含 2 个 Shard 分片(每个分片默认包含 3 个节点),每个 Shard 中的节点规格均为 2 核 4GiB。
    import com.volcengine.volcengine.ApiClient;
    import com.volcengine.volcengine.ApiException;
    import com.volcengine.volcengine.mongodb.MongodbApi;
    import com.volcengine.volcengine.sign.Credentials;
    import com.volcengine.mongodb.model.*;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestMongodb {
        //设置 AKSK 验证信息及 Region
        public static String ak = "Your AK"
        public static String sk = "Your SK";
        public static String region = "Your Region";
    
        
        //创建一个 MongoDB SDK 客户端,后续可以利用此客户端调用 API 执行任务请求
        public static ApiClient apiClient = new ApiClient()
                .setCredentials(Credentials.getCredentials(ak,sk))
                .setRegion(region);
        public static MongodbApi mongoApi = new MongodbApi(apiClient);
        
        //创建一个接口入参
        public static void main() throws ApiException {
            CreateDBInstanceRequest request = new CreateDBInstanceRequest();   
            
            //根据 CreateDBInstance 接口的参数说明,设置接口入参
            request.setZoneId("cn-beijing-a");
            request.dbEngine(CreateDBInstanceRequest.DbEngineEnum.MONGODB);
            request.dbEngineVersion(CreateDBInstanceRequest.DbEngineVersionEnum._4_0);
            request.setNodeSpec("mongo.shard.2c4g");
            request.setInstanceType(CreateDBInstanceRequest.InstanceTypeEnum.SHARDEDCLUSTER);
            request.setMongosNodeNumber(2);
            request.setShardNumber(2);
            request.setMongosNodeSpec("mongo.mongos.1c2g");
            request.setStorageSpaceGB(100);
            request.vpcId("vpc-13f24t16guqdc3n6nu4k5****");
            request.setSubnetId("subnet-rr96lovl7f28v0x58tv****");
            request.setChargeType(CreateDBInstanceRequest.ChargeTypeEnum.PREPAID);
            request.setPeriodUnit(CreateDBInstanceRequest.PeriodUnitEnum.MONTH);
            request.setPeriod(1);
            request.setAutoRenew(Boolean.TRUE);
            request.setInstanceName("java_sdk_test_shardedcluster");
            
            //调用接口创建一个 MongoDB 实例
            try {
                CreateDBInstanceResponse response = mongoApi.createDBInstance(request);
                System.out.println(response);
            } catch (ApiException e) {
                System.out.println(e.getResponseBody());
            }
        }
     }
    
  • 示例 2
    使用如下示例代码创建 1 个副本集实例(默认包含 3 个节点),配置如下:

    • 计费方式:包年包月
    • 使用时长:1 个月,且开通自动续费
    • 数据库引擎版本:MongoDB 4.0
    • 实例所在可用区:华北 2(北京)- 可用区 A
    • 节点规格:1 核 2GiB
    • 存储空间:100GiB
    import com.volcengine.volcengine.ApiClient;
    import com.volcengine.volcengine.ApiException;
    import com.volcengine.volcengine.mongodb.MongodbApi;
    import com.volcengine.volcengine.sign.Credentials;
    import com.volcengine.mongodb.model.*;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TestMongodb {
        //设置 AKSK 验证信息及 Region
        public static String ak = "Your AK"
        public static String sk = "Your SK";
        public static String region = "Your Region";
        
        //创建一个 MongoDB SDK 客户端,后续可以利用此客户端调用 API 执行任务请求
        public static ApiClient apiClient = new ApiClient()
                .setCredentials(Credentials.getCredentials(ak,sk))
                .setRegion(region);
        public static MongodbApi mongoApi = new MongodbApi(apiClient);
        
        //创建一个接口入参
        public static void main() throws ApiException {
            CreateDBInstanceRequest request = new CreateDBInstanceRequest();   
            
            //根据 CreateDBInstance 接口的参数说明,设置接口入参
            request.setZoneId("cn-beijing-a");
            request.dbEngine(CreateDBInstanceRequest.DbEngineEnum.MONGODB);
            request.dbEngineVersion(CreateDBInstanceRequest.DbEngineVersionEnum._4_0);
            request.setNodeSpec("mongo.1c2g");
            request.setInstanceType(CreateDBInstanceRequest.InstanceTypeEnum.REPLICASET);
            request.setStorageSpaceGB(100);
            request.vpcId("vpc-13f24t16guqdc3n6nu4k5****");
            request.setSubnetId("subnet-rr96lovl7f28v0x58tv****");
            request.setChargeType(CreateDBInstanceRequest.ChargeTypeEnum.PREPAID);
            request.setPeriodUnit(CreateDBInstanceRequest.PeriodUnitEnum.MONTH);
            request.setPeriod(1);
            request.setAutoRenew(Boolean.TRUE);
            request.setInstanceName("java_sdk_test_replica");
            
            //调用接口创建一个 MongoDB 实例
            try {
                CreateDBInstanceResponse response = mongoApi.createDBInstance(request);
                System.out.println(response);
            } catch (ApiException e) {
                System.out.println(e.getResponseBody());
            }
        }
     }
    

查询 MongoDB 实例信息

说明

关于下述 SDK 请求中所使用参数的定义、取值范围等信息,请参见 DescribeDBInstanceDetail

import com.volcengine.volcengine.ApiClient;
import com.volcengine.volcengine.ApiException;
import com.volcengine.volcengine.mongodb.MongodbApi;
import com.volcengine.volcengine.sign.Credentials;
import com.volcengine.mongodb.model.*;

import java.util.ArrayList;
import java.util.List;

public class TestMongodb {
    //设置 AKSK 验证信息及 Region
    public static String ak = "Your AK"
    public static String sk = "Your SK";
    public static String region = "Your Region";
    
    //创建一个 MongoDB SDK 客户端,后续可以利用此客户端调用 API 执行任务请求
    public static ApiClient apiClient = new ApiClient()
            .setCredentials(Credentials.getCredentials(ak,sk))
            .setRegion(region);
    public static MongodbApi mongoApi = new MongodbApi(apiClient);

    //创建一个接口入参
    public static void main() throws ApiException {
        DescribeDBInstanceDetailRequest request = new DescribeDBInstanceDetailRequest();   
        
        //根据 DescribeDBInstanceDetail 接口的参数说明,设置接口入参
        request.setInstanceId("mongo-replica-e649ae42****");
       
        //调用接口查询指定 MongoDB 实例详细信息
        try {
            DescribeDBInstanceDetailResponse response = mongoApi.describeDBInstanceDetail(request);
            System.out.println(response);
        } catch (ApiException e) {
            System.out.println(e.getResponseBody());
        }
    }
 }