Search in sources :

Example 31 with WriteConcern

use of com.mongodb.WriteConcern in project logging-log4j2 by apache.

the class MongoDbProvider method createNoSqlProvider.

/**
     * Factory method for creating a MongoDB provider within the plugin manager.
     *
     * @param collectionName The name of the MongoDB collection to which log events should be written.
     * @param writeConcernConstant The {@link WriteConcern} constant to control writing details, defaults to
     *                             {@link WriteConcern#ACKNOWLEDGED}.
     * @param writeConcernConstantClassName The name of a class containing the aforementioned static WriteConcern
     *                                      constant. Defaults to {@link WriteConcern}.
     * @param databaseName The name of the MongoDB database containing the collection to which log events should be
     *                     written. Mutually exclusive with {@code factoryClassName&factoryMethodName!=null}.
     * @param server The host name of the MongoDB server, defaults to localhost and mutually exclusive with
     *               {@code factoryClassName&factoryMethodName!=null}.
     * @param port The port the MongoDB server is listening on, defaults to the default MongoDB port and mutually
     *             exclusive with {@code factoryClassName&factoryMethodName!=null}.
     * @param userName The username to authenticate against the MongoDB server with.
     * @param password The password to authenticate against the MongoDB server with.
     * @param factoryClassName A fully qualified class name containing a static factory method capable of returning a
     *                         {@link DB} or a {@link MongoClient}.
     * @param factoryMethodName The name of the public static factory method belonging to the aforementioned factory
     *                          class.
     * @return a new MongoDB provider.
     */
@PluginFactory
public static MongoDbProvider createNoSqlProvider(@PluginAttribute("collectionName") final String collectionName, @PluginAttribute("writeConcernConstant") final String writeConcernConstant, @PluginAttribute("writeConcernConstantClass") final String writeConcernConstantClassName, @PluginAttribute("databaseName") final String databaseName, @PluginAttribute(value = "server", defaultString = "localhost") @ValidHost final String server, @PluginAttribute(value = "port", defaultString = "" + DEFAULT_PORT) @ValidPort final String port, @PluginAttribute("userName") final String userName, @PluginAttribute(value = "password", sensitive = true) final String password, @PluginAttribute("factoryClassName") final String factoryClassName, @PluginAttribute("factoryMethodName") final String factoryMethodName) {
    DB database;
    String description;
    if (Strings.isNotEmpty(factoryClassName) && Strings.isNotEmpty(factoryMethodName)) {
        try {
            final Class<?> factoryClass = LoaderUtil.loadClass(factoryClassName);
            final Method method = factoryClass.getMethod(factoryMethodName);
            final Object object = method.invoke(null);
            if (object instanceof DB) {
                database = (DB) object;
            } else if (object instanceof MongoClient) {
                if (Strings.isNotEmpty(databaseName)) {
                    database = ((MongoClient) object).getDB(databaseName);
                } else {
                    LOGGER.error("The factory method [{}.{}()] returned a MongoClient so the database name is " + "required.", factoryClassName, factoryMethodName);
                    return null;
                }
            } else if (object == null) {
                LOGGER.error("The factory method [{}.{}()] returned null.", factoryClassName, factoryMethodName);
                return null;
            } else {
                LOGGER.error("The factory method [{}.{}()] returned an unsupported type [{}].", factoryClassName, factoryMethodName, object.getClass().getName());
                return null;
            }
            description = "database=" + database.getName();
            final List<ServerAddress> addresses = database.getMongo().getAllAddress();
            if (addresses.size() == 1) {
                description += ", server=" + addresses.get(0).getHost() + ", port=" + addresses.get(0).getPort();
            } else {
                description += ", servers=[";
                for (final ServerAddress address : addresses) {
                    description += " { " + address.getHost() + ", " + address.getPort() + " } ";
                }
                description += "]";
            }
        } catch (final ClassNotFoundException e) {
            LOGGER.error("The factory class [{}] could not be loaded.", factoryClassName, e);
            return null;
        } catch (final NoSuchMethodException e) {
            LOGGER.error("The factory class [{}] does not have a no-arg method named [{}].", factoryClassName, factoryMethodName, e);
            return null;
        } catch (final Exception e) {
            LOGGER.error("The factory method [{}.{}()] could not be invoked.", factoryClassName, factoryMethodName, e);
            return null;
        }
    } else if (Strings.isNotEmpty(databaseName)) {
        final List<MongoCredential> credentials = new ArrayList<>();
        description = "database=" + databaseName;
        if (Strings.isNotEmpty(userName) && Strings.isNotEmpty(password)) {
            description += ", username=" + userName + ", passwordHash=" + NameUtil.md5(password + MongoDbProvider.class.getName());
            credentials.add(MongoCredential.createCredential(userName, databaseName, password.toCharArray()));
        }
        try {
            final int portInt = TypeConverters.convert(port, int.class, DEFAULT_PORT);
            description += ", server=" + server + ", port=" + portInt;
            database = new MongoClient(new ServerAddress(server, portInt), credentials).getDB(databaseName);
        } catch (final Exception e) {
            LOGGER.error("Failed to obtain a database instance from the MongoClient at server [{}] and " + "port [{}].", server, port);
            return null;
        }
    } else {
        LOGGER.error("No factory method was provided so the database name is required.");
        return null;
    }
    try {
        // Check if the database actually requires authentication
        database.getCollectionNames();
    } catch (final Exception e) {
        LOGGER.error("The database is not up, or you are not authenticated, try supplying a username and password to the MongoDB provider.", e);
        return null;
    }
    final WriteConcern writeConcern = toWriteConcern(writeConcernConstant, writeConcernConstantClassName);
    return new MongoDbProvider(database, writeConcern, collectionName, description);
}
Also used : ServerAddress(com.mongodb.ServerAddress) Method(java.lang.reflect.Method) MongoClient(com.mongodb.MongoClient) WriteConcern(com.mongodb.WriteConcern) ArrayList(java.util.ArrayList) List(java.util.List) DB(com.mongodb.DB) PluginFactory(org.apache.logging.log4j.core.config.plugins.PluginFactory)

Example 32 with WriteConcern

use of com.mongodb.WriteConcern in project logging-log4j2 by apache.

the class MongoDbProvider method toWriteConcern.

private static WriteConcern toWriteConcern(final String writeConcernConstant, final String writeConcernConstantClassName) {
    WriteConcern writeConcern;
    if (Strings.isNotEmpty(writeConcernConstant)) {
        if (Strings.isNotEmpty(writeConcernConstantClassName)) {
            try {
                final Class<?> writeConcernConstantClass = LoaderUtil.loadClass(writeConcernConstantClassName);
                final Field field = writeConcernConstantClass.getField(writeConcernConstant);
                writeConcern = (WriteConcern) field.get(null);
            } catch (final Exception e) {
                LOGGER.error("Write concern constant [{}.{}] not found, using default.", writeConcernConstantClassName, writeConcernConstant);
                writeConcern = DEFAULT_WRITE_CONCERN;
            }
        } else {
            writeConcern = WriteConcern.valueOf(writeConcernConstant);
            if (writeConcern == null) {
                LOGGER.warn("Write concern constant [{}] not found, using default.", writeConcernConstant);
                writeConcern = DEFAULT_WRITE_CONCERN;
            }
        }
    } else {
        writeConcern = DEFAULT_WRITE_CONCERN;
    }
    return writeConcern;
}
Also used : Field(java.lang.reflect.Field) WriteConcern(com.mongodb.WriteConcern)

Example 33 with WriteConcern

use of com.mongodb.WriteConcern in project nifi by apache.

the class PutMongo method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final ComponentLog logger = getLogger();
    final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).getValue());
    final String mode = context.getProperty(MODE).getValue();
    final String updateMode = context.getProperty(UPDATE_MODE).getValue();
    final WriteConcern writeConcern = getWriteConcern(context);
    final MongoCollection<Document> collection = getCollection(context, flowFile).withWriteConcern(writeConcern);
    try {
        // Read the contents of the FlowFile into a byte array
        final byte[] content = new byte[(int) flowFile.getSize()];
        session.read(flowFile, in -> StreamUtils.fillBuffer(in, content, true));
        // parse
        final Object doc = (mode.equals(MODE_INSERT) || (mode.equals(MODE_UPDATE) && updateMode.equals(UPDATE_WITH_DOC.getValue()))) ? Document.parse(new String(content, charset)) : JSON.parse(new String(content, charset));
        if (MODE_INSERT.equalsIgnoreCase(mode)) {
            collection.insertOne((Document) doc);
            logger.info("inserted {} into MongoDB", new Object[] { flowFile });
        } else {
            // update
            final boolean upsert = context.getProperty(UPSERT).asBoolean();
            final String updateKey = context.getProperty(UPDATE_QUERY_KEY).evaluateAttributeExpressions(flowFile).getValue();
            final String filterQuery = context.getProperty(UPDATE_QUERY).evaluateAttributeExpressions(flowFile).getValue();
            final Document query;
            if (!StringUtils.isBlank(updateKey)) {
                query = parseUpdateKey(updateKey, (Map) doc);
                removeUpdateKeys(updateKey, (Map) doc);
            } else {
                query = Document.parse(filterQuery);
            }
            if (updateMode.equals(UPDATE_WITH_DOC.getValue())) {
                collection.replaceOne(query, (Document) doc, new UpdateOptions().upsert(upsert));
            } else {
                BasicDBObject update = (BasicDBObject) doc;
                update.remove(updateKey);
                collection.updateOne(query, update, new UpdateOptions().upsert(upsert));
            }
            logger.info("updated {} into MongoDB", new Object[] { flowFile });
        }
        session.getProvenanceReporter().send(flowFile, getURI(context));
        session.transfer(flowFile, REL_SUCCESS);
    } catch (Exception e) {
        logger.error("Failed to insert {} into MongoDB due to {}", new Object[] { flowFile, e }, e);
        session.transfer(flowFile, REL_FAILURE);
        context.yield();
    }
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) Charset(java.nio.charset.Charset) Document(org.bson.Document) ComponentLog(org.apache.nifi.logging.ComponentLog) UpdateOptions(com.mongodb.client.model.UpdateOptions) ProcessException(org.apache.nifi.processor.exception.ProcessException) BasicDBObject(com.mongodb.BasicDBObject) WriteConcern(com.mongodb.WriteConcern) BasicDBObject(com.mongodb.BasicDBObject) Map(java.util.Map)

Example 34 with WriteConcern

use of com.mongodb.WriteConcern in project nifi by apache.

the class PutMongoRecord method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final RecordReaderFactory recordParserFactory = context.getProperty(RECORD_READER_FACTORY).asControllerService(RecordReaderFactory.class);
    final WriteConcern writeConcern = getWriteConcern(context);
    final MongoCollection<Document> collection = getCollection(context).withWriteConcern(writeConcern);
    List<Document> inserts = new ArrayList<>();
    int ceiling = context.getProperty(INSERT_COUNT).asInteger();
    int added = 0;
    boolean error = false;
    try (final InputStream inStream = session.read(flowFile);
        final RecordReader reader = recordParserFactory.createRecordReader(flowFile, inStream, getLogger())) {
        RecordSchema schema = reader.getSchema();
        Record record;
        while ((record = reader.nextRecord()) != null) {
            // Convert each Record to HashMap and put into the Mongo document
            Map<String, Object> contentMap = (Map<String, Object>) DataTypeUtils.convertRecordFieldtoObject(record, RecordFieldType.RECORD.getRecordDataType(record.getSchema()));
            Document document = new Document();
            for (String name : schema.getFieldNames()) {
                document.put(name, contentMap.get(name));
            }
            inserts.add(document);
            if (inserts.size() == ceiling) {
                collection.insertMany(inserts);
                added += inserts.size();
                inserts = new ArrayList<>();
            }
        }
        if (inserts.size() > 0) {
            collection.insertMany(inserts);
        }
    } catch (SchemaNotFoundException | IOException | MalformedRecordException e) {
        getLogger().error("PutMongoRecord failed with error:", e);
        session.transfer(flowFile, REL_FAILURE);
        error = true;
    } finally {
        if (!error) {
            session.getProvenanceReporter().send(flowFile, context.getProperty(URI).evaluateAttributeExpressions().getValue(), String.format("Added %d documents to MongoDB.", added));
            session.transfer(flowFile, REL_SUCCESS);
            getLogger().info("Inserted {} records into MongoDB", new Object[] { added });
        }
    }
    session.commit();
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) InputStream(java.io.InputStream) RecordReader(org.apache.nifi.serialization.RecordReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.bson.Document) RecordReaderFactory(org.apache.nifi.serialization.RecordReaderFactory) MalformedRecordException(org.apache.nifi.serialization.MalformedRecordException) WriteConcern(com.mongodb.WriteConcern) Record(org.apache.nifi.serialization.record.Record) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Map(java.util.Map)

Example 35 with WriteConcern

use of com.mongodb.WriteConcern in project spring-data-mongodb by spring-projects.

the class MongoTemplate method doUpdate.

protected UpdateResult doUpdate(final String collectionName, final Query query, final Update update, @Nullable final Class<?> entityClass, final boolean upsert, final boolean multi) {
    Assert.notNull(collectionName, "CollectionName must not be null!");
    Assert.notNull(query, "Query must not be null!");
    Assert.notNull(update, "Update must not be null!");
    return execute(collectionName, new CollectionCallback<UpdateResult>() {

        public UpdateResult doInCollection(MongoCollection<Document> collection) throws MongoException, DataAccessException {
            MongoPersistentEntity<?> entity = entityClass == null ? null : getPersistentEntity(entityClass);
            increaseVersionForUpdateIfNecessary(entity, update);
            UpdateOptions opts = new UpdateOptions();
            opts.upsert(upsert);
            Document queryObj = new Document();
            if (query != null) {
                queryObj.putAll(queryMapper.getMappedObject(query.getQueryObject(), entity));
                query.getCollation().map(Collation::toMongoCollation).ifPresent(opts::collation);
            }
            Document updateObj = update == null ? new Document() : updateMapper.getMappedObject(update.getUpdateObject(), entity);
            if (multi && update.isIsolated() && !queryObj.containsKey("$isolated")) {
                queryObj.put("$isolated", 1);
            }
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Calling update using query: {} and update: {} in collection: {}", serializeToJsonSafely(queryObj), serializeToJsonSafely(updateObj), collectionName);
            }
            MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName, entityClass, updateObj, queryObj);
            WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction);
            collection = writeConcernToUse != null ? collection.withWriteConcern(writeConcernToUse) : collection;
            if (!UpdateMapper.isUpdateObject(updateObj)) {
                return collection.replaceOne(queryObj, updateObj, opts);
            } else {
                if (multi) {
                    return collection.updateMany(queryObj, updateObj, opts);
                } else {
                    return collection.updateOne(queryObj, updateObj, opts);
                }
            }
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) MongoPersistentEntity(org.springframework.data.mongodb.core.mapping.MongoPersistentEntity) WriteConcern(com.mongodb.WriteConcern) Document(org.bson.Document) Collation(org.springframework.data.mongodb.core.query.Collation) UpdateResult(com.mongodb.client.result.UpdateResult) DataAccessException(org.springframework.dao.DataAccessException)

Aggregations

WriteConcern (com.mongodb.WriteConcern)53 Document (org.bson.Document)14 MongoException (com.mongodb.MongoException)11 List (java.util.List)11 ArrayList (java.util.ArrayList)9 ReadPreference (com.mongodb.ReadPreference)8 Map (java.util.Map)8 Collectors (java.util.stream.Collectors)8 TimeUnit (java.util.concurrent.TimeUnit)7 BsonDocument (org.bson.BsonDocument)7 DeleteResult (com.mongodb.client.result.DeleteResult)6 HashMap (java.util.HashMap)6 ClientSessionOptions (com.mongodb.ClientSessionOptions)5 ReadConcern (com.mongodb.ReadConcern)5 Arrays (java.util.Arrays)5 BsonValue (org.bson.BsonValue)5 FullDocument (com.mongodb.client.model.changestream.FullDocument)4 InsertOneResult (com.mongodb.client.result.InsertOneResult)4 MongoClient (com.mongodb.reactivestreams.client.MongoClient)4 MongoCollection (com.mongodb.reactivestreams.client.MongoCollection)4