mongo-kafka
CLAUDE.md388 starsChanged 7 months agoApache-2.0View on GitHub
Official MongoDB Kafka Connector — a Kafka Connect plugin that ships both a sink connector (Kafka → MongoDB) and a source connector (MongoDB change streams → Kafka). Published as org.mongodb.kafka:mongo-kafka-connect on Maven Central; also packaged as a Confluent Hub archive. Gradle wrapper, Kotlin DSL (build.gradle.kts). Java 8 bytecode (options.release = 8) compiled by JDK 17 toolchain — do not use Java 9+ APIs in src/main. Integration tests require a running MongoDB replica set (source connector needs change streams). The URI…
The file
Files
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project
Official MongoDB Kafka Connector — a Kafka Connect plugin that ships both a sink connector (Kafka → MongoDB) and a source connector (MongoDB change streams → Kafka). Published as org.mongodb.kafka:mongo-kafka-connect on Maven Central; also packaged as a Confluent Hub archive.
Build & test
Gradle wrapper, Kotlin DSL (build.gradle.kts). Java 8 bytecode (options.release = 8) compiled by JDK 17 toolchain — do not use Java 9+ APIs in src/main.
./gradlew check # compile + spotless + checkstyle + spotbugs + unit tests
./gradlew test # unit tests only
./gradlew integrationTest -Dorg.mongodb.test.uri=mongodb://localhost:27017/?replicaSet=rs0
./gradlew test --tests "com.mongodb.kafka.connect.sink.MongoSinkTaskTest"
./gradlew test --tests "*MongoSinkTaskTest.testSomething"
./gradlew spotlessApply # auto-format (also runs as compileJava dep)
./gradlew checkstyleMain spotbugsMain # static checks only
./gradlew confluentJar createConfluentArchive # Confluent Hub artifact
./gradlew compileBuildConfig # generates Versions.java if IntelliJ complainsIntegration tests require a running MongoDB replica set (source connector needs change streams). The URI is passed via -Dorg.mongodb.test.uri=...; without it the integration suite will fail to connect. Unit tests do not need MongoDB.
To run with a different JDK: ./gradlew -PjavaVersion=11 test (the toolchain auto-provisions).
spotlessApply runs automatically before compileJava, so formatting drift will be silently fixed on build — be aware when reviewing diffs after a build.
Architecture
Two independent connectors share util/ helpers but otherwise live in separate trees.
Sink (src/main/java/com/mongodb/kafka/connect/sink/)
MongoSinkConnector → MongoSinkTask → StartedMongoSinkTask. Per-topic config lives in MongoSinkTopicConfig; the task fans records out by topic. The processing pipeline for each batch:
- Convert:
converter/turns the KafkaSinkRecordinto aBsonDocument(key + value). - Post-process: chain of
processor/PostProcessors — projections (allow/block-list),DocumentIdAdder(assigns_idviaid/IdStrategy),KafkaMetaAdder, etc. - CDC handling (optional): if a
CdcHandleris configured (cdc/debezium/,cdc/mongodb/,cdc/qlik/), the post-processed doc is interpreted as a CDC event and translated into aWriteModel. - Write model:
writemodel/strategy/WriteModelStrategychooses the Mongo write op (InsertOneDefaultStrategy,ReplaceOneBusinessKeyStrategy,UpdateOneTimestampsStrategy, etc.) and emits aWriteModel<BsonDocument>forbulkWrite. - Errors:
dlq/integrates with Kafka Connect's dead-letter queue;MongoProcessedSinkRecordDatacarries the per-record outcome.
namespace/ resolves the target db.collection (supports topic→namespace mapping). RateLimitSettings and RecordBatches control throughput. Configurable is the marker for user-pluggable extension classes loaded via ClassHelper.
Source (src/main/java/com/mongodb/kafka/connect/source/)
MongoSourceConnector → MongoSourceTask → StartedMongoSourceTask. Tails a MongoDB change stream (cluster/db/collection scope) and emits Kafka SourceRecords. Key pieces:
MongoCopyDataManager: optional initialcopy.existingsnapshot phase before tailing the stream.producer/SchemaAndValueProducer: pluggable record shape —BsonSchemaAndValueProducer(raw BSON),AvroSchemaAndValueProducer(withschema/AvroSchemafrom a user-supplied schema),InferSchemaAndValueProducer(derives a ConnectSchemafrom the doc),RawJsonStringSchemaAndValueProducer.topic/mapping/: derives the destination Kafka topic from the change event's namespace.heartbeat/: emits heartbeat records so the resume token advances on idle namespaces.json/: BSON ↔ extended-JSON conversion used by the Avro/JSON producers.statistics/+util/jmx/SourceTaskStatistics: JMX metrics surface.
Resume tokens are persisted via Kafka Connect's source offset machinery; util/ResumeTokenUtils handles parsing/validation.
Cross-cutting util (src/main/java/com/mongodb/kafka/connect/util/)
ConnectionValidator+ServerApiConfig: pre-flight checks invoked fromvalidate()on both connectors (verifies connectivity and that the user has the required actions —insert/update/remove[/collStats]for sink,changeStream/findfor source).custom/credentials/: SPI for user-supplied auth providers (e.g. AWS IAM / Assume Role). Seeutil/custom/credentials/README.md.config/ConfigSoftValidator: emits warnings for deprecated/incompatible config combos without failing startup. Both connectors have a*ConfigSoftValidatorthat uses it.jmx/: per-task JMX metric beans.Versions.javais generated at build time by thebuildconfigplugin fromgit describe. If IntelliJ reportscannot find symbol: Versions, run./gradlew compileBuildConfig.
Packaging
Three jars are produced via the Shadow plugin:
mongo-kafka-connect-<ver>.jar— slim (no shaded deps), the Mavendefaultartifact.…-confluent.jar— bundles the Mongo driver,mongodb-crypt, and Avro for Confluent Hub.…-all.jar— same shading, distributed alongside Maven publish.
The dependencies block in build.gradle.kts carries several TODO: Remove this override security pins (CVE patches for transitive deps via Checkstyle, SpotBugs, Avro, and Kafka). When bumping those upstream versions, re-check whether the constraint is still needed and remove the TODO comment.
Conventions
- Spotless config: Google Java Format 1.12.0, custom import order
java, io, org, org.bson, com.mongodb, com.mongodb.kafka, "". Don't reorder imports manually; let Spotless do it. - Checkstyle config:
config/checkstyle/. Spotbugs exclusions:config/spotbugs-exclude.xml. - Tests use JUnit 5 (Jupiter) + Mockito + Hamcrest. Integration tests use Confluent's embedded Kafka (
org.apache.kafka:kafka-streams::test) andkafka-schema-registry. - Branch names and commits use
KAFKA-<n>JIRA prefixes (issues live at https://jira.mongodb.org/browse/KAFKA). - Releases are tag-driven:
publishArchivesrefuses to run if the project version doesn't matchgit describe.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

