Example usage for com.mongodb.client MongoCollection insertMany

List of usage examples for com.mongodb.client MongoCollection insertMany

Introduction

In this page you can find the example usage for com.mongodb.client MongoCollection insertMany.

Prototype

void insertMany(List<? extends TDocument> documents);

Source Link

Document

Inserts one or more documents.

Usage

From source file:com.bluedragon.mongo.MongoCollectionInsert.java

License:Open Source License

public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException {
    MongoDatabase db = getMongoDatabase(_session, argStruct);

    String collection = getNamedStringParam(argStruct, "collection", null);
    if (collection == null)
        throwException(_session, "please specify a collection");

    cfData data = getNamedParam(argStruct, "data", null);
    if (data == null)
        throwException(_session, "please specify data to insert");

    try {//from   w w w  . ja v a2  s.  c  om

        MongoCollection<Document> col = db.getCollection(collection);

        if (data.getDataType() == cfData.CFARRAYDATA) {

            cfArrayData idArr = cfArrayData.createArray(1);
            List<Document> list = new ArrayList<Document>();
            cfArrayData arrdata = (cfArrayData) data;

            for (int x = 0; x < arrdata.size(); x++) {
                Document doc = getDocument(arrdata.getData(x + 1));
                idArr.addElement(new cfStringData(String.valueOf(doc.get("_id"))));
                list.add(doc);
            }

            long start = System.currentTimeMillis();
            col.insertMany(list);
            _session.getDebugRecorder().execMongo(col, "insert", null, System.currentTimeMillis() - start);

            return idArr;

        } else {

            Document doc = getDocument(data);

            long start = System.currentTimeMillis();
            col.insertOne(doc);
            _session.getDebugRecorder().execMongo(col, "insert", null, System.currentTimeMillis() - start);

            return new cfStringData(String.valueOf(doc.get("_id")));
        }

    } catch (MongoException me) {
        throwException(_session, me.getMessage());
        return null;
    }
}

From source file:com.compose.nifi.processors.ComposeBatchPutMongo.java

License:Apache License

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;/*from   ww w  . ja va2  s. co  m*/
    }

    final Charset charset = Charset.forName(context.getProperty(CHARACTER_SET).getValue());
    final String collectionName = context.getProperty(COLLECTION_NAME).getValue();
    final WriteConcern writeConcern = mongoWrapper.getWriteConcern(context);

    final MongoCollection<Document> collection = mongoWrapper.getDatabase(context).getCollection(collectionName)
            .withWriteConcern(writeConcern);

    try {
        // Read the contents of the FlowFile into a byte array
        final byte[] content = new byte[(int) flowFile.getSize()];
        session.read(flowFile, new InputStreamCallback() {
            @Override
            public void process(final InputStream in) throws IOException {
                StreamUtils.fillBuffer(in, content, true);
            }
        });

        //Hack away :)
        ArrayList<Document> docs = new ArrayList<>();
        JsonParser parser = new JsonParser();
        JsonArray array = parser.parse(new String(content, charset)).getAsJsonArray();
        for (JsonElement element : array) {
            //Terrible. surely a better way. learn more.
            Document doc = Document.parse(element.toString());
            docs.add(doc);
        }

        collection.insertMany(docs);

        session.getProvenanceReporter().send(flowFile, mongoWrapper.getURI(context));
        session.transfer(flowFile, REL_SUCCESS);
    } catch (Exception e) {
        getLogger().error("Failed to insert {} into MongoDB due to {}", new Object[] { flowFile, e }, e);
        session.transfer(flowFile, REL_FAILURE);
        context.yield();
    }
}

From source file:com.epam.dlab.mongo.DlabResourceTypeDAO.java

License:Apache License

/**
 * Update monthly total in Mongo DB.//  w  w  w  .j  a v a2s.co  m
 *
 * @param month the month in format YYYY-MM.
 * @throws InitializationException
 */
public void updateMonthTotalCost(String month) throws InitializationException {
    LOGGER.debug("Update total cost for month {}", month);
    try {
        //Check month
        SimpleDateFormat fd = new SimpleDateFormat("yyyy-MM");
        fd.parse(month);
    } catch (java.text.ParseException e) {
        throw new InitializationException("Invalid month value. " + e.getLocalizedMessage(), e);
    }

    List<? extends Bson> pipeline = Arrays.asList(
            match(and(gte(ReportLine.FIELD_USAGE_DATE, month + "-01"),
                    lte(ReportLine.FIELD_USAGE_DATE, month + "-31"))),
            group(getGrouppingFields(FIELD_DLAB_RESOURCE_ID, FIELD_DLAB_RESOURCE_TYPE, FIELD_USER,
                    FIELD_EXPLORATORY_NAME, ReportLine.FIELD_CURRENCY_CODE, ReportLine.FIELD_RESOURCE_TYPE),
                    sum(ReportLine.FIELD_COST, "$" + ReportLine.FIELD_COST)));
    AggregateIterable<Document> docs = connection.getCollection(COLLECTION_BILLING).aggregate(pipeline);

    MongoCollection<Document> collection = connection.getCollection(COLLECTION_BILLING_TOTAL);
    long deletedCount = collection.deleteMany(eq(ReportLine.FIELD_USAGE_DATE, month)).getDeletedCount();
    LOGGER.debug("{} documents has been deleted from collection {}", deletedCount, COLLECTION_BILLING_TOTAL);
    List<Document> totals = new ArrayList<>();
    for (Document d : docs) {
        Document total = (Document) d.get(FIELD_ID);
        total.append(ReportLine.FIELD_USAGE_DATE, month).append(ReportLine.FIELD_COST,
                d.getDouble(ReportLine.FIELD_COST));
        totals.add(total);
    }
    if (!totals.isEmpty()) {
        LOGGER.debug("{} documents will be inserted into collection {}", totals.size(),
                COLLECTION_BILLING_TOTAL);
        collection.insertMany(totals);
    }
}

From source file:com.epam.dlab.mongo.MongoDbConnection.java

License:Apache License

/**
 * Insert documents from list to Mongo collection and clear list.
 *
 * @param collection Mongo collection.//  www .  j ava2 s.  c  o  m
 * @param documents  the list of documents.
 * @throws AdapterException
 */
public void insertRows(MongoCollection<Document> collection, List<Document> documents) throws AdapterException {
    try {
        if (documents.size() > 0) {
            collection.insertMany(documents);
            LOGGER.debug("{} documents has been inserted into collection {}", documents.size(),
                    collection.getNamespace());
            documents.clear();
        }
    } catch (Exception e) {
        throw new AdapterException("Cannot insert new documents into collection " + collection.getNamespace()
                + ": " + e.getLocalizedMessage(), e);
    }
}

From source file:com.mycompany.citysearchnosql.Executioner.java

public static void main(final String[] args) {

    // 1. Connect to MongoDB instance running on localhost
    MongoClient mongoClient = new MongoClient();

    // Access database named 'test'
    MongoDatabase database = mongoClient.getDatabase("test");

    // Access collection named 'restaurants'
    MongoCollection<Document> collection = database.getCollection("restaurants");

    // 2. Insert 
    List<Document> documents = asList(
            new Document("name", "Sun Bakery Trattoria").append("stars", 4).append("categories",
                    asList("Pizza", "Pasta", "Italian", "Coffee", "Sandwiches")),
            new Document("name", "Blue Bagels Grill").append("stars", 3).append("categories",
                    asList("Bagels", "Cookies", "Sandwiches")),
            new Document("name", "Hot Bakery Cafe").append("stars", 4).append("categories",
                    asList("Bakery", "Cafe", "Coffee", "Dessert")),
            new Document("name", "XYZ Coffee Bar").append("stars", 5).append("categories",
                    asList("Coffee", "Cafe", "Bakery", "Chocolates")),
            new Document("name", "456 Cookies Shop").append("stars", 4).append("categories",
                    asList("Bakery", "Cookies", "Cake", "Coffee")));

    collection.insertMany(documents);

    // 3. Query /*from  ww w  .  ja v a2  s  .  com*/
    List<Document> results = collection.find().into(new ArrayList<>());

    // 4. Create Index 
    collection.createIndex(Indexes.ascending("name"));
    // 5. Perform Aggregation
    collection.aggregate(asList(match(eq("categories", "Bakery")), group("$stars", sum("count", 1))));

    mongoClient.close();
}

From source file:com.mycompany.megadatamongo.Generator.java

public long generate(MongoCollection<Document> collection, int amount) throws IOException {
    long start = System.currentTimeMillis();
    long value;/*w ww.  j a v  a  2 s  .com*/
    SecureRandom random = null;
    int modV = (int) Math.log10(amount) * 10;
    FileWriter fw = new FileWriter("store.txt", true);
    BufferedWriter bw = new BufferedWriter(fw);
    PrintWriter fout = new PrintWriter(bw);
    try {
        random = SecureRandom.getInstanceStrong();
    } catch (NoSuchAlgorithmException ex) {
        System.err.println("No secure random algorithm found, " + ex.getMessage());
        return -1;
    }
    List<Document> nums = new ArrayList<>();
    for (int i = 0; i < amount; i++) {
        value = random.nextLong();
        nums.add(new Document(value + "", new Person()));
        if (i % modV == 0) {
            System.out.println("Adding " + nums.size() + " items");
            fout.println(value);
            collection.insertMany(nums);
            nums.clear();
        }
    }
    if (!nums.isEmpty()) {
        collection.insertMany(nums);
    }
    fout.flush();
    fout.close();
    return System.currentTimeMillis() - start;
}

From source file:com.mysweethome.TemperaturePersisterTimerTask.java

public synchronized void persistDataOnMongolab() {
    //disable console logging
    //Logger mongoLogger = Logger.getLogger("org.mongodb.driver"); 
    //mongoLogger.setLevel(Level.SEVERE);

    iStoredTemperatures = iTemperatureStore.getTemperatures();
    if (iStoredTemperatures.isEmpty()) {
        logger.info("Nothing to persist. Exiting");
        return;//from   www .  j  ava  2 s . c  om
    }
    logger.info("Prepairing to persist [{}] Temps in the cloud", iStoredTemperatures.size());
    MongoCollection<Document> mongoCollection = null;
    MongoClient client = null;
    List<Document> documents = new ArrayList<Document>();

    for (TemperatureMeasure tTemp : iStoredTemperatures) { //Exception in thread "Timer-2" java.util.ConcurrentModificationException
        Document doc = new Document();
        doc.put("Location", tTemp.getLocation()); //Location
        doc.put("Group", tTemp.getGroup()); //Group
        doc.put("Date", Helper.getDateAsString(tTemp.getDate())); //Date
        doc.put("Day", Helper.getDayAsString(tTemp.getDate()));
        doc.put("Time", Helper.getTimeAsString(tTemp.getDate()));
        doc.put("Temp", Helper.getTempAsString(tTemp.getTemp())); //Temp
        documents.add(doc);
        iPersistedTemperatures.add(tTemp);
    }

    try {
        MongoClientURI uri = new MongoClientURI(MySweetHomeProperties.ML_URL);
        client = new MongoClient(uri);
        MongoDatabase database = (MongoDatabase) client.getDatabase(uri.getDatabase());
        mongoCollection = database.getCollection("dailytemps");
        mongoCollection.insertMany(documents);
        //eliminate stored Temps from the collection
        iTemperatureStore.removeAll(iPersistedTemperatures);
        client.close();
        logger.info("Temperatures persisted on mongolab: [{}]. Exiting.", iPersistedTemperatures.size());
        iPersistedTemperatures.clear();
    } catch (Throwable e) {
        logger.error("Failed to store Temps in the cloud. Stacktrace: [{}]. Exiting.", e);
        iPersistedTemperatures.clear();
        e.printStackTrace();
    } finally {
        if (client != null) {
            client.close();
        }
        iPersistedTemperatures.clear();
    }
}

From source file:com.redhat.thermostat.gateway.common.mongodb.executor.MongoExecutor.java

License:Open Source License

public MongoDataResultContainer execPostRequest(MongoCollection<DBObject> collection, String body,
        Set<String> realms) {
    MongoDataResultContainer metaDataContainer = new MongoDataResultContainer();

    if (body.length() > 0) {
        List<DBObject> inputList = (List<DBObject>) JSON.parse(body);

        for (DBObject object : inputList) {
            object.removeField(KeycloakFields.REALMS_KEY);
            if (realms != null && !realms.isEmpty()) {
                object.put(KeycloakFields.REALMS_KEY, realms);
            }/*from  www . ja  va2 s.  c  o m*/

        }

        collection.insertMany(inputList);
    }

    return metaDataContainer;
}

From source file:com.redhat.thermostat.gateway.common.mongodb.MongoStorageHandler.java

License:Open Source License

public void addSystemObjects(MongoCollection<DBObject> collection, String systemId, String body) {
    if (body.length() > 0) {
        List<DBObject> inputList = (List<DBObject>) JSON.parse(body);
        for (DBObject o : inputList) {
            o.put(ThermostatFields.SYSTEM_ID, systemId);
        }//from  w  w w.  jav a  2  s  . co m
        collection.insertMany(inputList);
    }
}

From source file:com.redhat.thermostat.gateway.common.mongodb.MongoStorageHandler.java

License:Open Source License

public void addJvmObjects(MongoCollection<DBObject> collection, String systemId, String jvmId, String body) {
    if (body.length() > 0) {
        List<DBObject> inputList = (List<DBObject>) JSON.parse(body);
        for (DBObject o : inputList) {
            o.put(ThermostatFields.SYSTEM_ID, systemId);
            o.put(ThermostatFields.JVM_ID, jvmId);
        }/* ww w.  java2  s .com*/
        collection.insertMany(inputList);
    }
}