Search in sources :

Example 11 with CosmosContainerProperties

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

the class SampleQueryProfilerAsync method queryProfilerDemo.

// private static String customQuery =
// "SELECT * FROM c WHERE c.partitionKey ='Z50V4-745167' AND c.parameterDateTime >= '2020-04-10T27:16:00.000Z' AND c.parameterDateTime <= '2020-04-29T21:16:00.000Z' ORDER BY c.parameterDateTime DESC";
public static void queryProfilerDemo() {
    // Create Async client.
    // Building an async client is still a sync operation.
    client = new CosmosClientBuilder().endpoint(AccountSettings.HOST).key(AccountSettings.MASTER_KEY).consistencyLevel(ConsistencyLevel.EVENTUAL).contentResponseOnWriteEnabled(false).buildAsyncClient();
    // Describe the logic of database and container creation using Reactor...
    client.createDatabaseIfNotExists(databaseName).flatMap(databaseResponse -> {
        database = client.getDatabase(databaseResponse.getProperties().getId());
        logger.info("\n\n\n\nCreated or connected to database {}.\n\n\n\n", databaseName);
        CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, partitionKey);
        ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(manualThroughput);
        return database.createContainerIfNotExists(containerProperties, throughputProperties);
    }).flatMap(containerResponse -> {
        container = database.getContainer(containerResponse.getProperties().getId());
        logger.info("\n\n\n\nCreated or connected to container {}.\n\n\n\n", containerName);
        return Mono.empty();
    }).block();
    // With the client set up we are ready to execute and profile our query.
    Profile.tic();
    CosmosQueryRequestOptions queryOptions = new CosmosQueryRequestOptions();
    queryOptions.setMaxDegreeOfParallelism(1000);
    queryOptions.setMaxBufferedItemCount(1000);
    int preferredPageSize = 1000;
    executeQuery(customQuery, queryOptions, preferredPageSize);
    double toc_time = Profile.toc_ms() / 1000.0;
    logger.info("\n\n\n\nTotal query runtime (sec): {}.\n\n\n\n", toc_time);
    // Close client. This is always sync.
    logger.info("Closing client...");
    client.close();
    logger.info("Done with demo.");
}
Also used : Profile(com.azure.cosmos.examples.common.Profile) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) Mono(reactor.core.publisher.Mono) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) Collectors(java.util.stream.Collectors) Family(com.azure.cosmos.examples.common.Family) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosException(com.azure.cosmos.CosmosException) CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties)

Example 12 with CosmosContainerProperties

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

the class SampleSubpartitioningAsync method createContainerIfNotExists.

private void createContainerIfNotExists() throws Exception {
    logger.info("Create container " + containerName + " if not exists.");
    // Create container if not exists
    // <Create PartitionKeyDefinition>
    List<String> partitionKeyPaths = new ArrayList<String>();
    partitionKeyPaths.add("/tenantId");
    partitionKeyPaths.add("/userId");
    partitionKeyPaths.add("/sessionId");
    PartitionKeyDefinition subpartitionKeyDefinition = new PartitionKeyDefinition();
    subpartitionKeyDefinition.setPaths(partitionKeyPaths);
    subpartitionKeyDefinition.setKind(PartitionKind.MULTI_HASH);
    subpartitionKeyDefinition.setVersion(PartitionKeyDefinitionVersion.V2);
    // <CreateContainerIfNotExists>
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, subpartitionKeyDefinition);
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    Mono<CosmosContainerResponse> containerIfNotExists = database.createContainerIfNotExists(containerProperties, throughputProperties);
    // Create container with 400 RU/s
    CosmosContainerResponse cosmosContainerResponse = containerIfNotExists.block();
    container = database.getContainer(cosmosContainerResponse.getProperties().getId());
    // </CreateContainerIfNotExists>
    // Modify existing container
    containerProperties = cosmosContainerResponse.getProperties();
    Mono<CosmosContainerResponse> propertiesReplace = container.replace(containerProperties, new CosmosContainerRequestOptions());
    propertiesReplace.flatMap(containerResponse -> {
        logger.info("setupContainer(): Container " + container.getId() + " in " + database.getId() + "has been updated with it's new properties.");
        return Mono.empty();
    }).onErrorResume((exception) -> {
        logger.error("setupContainer(): Unable to update properties for container " + container.getId() + " in database " + database.getId() + ". e: " + exception.getLocalizedMessage());
        return Mono.empty();
    }).block();
}
Also used : CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) PartitionKey(com.azure.cosmos.models.PartitionKey) LoggerFactory(org.slf4j.LoggerFactory) CosmosQueryRequestOptions(com.azure.cosmos.models.CosmosQueryRequestOptions) SampleCRUDQuickstartAsync(com.azure.cosmos.examples.crudquickstart.async.SampleCRUDQuickstartAsync) ArrayList(java.util.ArrayList) CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) CosmosAsyncContainer(com.azure.cosmos.CosmosAsyncContainer) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) Duration(java.time.Duration) ConsistencyLevel(com.azure.cosmos.ConsistencyLevel) CosmosException(com.azure.cosmos.CosmosException) PartitionKind(com.azure.cosmos.models.PartitionKind) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Logger(org.slf4j.Logger) UserSessionData(com.azure.cosmos.examples.common.UserSessionData) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) PartitionKeyDefinitionVersion(com.azure.cosmos.models.PartitionKeyDefinitionVersion) Mono(reactor.core.publisher.Mono) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) Collectors(java.util.stream.Collectors) PartitionKeyBuilder(com.azure.cosmos.models.PartitionKeyBuilder) UserSession(com.azure.cosmos.examples.common.UserSession) Flux(reactor.core.publisher.Flux) List(java.util.List) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) PartitionKeyDefinition(com.azure.cosmos.models.PartitionKeyDefinition) CosmosDatabaseResponse(com.azure.cosmos.models.CosmosDatabaseResponse) CosmosPagedFlux(com.azure.cosmos.util.CosmosPagedFlux) AccountSettings(com.azure.cosmos.examples.common.AccountSettings) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) ArrayList(java.util.ArrayList) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) PartitionKeyDefinition(com.azure.cosmos.models.PartitionKeyDefinition) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 13 with CosmosContainerProperties

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

the class SampleCRUDQuickstart method createContainerIfNotExists.

private void createContainerIfNotExists() throws Exception {
    logger.info("Create container " + containerName + " if not exists.");
    // Create container if not exists
    // <CreateContainerIfNotExists>
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/lastName");
    // Create container with 400 RU/s
    ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
    CosmosContainerResponse containerResponse = database.createContainerIfNotExists(containerProperties, throughputProperties);
    container = database.getContainer(containerResponse.getProperties().getId());
    // </CreateContainerIfNotExists>
    logger.info("Checking container " + container.getId() + " completed!\n");
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

Example 14 with CosmosContainerProperties

use of com.azure.cosmos.models.CosmosContainerProperties in project scalardb by scalar-labs.

the class CosmosAdmin method computeContainerProperties.

private CosmosContainerProperties computeContainerProperties(String table, TableMetadata metadata) {
    IndexingPolicy indexingPolicy = new IndexingPolicy();
    List<IncludedPath> paths = new ArrayList<>();
    if (metadata.getClusteringKeyNames().isEmpty()) {
        paths.add(new IncludedPath(PARTITION_KEY_PATH + "/?"));
    } else {
        // Add a composite index when we have clustering keys
        List<CompositePath> compositePaths = new ArrayList<>();
        // Add concatenated partition key to the composite path first
        CompositePath partitionKeyCompositePath = new CompositePath();
        partitionKeyCompositePath.setPath(PARTITION_KEY_PATH);
        partitionKeyCompositePath.setOrder(CompositePathSortOrder.ASCENDING);
        compositePaths.add(partitionKeyCompositePath);
        // Then, add clustering keys to the composite path
        metadata.getClusteringKeyNames().forEach(c -> {
            CompositePath compositePath = new CompositePath();
            compositePath.setPath(CLUSTERING_KEY_PATH_PREFIX + c);
            compositePath.setOrder(metadata.getClusteringOrder(c) == Order.ASC ? CompositePathSortOrder.ASCENDING : CompositePathSortOrder.DESCENDING);
            compositePaths.add(compositePath);
        });
        indexingPolicy.setCompositeIndexes(Collections.singletonList(compositePaths));
    }
    paths.addAll(metadata.getSecondaryIndexNames().stream().map(index -> new IncludedPath(SECONDARY_INDEX_KEY_PATH_PREFIX + index + "/?")).collect(Collectors.toList()));
    if (!paths.isEmpty()) {
        indexingPolicy.setIncludedPaths(paths);
    }
    indexingPolicy.setExcludedPaths(Collections.singletonList(new ExcludedPath(EXCLUDED_PATH)));
    return new CosmosContainerProperties(table, PARTITION_KEY_PATH).setIndexingPolicy(indexingPolicy);
}
Also used : CompositePath(com.azure.cosmos.models.CompositePath) ArrayList(java.util.ArrayList) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) IncludedPath(com.azure.cosmos.models.IncludedPath) ExcludedPath(com.azure.cosmos.models.ExcludedPath) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy)

Example 15 with CosmosContainerProperties

use of com.azure.cosmos.models.CosmosContainerProperties in project scalardb by scalar-labs.

the class CosmosAdminTest method createTable_WithoutClusteringKeys_ShouldCreateContainerWithCompositeIndex.

private void createTable_WithoutClusteringKeys_ShouldCreateContainerWithCompositeIndex(Optional<String> tableMetadataDatabase) throws ExecutionException {
    // Arrange
    String namespace = "ns";
    String table = "sample_table";
    TableMetadata metadata = TableMetadata.newBuilder().addPartitionKey("c3").addColumn("c1", DataType.TEXT).addColumn("c2", DataType.BIGINT).addColumn("c3", DataType.BOOLEAN).addColumn("c4", DataType.BLOB).addColumn("c5", DataType.INT).addColumn("c6", DataType.DOUBLE).addColumn("c7", DataType.FLOAT).addSecondaryIndex("c4").build();
    when(client.getDatabase(namespace)).thenReturn(database);
    when(database.getContainer(table)).thenReturn(container);
    CosmosScripts cosmosScripts = Mockito.mock(CosmosScripts.class);
    when(container.getScripts()).thenReturn(cosmosScripts);
    // for metadata table
    String metadataDatabaseName = tableMetadataDatabase.orElse(CosmosAdmin.METADATA_DATABASE);
    CosmosDatabase metadataDatabase = mock(CosmosDatabase.class);
    CosmosContainer metadataContainer = mock(CosmosContainer.class);
    when(client.getDatabase(metadataDatabaseName)).thenReturn(metadataDatabase);
    when(metadataDatabase.getContainer(CosmosAdmin.METADATA_CONTAINER)).thenReturn(metadataContainer);
    if (tableMetadataDatabase.isPresent()) {
        when(config.getTableMetadataDatabase()).thenReturn(tableMetadataDatabase);
        admin = new CosmosAdmin(client, config);
    }
    // Act
    admin.createTable(namespace, table, metadata, Collections.emptyMap());
    // Assert
    ArgumentCaptor<CosmosContainerProperties> containerPropertiesCaptor = ArgumentCaptor.forClass(CosmosContainerProperties.class);
    verify(database).createContainer(containerPropertiesCaptor.capture());
    assertThat(containerPropertiesCaptor.getValue().getId()).isEqualTo(table);
    // check index related info
    IndexingPolicy indexingPolicy = containerPropertiesCaptor.getValue().getIndexingPolicy();
    assertThat(indexingPolicy.getIncludedPaths().size()).isEqualTo(2);
    assertThat(indexingPolicy.getIncludedPaths().get(0).getPath()).isEqualTo("/concatenatedPartitionKey/?");
    assertThat(indexingPolicy.getIncludedPaths().get(1).getPath()).isEqualTo("/values/c4/?");
    assertThat(indexingPolicy.getExcludedPaths().size()).isEqualTo(1);
    assertThat(indexingPolicy.getExcludedPaths().get(0).getPath()).isEqualTo("/*");
    assertThat(indexingPolicy.getCompositeIndexes()).isEmpty();
    verify(cosmosScripts).createStoredProcedure(any(CosmosStoredProcedureProperties.class));
    // for metadata table
    verify(client).createDatabaseIfNotExists(eq(metadataDatabaseName), refEq(ThroughputProperties.createManualThroughput(Integer.parseInt("400"))));
    verify(metadataDatabase).createContainerIfNotExists(containerPropertiesCaptor.capture());
    assertThat(containerPropertiesCaptor.getValue().getId()).isEqualTo(CosmosAdmin.METADATA_CONTAINER);
    assertThat(containerPropertiesCaptor.getValue().getPartitionKeyDefinition().getPaths()).containsExactly("/id");
    CosmosTableMetadata cosmosTableMetadata = new CosmosTableMetadata();
    cosmosTableMetadata.setId(getFullTableName(namespace, table));
    cosmosTableMetadata.setPartitionKeyNames(Collections.singletonList("c3"));
    cosmosTableMetadata.setClusteringOrders(Collections.emptyMap());
    cosmosTableMetadata.setClusteringKeyNames(Collections.emptyList());
    cosmosTableMetadata.setColumns(new ImmutableMap.Builder<String, String>().put("c1", "text").put("c2", "bigint").put("c3", "boolean").put("c4", "blob").put("c5", "int").put("c6", "double").put("c7", "float").build());
    cosmosTableMetadata.setSecondaryIndexNames(ImmutableSet.of("c4"));
    verify(metadataContainer).upsertItem(cosmosTableMetadata);
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CosmosScripts(com.azure.cosmos.CosmosScripts) ImmutableMap(com.google.common.collect.ImmutableMap) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosStoredProcedureProperties(com.azure.cosmos.models.CosmosStoredProcedureProperties) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy)

Aggregations

CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)40 ThroughputProperties (com.azure.cosmos.models.ThroughputProperties)23 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)19 CosmosAsyncDatabase (com.azure.cosmos.CosmosAsyncDatabase)14 CosmosDatabase (com.azure.cosmos.CosmosDatabase)12 CosmosAsyncContainer (com.azure.cosmos.CosmosAsyncContainer)11 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)11 ArrayList (java.util.ArrayList)11 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)10 ConsistencyLevel (com.azure.cosmos.ConsistencyLevel)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 CosmosAsyncClient (com.azure.cosmos.CosmosAsyncClient)7 CosmosException (com.azure.cosmos.CosmosException)7 AccountSettings (com.azure.cosmos.examples.common.AccountSettings)7 ConflictResolutionPolicy (com.azure.cosmos.models.ConflictResolutionPolicy)7 PartitionKey (com.azure.cosmos.models.PartitionKey)7 Mono (reactor.core.publisher.Mono)7 CosmosItemResponse (com.azure.cosmos.models.CosmosItemResponse)6 Flux (reactor.core.publisher.Flux)6