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:at.grahsl.kafka.connect.mongodb.cdc.debezium.mongodb.MongoDbDelete.java

License:Apache License

@Override
public WriteModel<BsonDocument> perform(SinkDocument doc) {

    BsonDocument keyDoc = doc.getKeyDoc()
            .orElseThrow(() -> new DataException("error: key doc must not be missing for delete operation"));

    try {/* www  .j  av  a  2 s  . c o  m*/
        BsonDocument filterDoc = BsonDocument.parse("{" + DBCollection.ID_FIELD_NAME + ":"
                + keyDoc.getString(MongoDbHandler.JSON_ID_FIELD_PATH).getValue() + "}");
        return new DeleteOneModel<>(filterDoc);
    } catch (Exception exc) {
        throw new DataException(exc);
    }

}

From source file:at.grahsl.kafka.connect.mongodb.cdc.debezium.mongodb.MongoDbInsert.java

License:Apache License

@Override
public WriteModel<BsonDocument> perform(SinkDocument doc) {

    BsonDocument valueDoc = doc.getValueDoc()
            .orElseThrow(() -> new DataException("error: value doc must not be missing for insert operation"));

    try {/*from w  w  w. j  a v  a 2 s.  c o m*/
        BsonDocument insertDoc = BsonDocument.parse(valueDoc.get(JSON_DOC_FIELD_PATH).asString().getValue());
        return new ReplaceOneModel<>(
                new BsonDocument(DBCollection.ID_FIELD_NAME, insertDoc.get(DBCollection.ID_FIELD_NAME)),
                insertDoc, UPDATE_OPTIONS);
    } catch (Exception exc) {
        throw new DataException(exc);
    }

}

From source file:at.grahsl.kafka.connect.mongodb.cdc.debezium.mongodb.MongoDbUpdate.java

License:Apache License

@Override
public WriteModel<BsonDocument> perform(SinkDocument doc) {

    BsonDocument valueDoc = doc.getValueDoc()
            .orElseThrow(() -> new DataException("error: value doc must not be missing for update operation"));

    try {/*from www.j  av a2s. com*/

        BsonDocument updateDoc = BsonDocument.parse(valueDoc.getString(JSON_DOC_FIELD_PATH).getValue());

        //patch contains full new document for replacement
        if (updateDoc.containsKey(DBCollection.ID_FIELD_NAME)) {
            BsonDocument filterDoc = new BsonDocument(DBCollection.ID_FIELD_NAME,
                    updateDoc.get(DBCollection.ID_FIELD_NAME));
            return new ReplaceOneModel<>(filterDoc, updateDoc, UPDATE_OPTIONS);
        }

        //patch contains idempotent change only to update original document with
        BsonDocument keyDoc = doc.getKeyDoc().orElseThrow(
                () -> new DataException("error: key doc must not be missing for update operation"));

        BsonDocument filterDoc = BsonDocument.parse("{" + DBCollection.ID_FIELD_NAME + ":"
                + keyDoc.getString(MongoDbHandler.JSON_ID_FIELD_PATH).getValue() + "}");

        return new UpdateOneModel<>(filterDoc, updateDoc);

    } catch (DataException exc) {
        exc.printStackTrace();
        throw exc;
    } catch (Exception exc) {
        exc.printStackTrace();
        throw new DataException(exc.getMessage(), exc);
    }

}

From source file:at.grahsl.kafka.connect.mongodb.cdc.debezium.rdbms.RdbmsHandler.java

License:Apache License

protected static BsonDocument generateFilterDoc(BsonDocument keyDoc, BsonDocument valueDoc,
        OperationType opType) {/*  w w  w . ja  v a 2 s.c o m*/
    if (keyDoc.keySet().isEmpty()) {
        if (opType.equals(OperationType.CREATE) || opType.equals(OperationType.READ)) {
            //create: no PK info in keyDoc -> generate ObjectId
            return new BsonDocument(DBCollection.ID_FIELD_NAME, new BsonObjectId());
        }
        //update or delete: no PK info in keyDoc -> take everything in 'before' field
        try {
            BsonDocument filter = valueDoc.getDocument(JSON_DOC_BEFORE_FIELD);
            if (filter.isEmpty())
                throw new BsonInvalidOperationException("value doc before field is empty");
            return filter;
        } catch (BsonInvalidOperationException exc) {
            throw new DataException("error: value doc 'before' field is empty or has invalid type"
                    + " for update/delete operation which seems severely wrong -> defensive actions taken!",
                    exc);
        }
    }
    //build filter document composed of all PK columns
    BsonDocument pk = new BsonDocument();
    for (String f : keyDoc.keySet()) {
        pk.put(f, keyDoc.get(f));
    }
    return new BsonDocument(DBCollection.ID_FIELD_NAME, pk);
}

From source file:at.grahsl.kafka.connect.mongodb.cdc.debezium.rdbms.RdbmsHandler.java

License:Apache License

protected static BsonDocument generateUpsertOrReplaceDoc(BsonDocument keyDoc, BsonDocument valueDoc,
        BsonDocument filterDoc) {/*from   w ww.  j a  v  a  2 s. c  o  m*/

    if (!valueDoc.containsKey(JSON_DOC_AFTER_FIELD) || valueDoc.get(JSON_DOC_AFTER_FIELD).isNull()
            || !valueDoc.get(JSON_DOC_AFTER_FIELD).isDocument()
            || valueDoc.getDocument(JSON_DOC_AFTER_FIELD).isEmpty()) {
        throw new DataException("error: valueDoc must contain non-empty 'after' field"
                + " of type document for insert/update operation");
    }

    BsonDocument upsertDoc = new BsonDocument();
    if (filterDoc.containsKey(DBCollection.ID_FIELD_NAME)) {
        upsertDoc.put(DBCollection.ID_FIELD_NAME, filterDoc.get(DBCollection.ID_FIELD_NAME));
    }

    BsonDocument afterDoc = valueDoc.getDocument(JSON_DOC_AFTER_FIELD);
    for (String f : afterDoc.keySet()) {
        if (!keyDoc.containsKey(f)) {
            upsertDoc.put(f, afterDoc.get(f));
        }
    }
    return upsertDoc;
}

From source file:com.everydots.kafka.connect.mongodb.MongoDbSinkTask.java

License:Apache License

private List<? extends WriteModel<BsonDocument>> buildWriteModel(Collection<SinkRecord> records) {

    List<ReplaceOneModel<BsonDocument>> docsToWrite = new ArrayList<>(records.size());

    records.forEach(record -> {//from  w w w.  j a  va2s.  c  o m
        SinkDocument doc = sinkConverter.convert(record);
        processorChain.process(doc, record);
        doc.getValueDoc()
                .ifPresent(vd -> docsToWrite.add(new ReplaceOneModel<>(
                        new BsonDocument(DBCollection.ID_FIELD_NAME, vd.get(DBCollection.ID_FIELD_NAME)), vd,
                        UPDATE_OPTIONS)));
    });

    return docsToWrite;
}

From source file:com.everydots.kafka.connect.mongodb.processor.DocumentIdAdder.java

License:Apache License

@Override
public void process(SinkDocument doc, SinkRecord orig) {
    doc.getValueDoc().ifPresent(vd -> vd.append(DBCollection.ID_FIELD_NAME, idStrategy.generateId(doc, orig)));
    getNext().ifPresent(pp -> pp.process(doc, orig));
}

From source file:com.everydots.kafka.connect.mongodb.processor.field.projection.BlacklistProjector.java

License:Apache License

@Override
protected void doProjection(String field, BsonDocument doc) {

    if (!field.contains(FieldProjector.SUB_FIELD_DOT_SEPARATOR)) {

        if (field.equals(FieldProjector.SINGLE_WILDCARD) || field.equals(FieldProjector.DOUBLE_WILDCARD)) {
            handleWildcard(field, "", doc);
            return;
        }/*from  w  w  w  .  j  a  v a 2s.c om*/

        //NOTE: never try to remove the _id field
        if (!field.equals(DBCollection.ID_FIELD_NAME))
            doc.remove(field);

        return;
    }

    int dotIdx = field.indexOf(FieldProjector.SUB_FIELD_DOT_SEPARATOR);
    String firstPart = field.substring(0, dotIdx);
    String otherParts = field.length() >= dotIdx ? field.substring(dotIdx + 1) : "";

    if (firstPart.equals(FieldProjector.SINGLE_WILDCARD) || firstPart.equals(FieldProjector.DOUBLE_WILDCARD)) {
        handleWildcard(firstPart, otherParts, doc);
        return;
    }

    BsonValue value = doc.get(firstPart);
    if (value != null && value.isDocument()) {
        doProjection(otherParts, (BsonDocument) value);
    }

}

From source file:com.everydots.kafka.connect.mongodb.processor.field.projection.BlacklistProjector.java

License:Apache License

private void handleWildcard(String firstPart, String otherParts, BsonDocument doc) {
    Iterator<Map.Entry<String, BsonValue>> iter = doc.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, BsonValue> entry = iter.next();
        BsonValue value = entry.getValue();

        //NOTE: never try to remove the _id field
        if (entry.getKey().equals(DBCollection.ID_FIELD_NAME))
            continue;

        if (firstPart.equals(FieldProjector.DOUBLE_WILDCARD)) {
            iter.remove();//from w w w  .  j  a va  2s.  c om
        }

        if (firstPart.equals(FieldProjector.SINGLE_WILDCARD)) {
            if (!value.isDocument()) {
                iter.remove();
            } else {
                if (!otherParts.isEmpty()) {
                    doProjection(otherParts, (BsonDocument) value);
                }
            }
        }
    }
}

From source file:com.everydots.kafka.connect.mongodb.processor.field.projection.WhitelistProjector.java

License:Apache License

@Override
protected void doProjection(String field, BsonDocument doc) {

    //special case short circuit check for '**' pattern
    //this is essentially the same as not using
    //whitelisting at all but instead take the full record
    if (fields.contains(DOUBLE_WILDCARD)) {
        return;//from  ww w.j a  v a  2  s.  c o m
    }

    Iterator<Map.Entry<String, BsonValue>> iter = doc.entrySet().iterator();
    while (iter.hasNext()) {
        Map.Entry<String, BsonValue> entry = iter.next();

        String key = field.isEmpty() ? entry.getKey() : field + SUB_FIELD_DOT_SEPARATOR + entry.getKey();
        BsonValue value = entry.getValue();

        if (!fields.contains(key)
                //NOTE: always keep the _id field
                && !key.equals(DBCollection.ID_FIELD_NAME)) {

            if (!checkForWildcardMatch(key))
                iter.remove();

        }

        if (value.isDocument()) {
            //short circuit check to avoid recursion
            //if 'key.**' pattern exists
            String matchDoubleWildCard = key + SUB_FIELD_DOT_SEPARATOR + DOUBLE_WILDCARD;
            if (!fields.contains(matchDoubleWildCard)) {
                doProjection(key, (BsonDocument) value);
            }
        }

    }
}