Class CosmosClientBuilder
- All Implemented Interfaces:
AzureKeyCredentialTrait<CosmosClientBuilder>
,EndpointTrait<CosmosClientBuilder>
,TokenCredentialTrait<CosmosClientBuilder>
CosmosAsyncClient
and CosmosClient CosmosClient
instances as logical representation of the Azure Cosmos database service.
When building client, endpoint() and key() are mandatory APIs, without these the initialization will fail.
Though consistencyLevel is not mandatory, but we strongly suggest to pay attention to this API when building client. By default, account consistency level is used if none is provided.
By default, direct connection mode is used if none specified.
Building Cosmos Async Client minimal APIs (without any customized configurations)
CosmosAsyncClient client = new CosmosClientBuilder()
.endpoint(serviceEndpoint)
.key(key)
.buildAsyncClient();
Building Cosmos Async Client with customizations
CosmosAsyncClient client = new CosmosClientBuilder()
.endpoint(serviceEndpoint)
.key(key)
.directMode(directConnectionConfig, gatewayConnectionConfig)
.consistencyLevel(ConsistencyLevel.SESSION)
.connectionSharingAcrossClientsEnabled(true)
.contentResponseOnWriteEnabled(true)
.userAgentSuffix("my-application1-client")
.preferredRegions(Collections.singletonList("West US", "East US"))
.buildAsyncClient();
Building Cosmos Sync Client minimal APIs (without any customized configurations)
CosmosClient client = new CosmosClientBuilder()
.endpoint(serviceEndpoint)
.key(key)
.buildClient();
Building Cosmos Sync Client with customizations
CosmosClient client = new CosmosClientBuilder()
.endpoint(serviceEndpoint)
.key(key)
.directMode(directConnectionConfig, gatewayConnectionConfig)
.consistencyLevel(ConsistencyLevel.SESSION)
.connectionSharingAcrossClientsEnabled(true)
.contentResponseOnWriteEnabled(true)
.userAgentSuffix("my-application1-client")
.preferredRegions(Collections.singletonList("West US", "East US"))
.buildClient();
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionauthorizationTokenResolver
(CosmosAuthorizationTokenResolver cosmosAuthorizationTokenResolver) Sets the token resolverBuilds a cosmos async client with the provided propertiesBuilds a cosmos sync client with the provided propertiesclientTelemetryEnabled
(boolean clientTelemetryEnabled) Sets the flag to enable client telemetry which will periodically collect database operations aggregation statistics, system information like cpu/memory and send it to cosmos monitoring service, which will be helpful during debugging.connectionSharingAcrossClientsEnabled
(boolean connectionSharingAcrossClientsEnabled) Enables connections sharing across multiple Cosmos Clients.consistencyLevel
(ConsistencyLevel desiredConsistencyLevel) Sets theConsistencyLevel
to be used By default,ConsistencyLevel.SESSION
consistency will be used.contentResponseOnWriteEnabled
(boolean contentResponseOnWriteEnabled) Sets the boolean to only return the headers and status code in Cosmos DB response in case of Create, Update and Delete operations on CosmosItem.credential
(AzureKeyCredential credential) Sets theAzureKeyCredential
to be usedcredential
(TokenCredential credential) Sets theTokenCredential
used to authorize requests sent to the service.Sets the default DIRECT connection configuration to be used.directMode
(DirectConnectionConfig directConnectionConfig) Sets the DIRECT connection configuration to be used.directMode
(DirectConnectionConfig directConnectionConfig, GatewayConnectionConfig gatewayConnectionConfig) Sets the DIRECT connection configuration to be used.Sets the Azure Cosmos DB endpoint the SDK will connect toendpointDiscoveryEnabled
(boolean endpointDiscoveryEnabled) Sets the flag to enable endpoint discovery for geo-replicated database accounts.Sets the default GATEWAY connection configuration to be used.gatewayMode
(GatewayConnectionConfig gatewayConnectionConfig) Sets the GATEWAY connection configuration to be used.Sets either a master or readonly key used to perform authentication for accessing resource.multipleWriteRegionsEnabled
(boolean multipleWriteRegionsEnabled) Sets the flag to enable writes on any regions for geo-replicated database accounts in the Azure Cosmos DB service.permissions
(List<CosmosPermissionProperties> permissions) Sets the permission list, which contains the resource tokens needed to access resources.preferredRegions
(List<String> preferredRegions) Sets the preferred regions for geo-replicated database accounts.readRequestsFallbackEnabled
(boolean readRequestsFallbackEnabled) Sets whether to allow for reads to go to multiple regions configured on an account of Azure Cosmos DB service.resourceToken
(String resourceToken) Sets a resource token used to perform authentication for accessing resource.sessionCapturingOverrideEnabled
(boolean sessionCapturingOverrideEnabled) Session capturing is enabled by default forConsistencyLevel.SESSION
.throttlingRetryOptions
(ThrottlingRetryOptions throttlingRetryOptions) Sets the retry policy options associated with the DocumentClient instance.userAgentSuffix
(String userAgentSuffix) sets the value of the user-agent suffix.
-
Constructor Details
-
CosmosClientBuilder
public CosmosClientBuilder()Instantiates a new Cosmos client builder.
-
-
Method Details
-
sessionCapturingOverrideEnabled
Session capturing is enabled by default forConsistencyLevel.SESSION
. For other consistency levels, it is not needed, unless if you need occasionally send requests with Session Consistency while the client is not configured in session.enabling Session capturing for Session mode has no effect.
- Parameters:
sessionCapturingOverrideEnabled
- session capturing override- Returns:
- current cosmosClientBuilder
-
connectionSharingAcrossClientsEnabled
public CosmosClientBuilder connectionSharingAcrossClientsEnabled(boolean connectionSharingAcrossClientsEnabled) Enables connections sharing across multiple Cosmos Clients. The default is false.CosmosAsyncClient client1 = new CosmosClientBuilder() .endpoint(serviceEndpoint1) .key(key1) .consistencyLevel(ConsistencyLevel.SESSION) .connectionSharingAcrossClientsEnabled(true) .buildAsyncClient(); CosmosAsyncClient client2 = new CosmosClientBuilder() .endpoint(serviceEndpoint2) .key(key2) .consistencyLevel(ConsistencyLevel.SESSION) .connectionSharingAcrossClientsEnabled(true) .buildAsyncClient(); // when configured this way client1 and client2 will share connections when possible.
- Parameters:
connectionSharingAcrossClientsEnabled
- connection sharing- Returns:
- current cosmosClientBuilder
-
authorizationTokenResolver
@Beta(value=V4_24_0, warningText="Preview API - subject to change in non-backwards compatible way") public CosmosClientBuilder authorizationTokenResolver(CosmosAuthorizationTokenResolver cosmosAuthorizationTokenResolver) Sets the token resolver- Parameters:
cosmosAuthorizationTokenResolver
- the token resolver- Returns:
- current cosmosClientBuilder
-
endpoint
Sets the Azure Cosmos DB endpoint the SDK will connect to- Specified by:
endpoint
in interfaceEndpointTrait<CosmosClientBuilder>
- Parameters:
endpoint
- the service endpoint- Returns:
- current Builder
-
key
Sets either a master or readonly key used to perform authentication for accessing resource.- Parameters:
key
- master or readonly key- Returns:
- current Builder.
-
resourceToken
Sets a resource token used to perform authentication for accessing resource.- Parameters:
resourceToken
- resourceToken for authentication- Returns:
- current Builder.
-
credential
Sets theTokenCredential
used to authorize requests sent to the service. Refer to the Azure SDK for Java identity and authentication documentation for more details on proper usage of theTokenCredential
type.- Specified by:
credential
in interfaceTokenCredentialTrait<CosmosClientBuilder>
- Parameters:
credential
-TokenCredential
used to authorize requests sent to the service.- Returns:
- the updated CosmosClientBuilder
- Throws:
NullPointerException
- Ifcredential
isnull
.
-
permissions
Sets the permission list, which contains the resource tokens needed to access resources.- Parameters:
permissions
- Permission list for authentication.- Returns:
- current Builder.
-
consistencyLevel
Sets theConsistencyLevel
to be used By default,ConsistencyLevel.SESSION
consistency will be used.- Parameters:
desiredConsistencyLevel
-ConsistencyLevel
- Returns:
- current Builder
-
credential
Sets theAzureKeyCredential
to be used- Specified by:
credential
in interfaceAzureKeyCredentialTrait<CosmosClientBuilder>
- Parameters:
credential
-AzureKeyCredential
- Returns:
- current cosmosClientBuilder
-
contentResponseOnWriteEnabled
Sets the boolean to only return the headers and status code in Cosmos DB response in case of Create, Update and Delete operations on CosmosItem. If set to false (which is by default), service doesn't return payload in the response. It reduces networking and CPU load by not sending the payload back over the network and serializing it on the client. This feature does not impact RU usage for read or write operations. By-default, this is false.- Parameters:
contentResponseOnWriteEnabled
- a boolean indicating whether payload will be included in the response or not- Returns:
- current cosmosClientBuilder
-
gatewayMode
Sets the default GATEWAY connection configuration to be used.- Returns:
- current CosmosClientBuilder
-
gatewayMode
Sets the GATEWAY connection configuration to be used.- Parameters:
gatewayConnectionConfig
- gateway connection configuration- Returns:
- current CosmosClientBuilder
-
directMode
Sets the default DIRECT connection configuration to be used. By default, the builder is initialized with directMode()- Returns:
- current CosmosClientBuilder
-
directMode
Sets the DIRECT connection configuration to be used. By default, the builder is initialized with directMode()- Parameters:
directConnectionConfig
- direct connection configuration- Returns:
- current CosmosClientBuilder
-
directMode
public CosmosClientBuilder directMode(DirectConnectionConfig directConnectionConfig, GatewayConnectionConfig gatewayConnectionConfig) Sets the DIRECT connection configuration to be used. gatewayConnectionConfig - represents basic configuration to be used for gateway client. Even in direct connection mode, some of the meta data operations go through gateway client, Setting gateway connection config in this API doesn't affect the connection mode, which will be Direct in this case.- Parameters:
directConnectionConfig
- direct connection configuration to be usedgatewayConnectionConfig
- gateway connection configuration to be used- Returns:
- current CosmosClientBuilder
-
userAgentSuffix
sets the value of the user-agent suffix.- Parameters:
userAgentSuffix
- The value to be appended to the user-agent header, this is used for monitoring purposes.- Returns:
- current CosmosClientBuilder
-
throttlingRetryOptions
Sets the retry policy options associated with the DocumentClient instance.Properties in the RetryOptions class allow application to customize the built-in retry policies. This property is optional. When it's not set, the SDK uses the default values for configuring the retry policies. See RetryOptions class for more details.
- Parameters:
throttlingRetryOptions
- the RetryOptions instance.- Returns:
- current CosmosClientBuilder
- Throws:
IllegalArgumentException
- thrown if an error occurs
-
preferredRegions
Sets the preferred regions for geo-replicated database accounts. For example, "East US" as the preferred region.When EnableEndpointDiscovery is true and PreferredRegions is non-empty, the SDK will prefer to use the regions in the container in the order they are specified to perform operations.
If EnableEndpointDiscovery is set to false, this property is ignored.
- Parameters:
preferredRegions
- the list of preferred regions.- Returns:
- current CosmosClientBuilder
-
endpointDiscoveryEnabled
Sets the flag to enable endpoint discovery for geo-replicated database accounts.When EnableEndpointDiscovery is true, the SDK will automatically discover the current write and read regions to ensure requests are sent to the correct region based on the capability of the region and the user's preference.
The default value for this property is true indicating endpoint discovery is enabled.
- Parameters:
endpointDiscoveryEnabled
- true if EndpointDiscovery is enabled.- Returns:
- current CosmosClientBuilder
-
multipleWriteRegionsEnabled
Sets the flag to enable writes on any regions for geo-replicated database accounts in the Azure Cosmos DB service.When the value of this property is true, the SDK will direct write operations to available writable regions of geo-replicated database account. Writable regions are ordered by PreferredRegions property. Setting the property value to true has no effect until EnableMultipleWriteRegions in DatabaseAccount is also set to true.
DEFAULT value is true indicating that writes are directed to available writable regions of geo-replicated database account.
- Parameters:
multipleWriteRegionsEnabled
- flag to enable writes on any regions for geo-replicated database accounts.- Returns:
- current CosmosClientBuilder
-
clientTelemetryEnabled
Sets the flag to enable client telemetry which will periodically collect database operations aggregation statistics, system information like cpu/memory and send it to cosmos monitoring service, which will be helpful during debugging.DEFAULT value is false indicating this is opt in feature, by default no telemetry collection.
- Parameters:
clientTelemetryEnabled
- flag to enable client telemetry.- Returns:
- current CosmosClientBuilder
-
readRequestsFallbackEnabled
Sets whether to allow for reads to go to multiple regions configured on an account of Azure Cosmos DB service.DEFAULT value is true.
If this property is not set, the default is true for all Consistency Levels other than Bounded Staleness, The default is false for Bounded Staleness. 1.
endpointDiscoveryEnabled
is true 2. the Azure Cosmos DB account has more than one region- Parameters:
readRequestsFallbackEnabled
- flag to enable reads to go to multiple regions configured on an account of Azure Cosmos DB service.- Returns:
- current CosmosClientBuilder
-
buildAsyncClient
Builds a cosmos async client with the provided properties- Returns:
- CosmosAsyncClient
-
buildClient
Builds a cosmos sync client with the provided properties- Returns:
- CosmosClient
-