Search in sources :

Example 1 with BulkWriteError

use of com.mongodb.bulk.BulkWriteError in project zeppelin by apache.

the class MongoNotebookRepo method printDuplicatedException.

/**
   * MongoBulkWriteException contains error messages that inform
   * which documents were duplicated. This method catches those ID and print them.
   * @param e
   */
private void printDuplicatedException(MongoBulkWriteException e) {
    List<BulkWriteError> errors = e.getWriteErrors();
    for (BulkWriteError error : errors) {
        String msg = error.getMessage();
        // regex for note ID
        Pattern pattern = Pattern.compile("[A-Z0-9]{9}");
        Matcher matcher = pattern.matcher(msg);
        if (matcher.find()) {
            // if there were a note ID
            String noteId = matcher.group();
            LOG.warn("Note " + noteId + " not inserted since already exists in MongoDB");
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) BulkWriteError(com.mongodb.bulk.BulkWriteError)

Example 2 with BulkWriteError

use of com.mongodb.bulk.BulkWriteError in project mongo-java-driver by mongodb.

the class WriteCommandResultHelper method getWriteErrors.

@SuppressWarnings("unchecked")
private static List<BulkWriteError> getWriteErrors(final BsonDocument result) {
    List<BulkWriteError> writeErrors = new ArrayList<BulkWriteError>();
    BsonArray writeErrorsDocuments = (BsonArray) result.get("writeErrors");
    if (writeErrorsDocuments != null) {
        for (BsonValue cur : writeErrorsDocuments) {
            BsonDocument curDocument = (BsonDocument) cur;
            writeErrors.add(new BulkWriteError(curDocument.getNumber("code").intValue(), curDocument.getString("errmsg").getValue(), curDocument.getDocument("errInfo", new BsonDocument()), curDocument.getNumber("index").intValue()));
        }
    }
    return writeErrors;
}
Also used : BsonDocument(org.bson.BsonDocument) BsonArray(org.bson.BsonArray) ArrayList(java.util.ArrayList) BulkWriteError(com.mongodb.bulk.BulkWriteError) BsonValue(org.bson.BsonValue)

Aggregations

BulkWriteError (com.mongodb.bulk.BulkWriteError)2 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BsonArray (org.bson.BsonArray)1 BsonDocument (org.bson.BsonDocument)1 BsonValue (org.bson.BsonValue)1