Example usage for com.mongodb DBCollection ID_FIELD_NAME

List of usage examples for com.mongodb DBCollection ID_FIELD_NAME

Introduction

In this page you can find the example usage for com.mongodb DBCollection ID_FIELD_NAME.

Prototype

String ID_FIELD_NAME

To view the source code for com.mongodb DBCollection ID_FIELD_NAME.

Click Source Link

Usage

From source file:com.everydots.kafka.connect.mongodb.processor.id.strategy.ProvidedStrategy.java

License:Apache License

@Override
public BsonValue generateId(SinkDocument doc, SinkRecord orig) {

    Optional<BsonDocument> bd = Optional.empty();

    if (where.equals(ProvidedIn.KEY)) {
        bd = doc.getKeyDoc();/*  w w  w  .ja v a  2s  . c  om*/
    }

    if (where.equals(ProvidedIn.VALUE)) {
        bd = doc.getValueDoc();
    }

    BsonValue _id = bd.map(vd -> vd.get(DBCollection.ID_FIELD_NAME))
            .orElseThrow(() -> new DataException("error: provided id strategy is used "
                    + "but the document structure either contained no _id field or it was null"));

    if (_id instanceof BsonNull) {
        throw new DataException("error: provided id strategy used "
                + "but the document structure contained an _id of type BsonNull");
    }

    return _id;

}