com.azure.cosmos.CosmosClient Maven / Gradle / Ivy
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.cosmos;
import com.azure.core.annotation.ServiceClient;
import com.azure.cosmos.implementation.ImplementationBridgeHelpers;
import com.azure.cosmos.models.CosmosContainerIdentity;
import com.azure.cosmos.models.CosmosDatabaseProperties;
import com.azure.cosmos.models.CosmosDatabaseRequestOptions;
import com.azure.cosmos.models.CosmosDatabaseResponse;
import com.azure.cosmos.models.CosmosQueryRequestOptions;
import com.azure.cosmos.models.SqlQuerySpec;
import com.azure.cosmos.models.ThroughputProperties;
import com.azure.cosmos.util.CosmosPagedFlux;
import com.azure.cosmos.util.CosmosPagedIterable;
import reactor.core.Exceptions;
import reactor.core.publisher.Mono;
import java.io.Closeable;
import java.time.Duration;
import java.util.List;
/**
* Provides a client-side logical representation of the Azure Cosmos DB service.
* Calls to CosmosClient API's are blocked for completion.
*
* CosmosClient is thread-safe.
* It's recommended to maintain a single instance of CosmosClient per lifetime of the application which enables efficient connection management and performance.
* CosmosClient initialization is a heavy operation - don't use initialization CosmosClient instances as credentials or network connectivity validations.
*/
@ServiceClient(builder = CosmosClientBuilder.class)
public final class CosmosClient implements Closeable {
private final CosmosAsyncClient asyncClientWrapper;
CosmosClient(CosmosClientBuilder builder) {
this.asyncClientWrapper = builder.buildAsyncClient(false);
}
/**
* Create a Cosmos database if it does not already exist on the service.
*
*
* CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
* cosmosClient.createDatabaseIfNotExists(databaseProperties);
*
*
* @param databaseProperties {@link CosmosDatabaseProperties} the database properties.
* @return the {@link CosmosDatabaseResponse} with the created database.
*/
CosmosDatabaseResponse createDatabaseIfNotExists(CosmosDatabaseProperties databaseProperties) {
return blockDatabaseResponse(asyncClientWrapper.createDatabaseIfNotExists(databaseProperties));
}
/**
* Create a Cosmos database if it does not already exist on the service.
*
*
* ThroughputProperties throughputProperties = ThroughputProperties
* .createAutoscaledThroughput(autoScaleMaxThroughput);
* cosmosClient.createDatabaseIfNotExists(databaseName, throughputProperties);
*
*
* The throughputProperties will only be used if the specified database
* does not exist and therefor a new database will be created with throughputProperties.
*
* @param id the id of the database.
* @param throughputProperties the throughputProperties.
* @return the {@link CosmosDatabaseResponse} with the created database.
*/
public CosmosDatabaseResponse createDatabaseIfNotExists(String id, ThroughputProperties throughputProperties) {
return blockDatabaseResponse(asyncClientWrapper.createDatabaseIfNotExists(id, throughputProperties));
}
/**
* Create a Cosmos database if it does not already exist on the service.
*
*
* CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
* cosmosClient.createDatabaseIfNotExists(databaseProperties);
*
*
* @param id the id of the database.
* @return the {@link CosmosDatabaseResponse} with the created database.
*/
public CosmosDatabaseResponse createDatabaseIfNotExists(String id) {
return blockDatabaseResponse(asyncClientWrapper.createDatabaseIfNotExists(id));
}
/**
* Creates a database.
*
*
* CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
* cosmosClient.createDatabase(databaseProperties);
*
*
* @param databaseProperties {@link CosmosDatabaseProperties} the database properties.
* @param options the request options.
* @return the {@link CosmosDatabaseResponse} with the created database.
* @throws CosmosException if resource with specified id already exists
*/
public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties,
CosmosDatabaseRequestOptions options) {
return blockDatabaseResponse(asyncClientWrapper.createDatabase(databaseProperties, options));
}
/**
* Creates a Cosmos database.
*
*
* CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
* cosmosClient.createDatabase(databaseProperties);
*
*
* @param databaseProperties {@link CosmosDatabaseProperties} the database properties.
* @return the {@link CosmosDatabaseResponse} with the created database.
* @throws CosmosException if resource with specified id already exists
*/
public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties) {
return blockDatabaseResponse(asyncClientWrapper.createDatabase(databaseProperties));
}
/**
* Creates a Cosmos database.
*
*
* CosmosDatabaseProperties databaseProperties = new CosmosDatabaseProperties(databaseName);
* cosmosClient.createDatabase(databaseProperties);
*
*
* @param id the id of the database.
* @return the {@link CosmosDatabaseResponse} with the created database.
* @throws CosmosException if resource with specified id already exists
*/
public CosmosDatabaseResponse createDatabase(String id) {
return blockDatabaseResponse(asyncClientWrapper.createDatabase(id));
}
/**
* Creates a Cosmos database.
*
*
* ThroughputProperties throughputProperties = ThroughputProperties
* .createAutoscaledThroughput(autoScaleMaxThroughput);
* cosmosClient.createDatabase(databaseName, throughputProperties);
*
*
* @param databaseProperties {@link CosmosDatabaseProperties} the database properties.
* @param throughputProperties the throughput properties.
* @param options {@link CosmosDatabaseRequestOptions} the request options.
* @return the {@link CosmosDatabaseResponse} with the created database.
* @throws CosmosException if resource with specified id already exists
*/
public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties,
ThroughputProperties throughputProperties,
CosmosDatabaseRequestOptions options) {
return blockDatabaseResponse(asyncClientWrapper.createDatabase(databaseProperties, throughputProperties, options));
}
/**
* Creates a Cosmos database.
*
*
* ThroughputProperties throughputProperties = ThroughputProperties
* .createAutoscaledThroughput(autoScaleMaxThroughput);
* cosmosClient.createDatabase(databaseName, throughputProperties);
*
*
* @param databaseProperties {@link CosmosDatabaseProperties} the database properties.
* @param throughputProperties the throughput properties.
* @return the {@link CosmosDatabaseResponse} with the created database.
* @throws CosmosException if resource with specified id already exists
*/
public CosmosDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties,
ThroughputProperties throughputProperties) {
return blockDatabaseResponse(asyncClientWrapper.createDatabase(databaseProperties, throughputProperties));
}
/**
* Creates a Cosmos database.
*
*
* ThroughputProperties throughputProperties = ThroughputProperties
* .createAutoscaledThroughput(autoScaleMaxThroughput);
* cosmosClient.createDatabase(databaseName, throughputProperties);
*
*
* @param id the id of the database.
* @param throughputProperties the throughput properties.
* @return the {@link CosmosDatabaseResponse} with the created database.
* @throws CosmosException if resource with specified id already exists
*/
public CosmosDatabaseResponse createDatabase(String id, ThroughputProperties throughputProperties) {
return blockDatabaseResponse(asyncClientWrapper.createDatabase(id, throughputProperties));
}
void openConnectionsAndInitCaches() {
asyncClientWrapper.openConnectionsAndInitCaches();
}
void openConnectionsAndInitCaches(Duration aggressiveWarmupDuration) {
asyncClientWrapper.openConnectionsAndInitCaches(aggressiveWarmupDuration);
}
void recordOpenConnectionsAndInitCachesCompleted(List cosmosContainerIdentities) {
this.asyncClientWrapper.recordOpenConnectionsAndInitCachesCompleted(cosmosContainerIdentities);
}
void recordOpenConnectionsAndInitCachesStarted(List cosmosContainerIdentities) {
this.asyncClientWrapper.recordOpenConnectionsAndInitCachesStarted(cosmosContainerIdentities);
}
CosmosDatabaseResponse blockDatabaseResponse(Mono databaseMono) {
try {
return databaseMono.block();
} catch (Exception ex) {
final Throwable throwable = Exceptions.unwrap(ex);
if (throwable instanceof CosmosException) {
throw (CosmosException) throwable;
} else {
throw Exceptions.propagate(ex);
}
}
}
/**
* Reads all Cosmos databases.
*
*
* CosmosPagedIterable<CosmosDatabaseProperties> cosmosDatabaseProperties =
* cosmosClient.readAllDatabases();
* cosmosDatabaseProperties.forEach(databaseProperties -> {
* System.out.println(databaseProperties);
* });
*
*
* @param options {@link CosmosQueryRequestOptions}the feed options.
* @return the {@link CosmosPagedIterable} for feed response with the read databases.
*/
CosmosPagedIterable readAllDatabases(CosmosQueryRequestOptions options) {
return getCosmosPagedIterable(asyncClientWrapper.readAllDatabases(options));
}
/**
* Reads all Cosmos databases.
*
*
* CosmosPagedIterable<CosmosDatabaseProperties> cosmosDatabaseProperties =
* cosmosClient.readAllDatabases();
* cosmosDatabaseProperties.forEach(databaseProperties -> {
* System.out.println(databaseProperties);
* });
*
*
* @return the {@link CosmosPagedIterable} for feed response with the read databases.
*/
public CosmosPagedIterable readAllDatabases() {
return getCosmosPagedIterable(asyncClientWrapper.readAllDatabases());
}
/**
* Query a Cosmos database.
*
*
* CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
* CosmosPagedIterable<CosmosDatabaseProperties> databaseProperties =
* cosmosClient.queryDatabases("select * from d", options);
* databaseProperties.forEach(properties -> {
* System.out.println(properties.getId());
* });
*
*
* @param query the query.
* @param options {@link CosmosQueryRequestOptions}the feed options.
* @return the {@link CosmosPagedIterable} for feed response with the obtained databases.
*/
public CosmosPagedIterable queryDatabases(String query, CosmosQueryRequestOptions options) {
return getCosmosPagedIterable(asyncClientWrapper.queryDatabases(query, options));
}
/**
* Query a Cosmos database.
*
*
* CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
* CosmosPagedIterable<CosmosDatabaseProperties> databaseProperties =
* cosmosClient.queryDatabases("select * from d", options);
* databaseProperties.forEach(properties -> {
* System.out.println(properties.getId());
* });
*
*
* @param querySpec {@link SqlQuerySpec} the query spec.
* @param options the query request options.
* @return the {@link CosmosPagedIterable} for feed response with the obtained databases.
*/
public CosmosPagedIterable queryDatabases(SqlQuerySpec querySpec,
CosmosQueryRequestOptions options) {
return getCosmosPagedIterable(asyncClientWrapper.queryDatabases(querySpec, options));
}
/**
* Gets the Cosmos database instance without making a service call.
*
* @param id the id of the database.
* @return {@link CosmosDatabase} the cosmos sync database.
*/
public CosmosDatabase getDatabase(String id) {
return new CosmosDatabase(id, this, asyncClientWrapper.getDatabase(id));
}
CosmosAsyncClient asyncClient() {
return this.asyncClientWrapper;
}
/**
* Close this {@link CosmosClient} instance.
*/
public void close() {
asyncClientWrapper.close();
}
private CosmosPagedIterable getCosmosPagedIterable(CosmosPagedFlux cosmosPagedFlux) {
return new CosmosPagedIterable<>(cosmosPagedFlux);
}
/**
* Create global throughput control config builder which will be used to build {@link GlobalThroughputControlConfig}.
*
* @param databaseId The database id of the control container.
* @param containerId The container id of the control container.
* @return A {@link GlobalThroughputControlConfigBuilder}.
*/
public GlobalThroughputControlConfigBuilder createGlobalThroughputControlConfigBuilder(String databaseId, String containerId) {
return new GlobalThroughputControlConfigBuilder(this.asyncClientWrapper, databaseId, containerId);
}
static void initialize() {
ImplementationBridgeHelpers.CosmosClientHelper.setCosmosClientAccessor(
cosmosClient -> cosmosClient.asyncClient());
}
static { initialize(); }
}