Search in sources :

Example 6 with CosmosClient

use of com.azure.cosmos.CosmosClient in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SampleDocumentationSnippets method PerformanceTipsJavaSDKv4ClientSync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips-java-sdk-v4-sql
 * Performance tips - needs scheduler
 */
/**
 * Performance tips - needs scheduler
 */
public static void PerformanceTipsJavaSDKv4ClientSync() {
    String HOSTNAME = "";
    String MASTERKEY = "";
    // Arbitrary
    ConsistencyLevel CONSISTENCY = ConsistencyLevel.EVENTUAL;
    // <PerformanceClientSync>
    CosmosClient client = new CosmosClientBuilder().endpoint(HOSTNAME).key(MASTERKEY).consistencyLevel(CONSISTENCY).buildClient();
// </PerformanceClientSync>
}
Also used : ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) CosmosClient(com.azure.cosmos.CosmosClient)

Example 7 with CosmosClient

use of com.azure.cosmos.CosmosClient in project azure-cosmos-java-sql-api-samples by Azure-Samples.

the class SampleDocumentationSnippets method PerformanceTipsJavaSDKv4ConnectionModeSync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/performance-tips-java-sdk-v4-sql
 * Performance tips - sync Connection Mode
 */
/**
 * Performance tips - sync Connection Mode
 */
public static void PerformanceTipsJavaSDKv4ConnectionModeSync() {
    String HOSTNAME = "";
    String MASTERKEY = "";
    // Arbitrary
    ConsistencyLevel CONSISTENCY = ConsistencyLevel.EVENTUAL;
    // <PerformanceClientConnectionModeSync>
    /* Direct mode, default settings */
    CosmosClient clientDirectDefault = new CosmosClientBuilder().endpoint(HOSTNAME).key(MASTERKEY).consistencyLevel(CONSISTENCY).directMode().buildClient();
    /* Direct mode, custom settings */
    DirectConnectionConfig directConnectionConfig = DirectConnectionConfig.getDefaultConfig();
    // Example config, do not use these settings as defaults
    directConnectionConfig.setMaxConnectionsPerEndpoint(120);
    directConnectionConfig.setIdleConnectionTimeout(Duration.ofMillis(100));
    CosmosClient clientDirectCustom = new CosmosClientBuilder().endpoint(HOSTNAME).key(MASTERKEY).consistencyLevel(CONSISTENCY).directMode(directConnectionConfig).buildClient();
    /* Gateway mode, default settings */
    CosmosClient clientGatewayDefault = new CosmosClientBuilder().endpoint(HOSTNAME).key(MASTERKEY).consistencyLevel(CONSISTENCY).gatewayMode().buildClient();
    /* Gateway mode, custom settings */
    GatewayConnectionConfig gatewayConnectionConfig = GatewayConnectionConfig.getDefaultConfig();
    // Example config, do not use these settings as defaults
    gatewayConnectionConfig.setProxy(new ProxyOptions(ProxyOptions.Type.HTTP, InetSocketAddress.createUnresolved("your.proxy.addr", 80)));
    gatewayConnectionConfig.setMaxConnectionPoolSize(150);
    CosmosClient clientGatewayCustom = new CosmosClientBuilder().endpoint(HOSTNAME).key(MASTERKEY).consistencyLevel(CONSISTENCY).gatewayMode(gatewayConnectionConfig).buildClient();
    /* No connection mode, defaults to Direct mode with default settings */
    CosmosClient clientDefault = new CosmosClientBuilder().endpoint(HOSTNAME).key(MASTERKEY).consistencyLevel(CONSISTENCY).buildClient();
// </PerformanceClientConnectionModeSync>
}
Also used : ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) DirectConnectionConfig(com.azure.cosmos.DirectConnectionConfig) ProxyOptions(com.azure.core.http.ProxyOptions) CosmosClient(com.azure.cosmos.CosmosClient) GatewayConnectionConfig(com.azure.cosmos.GatewayConnectionConfig)

Example 8 with CosmosClient

use of com.azure.cosmos.CosmosClient in project DataSpaceConnector by eclipse-dataspaceconnector.

the class CosmosDbApiImpl method getContainer.

private static CosmosContainer getContainer(Vault vault, AbstractCosmosConfig config) {
    CosmosClient client = createClient(vault, config.getAccountName(), Collections.singletonList(config.getPreferredRegion()));
    CosmosDatabase database = getDatabase(client, config.getDbName());
    if (database.readAllContainers().stream().noneMatch(sp -> sp.getId().equals(config.getContainerName()))) {
        throw new EdcException("No CosmosDB container named '" + config.getContainerName() + "' was found in account '" + config.getAccountName() + "'. Please create one, preferably using terraform.");
    }
    return database.getContainer(config.getContainerName());
}
Also used : CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosClient(com.azure.cosmos.CosmosClient) EdcException(org.eclipse.dataspaceconnector.spi.EdcException)

Aggregations

CosmosClient (com.azure.cosmos.CosmosClient)8 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)7 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)3 ProxyOptions (com.azure.core.http.ProxyOptions)2 DirectConnectionConfig (com.azure.cosmos.DirectConnectionConfig)2 GatewayConnectionConfig (com.azure.cosmos.GatewayConnectionConfig)2 ArrayList (java.util.ArrayList)2 CosmosDatabase (com.azure.cosmos.CosmosDatabase)1 EdcException (org.eclipse.dataspaceconnector.spi.EdcException)1 CosmosDbContainer (org.vividus.azure.cosmos.model.CosmosDbContainer)1