at.grahsl.kafka.connect.mongodb.writemodel.strategy.ReplaceOneBusinessKeyStrategy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kafka-connect-mongodb Show documentation
Show all versions of kafka-connect-mongodb Show documentation
A Kafka connect MongoDB sink connector.
The newest version!
package at.grahsl.kafka.connect.mongodb.writemodel.strategy;
import at.grahsl.kafka.connect.mongodb.converter.SinkDocument;
import com.mongodb.DBCollection;
import com.mongodb.client.model.ReplaceOneModel;
import com.mongodb.client.model.UpdateOptions;
import com.mongodb.client.model.WriteModel;
import org.apache.kafka.connect.errors.DataException;
import org.bson.BsonDocument;
import org.bson.BsonValue;
public class ReplaceOneBusinessKeyStrategy implements WriteModelStrategy {
private static final UpdateOptions UPDATE_OPTIONS =
new UpdateOptions().upsert(true);
@Override
public WriteModel createWriteModel(SinkDocument document) {
BsonDocument vd = document.getValueDoc().orElseThrow(
() -> new DataException("error: cannot build the WriteModel since"
+ " the value document was missing unexpectedly")
);
BsonValue businessKey = vd.get(DBCollection.ID_FIELD_NAME);
if(businessKey == null || !(businessKey instanceof BsonDocument)) {
throw new DataException("error: cannot build the WriteModel since"
+ " the value document does not contain an _id field of type BsonDocument"
+ " which holds the business key fields");
}
vd.remove(DBCollection.ID_FIELD_NAME);
return new ReplaceOneModel<>((BsonDocument)businessKey, vd, UPDATE_OPTIONS);
}
}