Search in sources :

Example 21 with CosmosContainerProperties

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

the class CosmosAdmin method createMetadataDatabaseAndContainerIfNotExists.

private void createMetadataDatabaseAndContainerIfNotExists() {
    ThroughputProperties manualThroughput = ThroughputProperties.createManualThroughput(Integer.parseInt(DEFAULT_REQUEST_UNIT));
    client.createDatabaseIfNotExists(metadataDatabase, manualThroughput);
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(METADATA_CONTAINER, "/id");
    client.getDatabase(metadataDatabase).createContainerIfNotExists(containerProperties);
}
Also used : ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties)

Example 22 with CosmosContainerProperties

use of com.azure.cosmos.models.CosmosContainerProperties in project cas by apereo.

the class CosmosDbObjectFactory method createContainer.

/**
 * Create container.
 *
 * @param name         the name
 * @param partitionKey the partition key
 */
public void createContainer(final String name, final String partitionKey) {
    val database = client.getDatabase(properties.getDatabase());
    LOGGER.debug("Creating CosmosDb container [{}]", name);
    val containerProperties = new CosmosContainerProperties(name, '/' + partitionKey);
    containerProperties.setIndexingPolicy(new IndexingPolicy().setIndexingMode(IndexingMode.valueOf(properties.getIndexingMode())));
    val response = database.createContainerIfNotExists(containerProperties);
    LOGGER.debug("Created CosmosDb container [{}]", response.getProperties().getId());
}
Also used : lombok.val(lombok.val) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) IndexingPolicy(com.azure.cosmos.models.IndexingPolicy)

Example 23 with CosmosContainerProperties

use of com.azure.cosmos.models.CosmosContainerProperties in project kafka-connect-cosmosdb by microsoft.

the class SinkConnectorIT method before.

/**
 * Load CosmosDB configuration from the connector config JSON and set up CosmosDB client.
 * Create an embedded Kafka Connect cluster.
 */
@Before
public void before() throws URISyntaxException, IOException {
    // Load the sink.config.json config file
    URL configFileUrl = SinkConnectorIT.class.getClassLoader().getResource("sink.config.json");
    JsonNode config = new ObjectMapper().readTree(configFileUrl);
    connectorName = config.get("name").textValue();
    config = config.get("config");
    String topicContainerMap = config.get("connect.cosmos.containers.topicmap").textValue();
    kafkaTopicJson = StringUtils.substringBefore(topicContainerMap, "#");
    String containerName = StringUtils.substringAfter(topicContainerMap, "#");
    // Setup Cosmos Client
    logger.debug("Setting up the Cosmos DB client");
    cosmosClient = new CosmosClientBuilder().endpoint(config.get("connect.cosmos.connection.endpoint").textValue()).key(config.get("connect.cosmos.master.key").textValue()).buildClient();
    // Create CosmosDB database if not exists
    databaseName = config.get("connect.cosmos.databasename").textValue();
    cosmosClient.createDatabaseIfNotExists(databaseName);
    CosmosDatabase targetDatabase = cosmosClient.getDatabase(databaseName);
    // Create Cosmos Container if not exists
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/id");
    containerProperties.setDefaultTimeToLiveInSeconds(-1);
    targetDatabase.createContainerIfNotExists(containerProperties, ThroughputProperties.createManualThroughput(400));
    targetContainer = targetDatabase.getContainer(containerName);
    // Setup Kafka Connect Client and connector config
    logger.debug("Setting up the Kafka Connect client");
    connectClient = new KafkaConnectClient(new Configuration(CONNECT_CLIENT_URL));
    setupConnectorConfig(config);
}
Also used : CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) KafkaConnectClient(org.sourcelab.kafka.connect.apiclient.KafkaConnectClient) Configuration(org.sourcelab.kafka.connect.apiclient.Configuration) CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) JsonNode(com.fasterxml.jackson.databind.JsonNode) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 24 with CosmosContainerProperties

use of com.azure.cosmos.models.CosmosContainerProperties in project kafka-connect-cosmosdb by microsoft.

the class SourceConnectorIT method before.

/**
 * Load CosmosDB configuration from the connector config JSON and set up CosmosDB client.
 * Create an embedded Kafka Connect cluster.
 */
@Before
public void before() throws URISyntaxException, IOException {
    // Load the source.config.json config file
    URL configFileUrl = SourceConnectorIT.class.getClassLoader().getResource("source.config.json");
    JsonNode config = new ObjectMapper().readTree(configFileUrl);
    connectorName = config.get("name").textValue();
    config = config.get("config");
    String topicContainerMap = config.get("connect.cosmos.containers.topicmap").textValue();
    String topic = StringUtils.substringBefore(topicContainerMap, "#");
    String containerName = StringUtils.substringAfter(topicContainerMap, "#");
    // Setup Cosmos Client
    logger.debug("Setting up the Cosmos DB client");
    cosmosClient = new CosmosClientBuilder().endpoint(config.get("connect.cosmos.connection.endpoint").textValue()).key(config.get("connect.cosmos.master.key").textValue()).buildClient();
    // Create CosmosDB database if not exists
    databaseName = config.get("connect.cosmos.databasename").textValue();
    cosmosClient.createDatabaseIfNotExists(databaseName);
    CosmosDatabase targetDatabase = cosmosClient.getDatabase(databaseName);
    // Create Cosmos Containers (one from config, another for testing multiple workers) if they do not exist
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(containerName, "/id");
    containerProperties.setDefaultTimeToLiveInSeconds(-1);
    targetDatabase.createContainerIfNotExists(containerProperties, ThroughputProperties.createManualThroughput(400));
    containerProperties.setId(SECOND_COSMOS_CONTAINER);
    targetDatabase.createContainerIfNotExists(containerProperties, ThroughputProperties.createManualThroughput(400));
    targetContainer = targetDatabase.getContainer(containerName);
    secondContainer = targetDatabase.getContainer(SECOND_COSMOS_CONTAINER);
    // Setup Kafka Connect Client and connector config
    logger.debug("Setting up the Kafka Connect client");
    connectClient = new KafkaConnectClient(new Configuration(CONNECT_CLIENT_URL));
    setupConnectorConfig(config);
    // Create Kafka Consumer subscribed to topics, recordBuffer to store records from topics
    Properties kafkaProperties = createKafkaConsumerProperties();
    kafkaProperties.put("value.deserializer", JsonDeserializer.class.getName());
    consumer = new KafkaConsumer<>(kafkaProperties);
    consumer.subscribe(Arrays.asList(topic, SECOND_KAFKA_TOPIC));
    // Create Kafka Consumer subscribed to AVRO topic, avroRecordBuffer to store records from AVRO topic
    Properties kafkaAvroProperties = createKafkaConsumerProperties();
    kafkaAvroProperties.put("value.deserializer", KafkaAvroDeserializer.class.getName());
    kafkaAvroProperties.put("schema.registry.url", SCHEMA_REGISTRY_URL);
    avroConsumer = new KafkaConsumer<>(kafkaAvroProperties);
    avroConsumer.subscribe(Arrays.asList(AVRO_KAFKA_TOPIC));
    logger.debug("Consuming Kafka messages from " + kafkaProperties.getProperty("bootstrap.servers"));
    recordBuffer = new ArrayList<>();
    avroRecordBuffer = new ArrayList<>();
}
Also used : Configuration(org.sourcelab.kafka.connect.apiclient.Configuration) CosmosDatabase(com.azure.cosmos.CosmosDatabase) JsonNode(com.fasterxml.jackson.databind.JsonNode) KafkaAvroDeserializer(io.confluent.kafka.serializers.KafkaAvroDeserializer) ThroughputProperties(com.azure.cosmos.models.ThroughputProperties) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Properties(java.util.Properties) JsonDeserializer(org.apache.kafka.connect.json.JsonDeserializer) URL(java.net.URL) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) KafkaConnectClient(org.sourcelab.kafka.connect.apiclient.KafkaConnectClient) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 25 with CosmosContainerProperties

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

the class SampleCRUDQuickstartAsync 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");
    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) ArrayList(java.util.ArrayList) CosmosContainerRequestOptions(com.azure.cosmos.models.CosmosContainerRequestOptions) Families(com.azure.cosmos.examples.common.Families) Family(com.azure.cosmos.examples.common.Family) 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) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) Logger(org.slf4j.Logger) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse) Mono(reactor.core.publisher.Mono) CosmosItemResponse(com.azure.cosmos.models.CosmosItemResponse) CosmosClientBuilder(com.azure.cosmos.CosmosClientBuilder) Collectors(java.util.stream.Collectors) Flux(reactor.core.publisher.Flux) CosmosAsyncClient(com.azure.cosmos.CosmosAsyncClient) CosmosAsyncDatabase(com.azure.cosmos.CosmosAsyncDatabase) 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) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) CosmosContainerResponse(com.azure.cosmos.models.CosmosContainerResponse)

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