Search in sources :

Example 11 with CosmosDatabase

use of com.azure.cosmos.CosmosDatabase 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 12 with CosmosDatabase

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

the class CosmosDBSinkTaskTest method setup.

@Before
public void setup() throws IllegalAccessException {
    testTask = new CosmosDBSinkTask();
    // Configure settings
    Map<String, String> settingAssignment = CosmosDBSinkConfigTest.setupConfigs();
    settingAssignment.put(CosmosDBSinkConfig.COSMOS_CONTAINER_TOPIC_MAP_CONF, topicName + "#" + containerName);
    settingAssignment.put(CosmosDBSinkConfig.COSMOS_DATABASE_NAME_CONF, databaseName);
    CosmosDBSinkConfig config = new CosmosDBSinkConfig(settingAssignment);
    FieldUtils.writeField(testTask, "config", config, true);
    // Mock the Cosmos SDK
    mockCosmosClient = Mockito.mock(CosmosClient.class);
    CosmosDatabase mockDatabase = Mockito.mock(CosmosDatabase.class);
    when(mockCosmosClient.getDatabase(anyString())).thenReturn(mockDatabase);
    mockContainer = Mockito.mock(CosmosContainer.class);
    when(mockDatabase.getContainer(any())).thenReturn(mockContainer);
    FieldUtils.writeField(testTask, "client", mockCosmosClient, true);
}
Also used : CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosClient(com.azure.cosmos.CosmosClient) Before(org.junit.Before)

Example 13 with CosmosDatabase

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

the class CosmosDBSinkTaskTestNotFails method setup.

@Before
public void setup() throws IllegalAccessException {
    testTask = new CosmosDBSinkTask();
    // Configure settings
    Map<String, String> settingAssignment = CosmosDBSinkConfigTest.setupConfigs();
    settingAssignment.put(CosmosDBSinkConfig.COSMOS_CONTAINER_TOPIC_MAP_CONF, topicName + "#" + containerName);
    settingAssignment.put(CosmosDBSinkConfig.COSMOS_DATABASE_NAME_CONF, databaseName);
    settingAssignment.put(CosmosDBSinkConfig.TOLERANCE_ON_ERROR_CONFIG, "all");
    CosmosDBSinkConfig config = new CosmosDBSinkConfig(settingAssignment);
    FieldUtils.writeField(testTask, "config", config, true);
    // Mock the Cosmos SDK
    mockCosmosClient = Mockito.mock(CosmosClient.class);
    CosmosDatabase mockDatabase = Mockito.mock(CosmosDatabase.class);
    when(mockCosmosClient.getDatabase(anyString())).thenReturn(mockDatabase);
    mockContainer = Mockito.mock(CosmosContainer.class);
    when(mockDatabase.getContainer(any())).thenReturn(mockContainer);
    when(mockContext.errantRecordReporter()).thenReturn(mockErrantReporter);
    FieldUtils.writeField(testTask, "client", mockCosmosClient, true);
}
Also used : CosmosContainer(com.azure.cosmos.CosmosContainer) CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosClient(com.azure.cosmos.CosmosClient) Before(org.junit.Before)

Example 14 with CosmosDatabase

use of com.azure.cosmos.CosmosDatabase 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 15 with CosmosDatabase

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

the class SampleDocumentationSnippets method ManageConflictResolutionPoliciesInAzureCosmosDBLWWSync.

/**
 * https://docs.microsoft.com/en-us/azure/cosmos-db/how-to-manage-conflicts
 * Resolve conflicts, LWW policy
 */
/**
 * Client-side conflict resolution settings for LWW policy
 */
public static void ManageConflictResolutionPoliciesInAzureCosmosDBLWWSync() {
    String container_id = "family_container";
    String partition_key = "/pk";
    CosmosDatabase database = null;
    // <ManageConflictResolutionLWWSync>
    ConflictResolutionPolicy policy = ConflictResolutionPolicy.createLastWriterWinsPolicy("/myCustomId");
    CosmosContainerProperties containerProperties = new CosmosContainerProperties(container_id, partition_key);
    containerProperties.setConflictResolutionPolicy(policy);
    /* ...other container config... */
    database.createContainerIfNotExists(containerProperties);
// </ManageConflictResolutionLWWSync>
}
Also used : CosmosDatabase(com.azure.cosmos.CosmosDatabase) CosmosContainerProperties(com.azure.cosmos.models.CosmosContainerProperties) ConflictResolutionPolicy(com.azure.cosmos.models.ConflictResolutionPolicy)

Aggregations

CosmosDatabase (com.azure.cosmos.CosmosDatabase)18 CosmosContainerProperties (com.azure.cosmos.models.CosmosContainerProperties)11 CosmosContainer (com.azure.cosmos.CosmosContainer)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6 PartitionKey (com.azure.cosmos.models.PartitionKey)5 IndexingPolicy (com.azure.cosmos.models.IndexingPolicy)4 ExecutionException (com.scalar.db.exception.storage.ExecutionException)4 Before (org.junit.Before)4 CosmosClient (com.azure.cosmos.CosmosClient)3 ConflictResolutionPolicy (com.azure.cosmos.models.ConflictResolutionPolicy)3 CosmosContainerResponse (com.azure.cosmos.models.CosmosContainerResponse)3 CosmosItemRequestOptions (com.azure.cosmos.models.CosmosItemRequestOptions)3 CosmosStoredProcedureProperties (com.azure.cosmos.models.CosmosStoredProcedureProperties)3 CosmosClientBuilder (com.azure.cosmos.CosmosClientBuilder)2 CosmosScripts (com.azure.cosmos.CosmosScripts)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 TableMetadata (com.scalar.db.api.TableMetadata)2 URL (java.net.URL)2