Skip to content

Add Azure Cosmos DB sink #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
<version>1.14.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
Expand All @@ -53,7 +58,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.10.8</version>
<version>2.13.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
Expand Down Expand Up @@ -133,7 +138,20 @@
<artifactId>iot-device-client</artifactId>
<version>1.3.30</version>
</dependency>

<!-- Microsoft -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-sdk-bom</artifactId>
<version>1.2.23</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<artifactId>azure-cosmos</artifactId>
<groupId>com.azure</groupId>
<version>4.59.0</version>
</dependency>

<!-- AWS -->
<dependency>
<groupId>com.amazonaws</groupId>
Expand All @@ -149,7 +167,7 @@
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.7</version>
<version>2.13.5</version>
</dependency>
<dependency>
<groupId>org.apache.pulsar</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ public JsonDataGenerator(String simConfigString) {
}
break;
}
// add a case for ComsoDB
case "cosmosdb": {
log.info("Adding CosmosDB Logger with properties: " + elProps);
loggers.add(new CosmosDBLogger(elProps));
break;
}
}
}
if (loggers.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package net.acesinc.data.json.generator.log;

import com.azure.cosmos.CosmosClient;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosContainer;
import com.azure.cosmos.CosmosDatabase;
import com.azure.cosmos.models.CosmosItemRequestOptions;
import com.azure.cosmos.models.CosmosItemResponse;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class CosmosDBLogger implements EventLogger {

private static final Logger log = LogManager.getLogger(CosmosDBLogger.class);
private CosmosClient cosmosClient;
private CosmosDatabase cosmosDatabase;
private CosmosContainer cosmosContainer;
private final ObjectMapper mapper = new ObjectMapper();

public CosmosDBLogger(Map<String, Object> props) {
String uri = (String) props.get("uri");
String key = (String) props.get("key");
String databaseName = (String) props.get("databaseName");
String containerName = (String) props.get("containerName");

cosmosClient = new CosmosClientBuilder()
.endpoint(uri)
.key(key)
.buildClient();

cosmosDatabase = cosmosClient.getDatabase(databaseName);
cosmosContainer = cosmosDatabase.getContainer(containerName);
}

@Override
public void logEvent(String event, Map<String, Object> producerConfig) {
try {
JsonNode jsonNode = mapper.readTree(event);
CosmosItemResponse<JsonNode> item = cosmosContainer.createItem(jsonNode);
log.info("Document added to Cosmos DB with request charge of " + item.getRequestCharge() + " within session " + item.getSessionToken());
} catch (Exception e) {
log.error("Error inserting JSON data into Cosmos DB", e);
}
}

@Override
public void shutdown() {
if (cosmosClient != null) {
cosmosClient.close();
log.info("Cosmos DB client closed successfully");
}
}
}