本文提供 MongoDB Java SDK 的代码示例供您参考。
使用 Java SDK 调用 MongoDB 接口时,默认无超时时间设置。
您可以在初始化客户端时,通过 setConnectTimeout(${超时时间})
(默认单位:毫秒) 来指定客户端的请求超时时间,若未在该时间内完成,请求即会终止并报超时错误。
例如,您可以通过如下代码将客户端的请求超时时间设置为 5000 毫秒。
public static ApiClient apiClient = new ApiClient() .setCredentials(Credentials.getCredentials(ak,sk)) .setRegion(region).setConnectTimeout(5000);
关于下述 SDK 代码中所使用参数的定义、取值范围等信息,请参见 CreateDBInstance。
示例 1
使用如下示例代码创建 1 个分片集群实例,配置如下:
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 个节点),配置如下:
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()); } } }
说明
关于下述 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()); } } }