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);
}
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);
}
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);
}
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<>();
}
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>
}
Aggregations