List of usage examples for com.mongodb.client.model InsertOneModel InsertOneModel
public InsertOneModel(final T document)
From source file:actor4j.core.persistence.connectors.MongoDBPersistenceAdapter.java
License:Open Source License
@Override public void receive(ActorMessage<?> message) { if (message.tag == PERSIST_EVENTS) { try {/*from ww w . j a va 2s . co m*/ JSONArray array = new JSONArray(message.valueAsString()); if (array.length() == 1) { Document document = Document.parse(array.get(0).toString()); events.insertOne(document); } else { List<WriteModel<Document>> requests = new ArrayList<WriteModel<Document>>(); for (Object obj : array) { Document document = Document.parse(obj.toString()); requests.add(new InsertOneModel<Document>(document)); } events.bulkWrite(requests); } parent.send(new ActorMessage<Object>(null, INTERNAL_PERSISTENCE_SUCCESS, self(), message.source)); } catch (Exception e) { e.printStackTrace(); parent.send(new ActorMessage<Exception>(e, INTERNAL_PERSISTENCE_FAILURE, self(), message.source)); } } else if (message.tag == PERSIST_STATE) { try { Document document = Document.parse(message.valueAsString()); states.insertOne(document); parent.send(new ActorMessage<Object>(null, INTERNAL_PERSISTENCE_SUCCESS, self(), message.source)); } catch (Exception e) { e.printStackTrace(); parent.send(new ActorMessage<Exception>(e, INTERNAL_PERSISTENCE_FAILURE, self(), message.source)); } } else if (message.tag == RECOVER) { try { JSONObject obj = new JSONObject(); Document document = null; FindIterable<Document> statesIterable = states .find(new Document("persistenceId", message.valueAsString())) .sort(new Document("timeStamp", -1)).limit(1); document = statesIterable.first(); if (document != null) { JSONObject stateValue = new JSONObject(document.toJson()); stateValue.remove("_id"); long timeStamp = stateValue.getJSONObject("timeStamp").getLong("$numberLong"); stateValue.put("timeStamp", timeStamp); obj.put("state", stateValue); FindIterable<Document> eventsIterable = events .find(new Document("persistenceId", message.valueAsString()).append("timeStamp", new Document("$gte", timeStamp))) .sort(new Document("timeStamp", -1)); JSONArray array = new JSONArray(); MongoCursor<Document> cursor = eventsIterable.iterator(); while (cursor.hasNext()) { document = cursor.next(); JSONObject eventValue = new JSONObject(document.toJson()); eventValue.remove("_id"); timeStamp = eventValue.getJSONObject("timeStamp").getLong("$numberLong"); eventValue.put("timeStamp", timeStamp); array.put(eventValue); } cursor.close(); obj.put("events", array); } else obj.put("state", new JSONObject()); parent.send(new ActorMessage<String>(obj.toString(), INTERNAL_PERSISTENCE_RECOVER, self(), message.source)); } catch (Exception e) { e.printStackTrace(); JSONObject obj = new JSONObject(); obj.put("error", e.getMessage()); parent.send(new ActorMessage<String>(obj.toString(), INTERNAL_PERSISTENCE_RECOVER, self(), message.source)); } } }
From source file:at.rocworks.oa4j.logger.dbs.NoSQLMongoDB.java
private int storeDataEvents(DataList events) { Date t1 = new Date(); DataItem item;//from w ww .j a va 2 s. c om EventItem event; ValueItem value; ArrayList<WriteModel<Document>> list = new ArrayList<>(); try { for (int i = 0; i <= events.getHighWaterMark() && (item = events.getItem(i)) != null; i++) { if (!(item instanceof EventItem)) continue; event = (EventItem) item; //JDebug.out.info(getTagOfDp(event.getDp())); Document obj = new Document("type", "event").append("tag", getTagOfDp(event.getDp())) .append("ns", event.getTimeNS()) // ISODate does not support nanoseconds .append("ts", new Date(event.getTimeMS())) // ISODate ; // .append("sys", event.getDp().getSystem()) // .append("dp", event.getDp().getDp()) // .append("el", event.getDp().getElement()) // .append("dpel", event.getDp().getDpEl()); // value value = event.getValue(); Map<String, Object> map = new HashMap<>(); // value_number Double dval = value.getDouble(); if (dval != null) map.put("number", dval); // value_string if (value.getString() != null) map.put("text", value.getString()); // value_timestamp if (value.getTime() != null) map.put("time", value.getTime()); // dynvar if (value.getVariableType() == VariableType.DynVar) { if (value.getValueObject() instanceof DynVar) { DynVar dyn = (DynVar) value.getValueObject(); JSONArray arr = new JSONArray(); dyn.asList().forEach((row) -> arr.add(row.getValueObject())); map.put("array", arr); } } obj.append("value", map); // attributes if (event.hasAttributes()) { obj.append("status", event.getStatus()); obj.append("manager", event.getManager()); obj.append("user", event.getUser()); } // check if text is a json document if (documents && value.getString() != null) { try { Object doc = (new JSONParser()).parse(value.getString()); obj.append("document", doc); } catch (ParseException ex) { // no json text } } list.add(new InsertOneModel<>(obj)); //JDebug.out.info(obj.string()); } evcoll.bulkWrite(list, new BulkWriteOptions().ordered(false)); Date t2 = new Date(); addServerStats(events.getHighWaterMark(), t2.getTime() - t1.getTime()); return INoSQLInterface.OK; } catch (MongoBulkWriteException ex) { // TODO mongodb bulk exception //JDebug.out.log(Level.SEVERE, "Bulk exception {0} on {1} records.", new Object[]{ex.getCode(), ex.getWriteErrors().size()}); //JDebug.StackTrace(Level.SEVERE, ex); return INoSQLInterface.ERR_UNRECOVERABLE; } catch (Exception ex) { JDebug.StackTrace(Level.SEVERE, ex); return INoSQLInterface.ERR_REPEATABLE; } }
From source file:com.hazelcast.loader.MongoMapStore.java
License:Open Source License
@Override public void storeAll(Map<String, Supplement> map) { List<InsertOneModel> batch = new LinkedList<InsertOneModel>(); for (Map.Entry<String, Supplement> entry : map.entrySet()) { String key = entry.getKey(); Supplement value = entry.getValue(); batch.add(new InsertOneModel( new Document("name", value.getName()).append("price", value.getPrice()).append("_id", key))); }/*from w ww. j a v a 2s . c o m*/ this.collection.bulkWrite(batch, new BulkWriteOptions().ordered(false)); }
From source file:com.helion3.prism.storage.mongodb.MongoRecords.java
License:MIT License
@Override public StorageWriteResult write(List<DataContainer> containers) throws Exception { MongoCollection<Document> collection = MongoStorageAdapter .getCollection(MongoStorageAdapter.collectionEventRecordsName); // Build an array of documents List<WriteModel<Document>> documents = new ArrayList<WriteModel<Document>>(); for (DataContainer container : containers) { Document document = documentFromView(container); //Prism.getLogger().debug(DataUtil.jsonFromDataView(container).toString()); // TTL/* w w w . j a va2 s.c om*/ document.append("Expires", DateUtil.parseTimeStringToDate(expiration, true)); // Insert documents.add(new InsertOneModel<Document>(document)); } // Write collection.bulkWrite(documents, bulkWriteOptions); // @todo implement real results, BulkWriteResult return new StorageWriteResult(); }
From source file:com.mastfrog.asyncpromises.mongo.BulkWriteBuilderImpl.java
License:Open Source License
@Override public BulkWriteBuilder<T> insert(T doc) { requests.add(new InsertOneModel<>(doc)); return this; }
From source file:com.px100systems.data.plugin.storage.mongo.MongoDatabaseStorage.java
License:Open Source License
private void batchSave(MongoDatabase db, List<StoredBean> inserts, List<StoredBean> updates, List<Delete> deletes) { Map<String, List<WriteModel<Document>>> batches = new HashMap<>(); for (StoredBean bean : inserts) { String unitName = bean.unitName(); List<WriteModel<Document>> batch = batches.get(unitName); if (batch == null) { batch = new ArrayList<>(); batches.put(unitName, batch); }/*from w ww .j av a 2 s.c o m*/ batch.add(new InsertOneModel<Document>(serialize(bean))); } for (StoredBean bean : updates) { String unitName = bean.unitName(); List<WriteModel<Document>> batch = batches.get(unitName); if (batch == null) { batch = new ArrayList<>(); batches.put(unitName, batch); } batch.add(new ReplaceOneModel<Document>(Filters.eq("id", bean.getId()), serialize(bean))); } for (Delete delete : deletes) { String unitName = delete.getUnitName(); List<WriteModel<Document>> batch = batches.get(unitName); if (batch == null) { batch = new ArrayList<>(); batches.put(unitName, batch); } batch.add(delete.getId() == null ? new DeleteManyModel<Document>(delete.getCriteria().convert(new FilterQueryBuilder())) : new DeleteOneModel<Document>(Filters.eq("id", delete.getId()))); } for (Map.Entry<String, List<WriteModel<Document>>> e : batches.entrySet()) db.getCollection(e.getKey()).bulkWrite(e.getValue()); }
From source file:com.streamsets.pipeline.stage.destination.mongodb.MongoDBTarget.java
License:Apache License
@Override public void write(Batch batch) throws StageException { Iterator<Record> records = batch.getRecords(); List<WriteModel<Document>> documentList = new ArrayList<>(); List<Record> recordList = new ArrayList<>(); while (records.hasNext()) { Record record = records.next();// ww w . j a v a2 s .com try { ByteArrayOutputStream baos = new ByteArrayOutputStream(DEFAULT_CAPACITY); DataGenerator generator = generatorFactory.getGenerator(baos); generator.write(record); generator.close(); Document document = Document.parse(new String(baos.toByteArray())); //create a write model based on record header if (!record.getHeader().getAttributeNames().contains(OPERATION_KEY)) { LOG.error(Errors.MONGODB_15.getMessage(), record.getHeader().getSourceId()); throw new OnRecordErrorException(Errors.MONGODB_15, record.getHeader().getSourceId()); } String operation = record.getHeader().getAttribute(OPERATION_KEY); switch (operation) { case INSERT: documentList.add(new InsertOneModel<>(document)); recordList.add(record); break; case UPSERT: validateUniqueKey(operation, record); recordList.add(record); documentList.add(new ReplaceOneModel<>( new Document(removeLeadingSlash(mongoTargetConfigBean.uniqueKeyField), record.get(mongoTargetConfigBean.uniqueKeyField).getValueAsString()), document, new UpdateOptions().upsert(true))); break; case DELETE: recordList.add(record); documentList.add(new DeleteOneModel<Document>(document)); break; default: LOG.error(Errors.MONGODB_14.getMessage(), operation, record.getHeader().getSourceId()); throw new StageException(Errors.MONGODB_14, operation, record.getHeader().getSourceId()); } } catch (IOException | StageException e) { errorRecordHandler.onError(new OnRecordErrorException(record, Errors.MONGODB_13, e.toString(), e)); } } if (!documentList.isEmpty()) { try { BulkWriteResult bulkWriteResult = coll.bulkWrite(documentList); if (bulkWriteResult.wasAcknowledged()) { LOG.trace("Wrote batch with {} inserts, {} updates and {} deletes", bulkWriteResult.getInsertedCount(), bulkWriteResult.getModifiedCount(), bulkWriteResult.getDeletedCount()); } } catch (MongoException e) { for (Record record : recordList) { errorRecordHandler .onError(new OnRecordErrorException(record, Errors.MONGODB_17, e.toString(), e)); } } } }
From source file:examples.tour.QuickTour.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes an optional single argument for the connection string *//*from w w w .j av a2 s . c om*/ public static void main(final String[] args) { MongoClient mongoClient; if (args.length == 0) { // connect to the local database server mongoClient = new MongoClient(); } else { mongoClient = new MongoClient(new MongoClientURI(args[0])); } // get handle to "mydb" database MongoDatabase database = mongoClient.getDatabase("mydb"); // get a handle to the "test" collection MongoCollection<Document> collection = database.getCollection("test"); // drop all the data in it collection.drop(); // make a document and insert it Document doc = new Document("name", "MongoDB").append("type", "database").append("count", 1).append("info", new Document("x", 203).append("y", 102)); collection.insertOne(doc); // get it (since it's the only one in there since we dropped the rest earlier on) Document myDoc = collection.find().first(); System.out.println(myDoc.toJson()); // now, lets add lots of little documents to the collection so we can explore queries and cursors List<Document> documents = new ArrayList<Document>(); for (int i = 0; i < 100; i++) { documents.add(new Document("i", i)); } collection.insertMany(documents); System.out.println( "total # of documents after inserting 100 small ones (should be 101) " + collection.count()); // find first myDoc = collection.find().first(); System.out.println(myDoc.toJson()); // lets get all the documents in the collection and print them out MongoCursor<Document> cursor = collection.find().iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } for (Document cur : collection.find()) { System.out.println(cur.toJson()); } // now use a query to get 1 document out myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson()); // now use a range query to get a larger subset cursor = collection.find(gt("i", 50)).iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } // range query with multiple constraints cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } // Query Filters myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson()); // now use a range query to get a larger subset Block<Document> printBlock = new Block<Document>() { public void apply(final Document document) { System.out.println(document.toJson()); } }; collection.find(gt("i", 50)).forEach(printBlock); // filter where; 50 < i <= 100 collection.find(and(gt("i", 50), lte("i", 100))).forEach(printBlock); // Sorting myDoc = collection.find(exists("i")).sort(descending("i")).first(); System.out.println(myDoc.toJson()); // Projection myDoc = collection.find().projection(excludeId()).first(); System.out.println(myDoc.toJson()); // Aggregation collection .aggregate( asList(match(gt("i", 0)), project(Document.parse("{ITimes10: {$multiply: ['$i', 10]}}")))) .forEach(printBlock); myDoc = collection.aggregate(singletonList(group(null, sum("total", "$i")))).first(); System.out.println(myDoc.toJson()); // Update One collection.updateOne(eq("i", 10), set("i", 110)); // Update Many UpdateResult updateResult = collection.updateMany(lt("i", 100), inc("i", 100)); System.out.println(updateResult.getModifiedCount()); // Delete One collection.deleteOne(eq("i", 110)); // Delete Many DeleteResult deleteResult = collection.deleteMany(gte("i", 100)); System.out.println(deleteResult.getDeletedCount()); collection.drop(); // ordered bulk writes List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>(); writes.add(new InsertOneModel<Document>(new Document("_id", 4))); writes.add(new InsertOneModel<Document>(new Document("_id", 5))); writes.add(new InsertOneModel<Document>(new Document("_id", 6))); writes.add( new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2)))); writes.add(new DeleteOneModel<Document>(new Document("_id", 2))); writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4))); collection.bulkWrite(writes); collection.drop(); collection.bulkWrite(writes, new BulkWriteOptions().ordered(false)); //collection.find().forEach(printBlock); // Clean up database.drop(); // release resources mongoClient.close(); }
From source file:io.sip3.tapir.salto.handler.sip.SipMessageSaveHandler.java
License:Apache License
@Override public void onEvent(List<SipMessage> messages) throws Exception { if (messages.isEmpty()) { return;/*w ww . j a va 2s .c o m*/ } Map<String, List> documents = new HashMap<>(); for (SipMessage message : messages) { String suffix = defineSuffix(message); if (message.isOrigin()) { Document document = convertToIndex(message); documents.computeIfAbsent("index_" + suffix, k -> new ArrayList()) .add(new InsertOneModel<>(document)); } Document document = convertToRaw(message); documents.computeIfAbsent("raw_" + suffix, k -> new ArrayList()).add(new InsertOneModel<>(document)); } documents.forEach((k, v) -> db.getCollection(k).bulkWrite(v)); }
From source file:mongodb.QuickTour.java
License:Apache License
/** * Run this main method to see the output of this quick example. * * @param args takes an optional single argument for the connection string *//*from w w w.j a va 2s .co m*/ public static void main(final String[] args) { //represents a pool of connections to the database MongoClient mongoClient = new MongoClient("10.9.17.105", 27017); // get handle to "mydb" database MongoDatabase database = mongoClient.getDatabase("test"); // get a handle to the "test" collection MongoCollection<Document> collection = database.getCollection("test"); // drop all the data in it collection.drop(); // make a document and insert it Document doc = new Document("name", "MongoDB").append("type", "database").append("count", 1).append("info", new Document("x", 203).append("y", 102)); collection.insertOne(doc); // get it (since it's the only one in there since we dropped the rest earlier on) Document myDoc = collection.find().first(); System.out.println(myDoc.toJson()); // now, lets add lots of little documents to the collection so we can explore queries and cursors List<Document> documents = new ArrayList<Document>(); for (int i = 0; i < 100; i++) { documents.add(new Document("i", i)); } collection.insertMany(documents); System.out.println( "total # of documents after inserting 100 small ones (should be 101) " + collection.count()); // find first myDoc = collection.find().first(); System.out.println(myDoc.toJson()); // lets get all the documents in the collection and print them out MongoCursor<Document> cursor = collection.find().iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } for (Document cur : collection.find()) { System.out.println(cur.toJson()); } // now use a query to get 1 document out myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson()); // now use a range query to get a larger subset cursor = collection.find(gt("i", 50)).iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } // range query with multiple constraints cursor = collection.find(and(gt("i", 50), lte("i", 100))).iterator(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toJson()); } } finally { cursor.close(); } // Query Filters myDoc = collection.find(eq("i", 71)).first(); System.out.println(myDoc.toJson()); // now use a range query to get a larger subset Block<Document> printBlock = new Block<Document>() { @Override public void apply(final Document document) { System.out.println(document.toJson()); } }; collection.find(gt("i", 50)).forEach(printBlock); // filter where; 50 < i <= 100 collection.find(and(gt("i", 50), lte("i", 100))).forEach(printBlock); // Sorting myDoc = collection.find(exists("i")).sort(descending("i")).first(); System.out.println(myDoc.toJson()); // Projection myDoc = collection.find().projection(excludeId()).first(); System.out.println(myDoc.toJson()); // Update One collection.updateOne(eq("i", 10), new Document("$set", new Document("i", 110))); // Update Many UpdateResult updateResult = collection.updateMany(lt("i", 100), new Document("$inc", new Document("i", 100))); System.out.println(updateResult.getModifiedCount()); // Delete One collection.deleteOne(eq("i", 110)); // Delete Many DeleteResult deleteResult = collection.deleteMany(gte("i", 100)); System.out.println(deleteResult.getDeletedCount()); collection.drop(); // ordered bulk writes List<WriteModel<Document>> writes = new ArrayList<WriteModel<Document>>(); writes.add(new InsertOneModel<Document>(new Document("_id", 4))); writes.add(new InsertOneModel<Document>(new Document("_id", 5))); writes.add(new InsertOneModel<Document>(new Document("_id", 6))); writes.add( new UpdateOneModel<Document>(new Document("_id", 1), new Document("$set", new Document("x", 2)))); writes.add(new DeleteOneModel<Document>(new Document("_id", 2))); writes.add(new ReplaceOneModel<Document>(new Document("_id", 3), new Document("_id", 3).append("x", 4))); collection.bulkWrite(writes); collection.drop(); collection.bulkWrite(writes, new BulkWriteOptions().ordered(false)); collection.find().forEach(printBlock); // Clean up // database.drop(); // release resources mongoClient.close(); }