org.bson.BsonDocument Java Examples
The following examples show how to use
org.bson.BsonDocument.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JacksonRepoTest.java From immutables with Apache License 2.0 | 6 votes |
@Test public void withDate() { final Date date = new Date(); final ObjectId id = ObjectId.get(); final Jackson expected = ImmutableJackson.builder() .id(id) .prop1("prop1") .prop2("22") .date(new Date(date.getTime())) .build(); repository.insert(expected).getUnchecked(); check(collection.count()).is(1L); final Jackson actual = repository.findAll().fetchAll().getUnchecked().get(0); check(expected).is(actual); final BsonDocument doc = collection.find().first(); check(doc.keySet()).hasContentInAnyOrder("_id", "prop1", "prop2", "date", "uuid"); check(doc.get("date").asDateTime().getValue()).is(date.getTime()); check(doc.get("_id").asObjectId().getValue()).is(id); }
Example #2
Source File: UpdateDescription.java From stitch-android-sdk with Apache License 2.0 | 6 votes |
/** * Convert this update description to an update document. * * @return an update document with the appropriate $set and $unset documents. */ public BsonDocument toUpdateDocument() { final List<BsonElement> unsets = new ArrayList<>(); for (final String removedField : this.removedFields) { unsets.add(new BsonElement(removedField, new BsonBoolean(true))); } final BsonDocument updateDocument = new BsonDocument(); if (this.updatedFields.size() > 0) { updateDocument.append("$set", this.updatedFields); } if (unsets.size() > 0) { updateDocument.append("$unset", new BsonDocument(unsets)); } return updateDocument; }
Example #3
Source File: MongoDbUpdateTest.java From mongo-kafka with Apache License 2.0 | 6 votes |
@Test @DisplayName("when valid doc change cdc event then correct UpdateOneModel") void testValidSinkDocumentForUpdate() { BsonDocument keyDoc = BsonDocument.parse("{id: '1234'}"); BsonDocument valueDoc = new BsonDocument("op", new BsonString("u")) .append("patch", new BsonString(UPDATE_DOC.toJson())); WriteModel<BsonDocument> result = UPDATE.perform(new SinkDocument(keyDoc, valueDoc)); assertTrue(result instanceof UpdateOneModel, "result expected to be of type UpdateOneModel"); UpdateOneModel<BsonDocument> writeModel = (UpdateOneModel<BsonDocument>) result; assertEquals(UPDATE_DOC, writeModel.getUpdate(), "update doc not matching what is expected"); assertTrue( writeModel.getFilter() instanceof BsonDocument, "filter expected to be of type BsonDocument"); assertEquals(FILTER_DOC, writeModel.getFilter()); }
Example #4
Source File: MongoDbHandler.java From mongo-kafka with Apache License 2.0 | 6 votes |
@Override public Optional<WriteModel<BsonDocument>> handle(final SinkDocument doc) { BsonDocument keyDoc = doc.getKeyDoc() .orElseThrow( () -> new DataException("Error: key document must not be missing for CDC mode")); BsonDocument valueDoc = doc.getValueDoc().orElseGet(BsonDocument::new); if (keyDoc.containsKey(JSON_ID_FIELD) && valueDoc.isEmpty()) { LOGGER.debug("skipping debezium tombstone event for kafka topic compaction"); return Optional.empty(); } LOGGER.debug("key: " + keyDoc.toString()); LOGGER.debug("value: " + valueDoc.toString()); return Optional.of(getCdcOperation(valueDoc).perform(doc)); }
Example #5
Source File: DeleteOneDefaultStrategy.java From kafka-connect-mongodb with Apache License 2.0 | 6 votes |
@Override public WriteModel<BsonDocument> createWriteModel(SinkDocument document) { BsonDocument kd = document.getKeyDoc().orElseThrow( () -> new DataException("error: cannot build the WriteModel since" + " the key document was missing unexpectedly") ); //NOTE: fallback for backwards / deprecation compatibility if(idStrategy == null) { return kd.containsKey(DBCollection.ID_FIELD_NAME) ? new DeleteOneModel<>(kd) : new DeleteOneModel<>(new BsonDocument(DBCollection.ID_FIELD_NAME,kd)); } //NOTE: current design doesn't allow to access original SinkRecord (= null) BsonValue _id = idStrategy.generateId(document,null); return new DeleteOneModel<>( new BsonDocument(DBCollection.ID_FIELD_NAME,_id) ); }
Example #6
Source File: GridFSTest.java From mongo-java-driver-reactivestreams with Apache License 2.0 | 6 votes |
private void actionGridFS(final BsonDocument action, final BsonDocument assertion) throws Throwable { if (action.isEmpty()) { return; } String operation = action.getString("operation").getValue(); if (operation.equals("delete")) { doDelete(action.getDocument("arguments"), assertion); } else if (operation.equals("download")) { doDownload(action.getDocument("arguments"), assertion); } else if (operation.equals("download_by_name")) { doDownloadByName(action.getDocument("arguments"), assertion); } else if (operation.equals("upload")) { doUpload(action.getDocument("arguments"), assertion); } else { throw new IllegalArgumentException("Unknown operation: " + operation); } }
Example #7
Source File: CaptureUtil.java From epcis with Apache License 2.0 | 6 votes |
public BsonDocument putQuantityList(BsonDocument base, List<QuantityElement> quantityList) { BsonArray quantityArray = new BsonArray(); for (QuantityElement quantityElement : quantityList) { BsonDocument bsonQuantityElement = new BsonDocument("epcClass", new BsonString(quantityElement.getEpcClass())); if (quantityElement.getQuantity() != null) { bsonQuantityElement.put("quantity", new BsonDouble(quantityElement.getQuantity())); } if (quantityElement.getUom() != null) { bsonQuantityElement.put("uom", new BsonString(quantityElement.getUom())); } quantityArray.add(bsonQuantityElement); } base.put("quantityList", quantityArray); return base; }
Example #8
Source File: ChangeEvent.java From stitch-android-sdk with Apache License 2.0 | 6 votes |
/** * Constructs a change event. * * @param id The id of the change event. * @param operationType The operation type represented by the change event. * @param fullDocument The full document at some point after the change is applied. * @param ns The namespace (database and collection) of the document. * @param documentKey The id if the underlying document that changed. * @param updateDescription The description of what has changed (for updates only). * @param hasUncommittedWrites Whether this represents a local uncommitted write. */ public ChangeEvent( final BsonDocument id, final OperationType operationType, final DocumentT fullDocument, final MongoNamespace ns, final BsonDocument documentKey, final UpdateDescription updateDescription, final boolean hasUncommittedWrites ) { super( operationType, fullDocument, documentKey, updateDescription, hasUncommittedWrites ); this.id = id; this.ns = ns; }
Example #9
Source File: MongoMetadataDaoImpl.java From eagle with Apache License 2.0 | 6 votes |
/** * get the basic ScheduleState, and then based on the version to get all sub-part(spoutSpecs/alertSpecs/etc) * to form a completed ScheduleState. * @return the latest ScheduleState */ @Override public ScheduleState getScheduleState() { BsonDocument sort = new BsonDocument(); sort.append("generateTime", new BsonInt32(-1)); ScheduleState state = scheduleStates.find().sort(sort).map(new Function<Document, ScheduleState>() { @Override public ScheduleState apply(Document t) { String json = t.toJson(); try { return mapper.readValue(json, ScheduleState.class); } catch (IOException e) { LOG.error("deserialize config item failed!", e); } return null; } }).first(); if (state != null) { String version = state.getVersion(); // based on version, to add content from collections of spoutSpecs/alertSpecs/etc.. state = addDetailForScheduleState(state, version); } return state; }
Example #10
Source File: ChronoGraph.java From epcis with Apache License 2.0 | 6 votes |
public TreeSet<Long> getTimestamps(Long startTime, Long endTime) { TreeSet<Long> timestampSet = new TreeSet<Long>(); Function<BsonDateTime, Long> mapper = new Function<BsonDateTime, Long>() { @Override public Long apply(BsonDateTime val) { return val.getValue(); } }; edgeEvents.distinct(Tokens.TIMESTAMP, BsonDateTime.class) .filter(new BsonDocument(Tokens.TIMESTAMP, new BsonDocument(Tokens.FC.$gt.toString(), new BsonDateTime(startTime)) .append(Tokens.FC.$lt.toString(), new BsonDateTime(endTime)))) .map(mapper).into(timestampSet); Set<Long> vtSet = new TreeSet<Long>(); vertexEvents.distinct(Tokens.TIMESTAMP, BsonDateTime.class) .filter(new BsonDocument(Tokens.TIMESTAMP, new BsonDocument(Tokens.FC.$gt.toString(), new BsonDateTime(startTime)) .append(Tokens.FC.$lt.toString(), new BsonDateTime(endTime)))) .map(mapper).into(timestampSet); timestampSet.addAll(vtSet); return timestampSet; }
Example #11
Source File: GridFSTest.java From mongo-java-driver-rx with Apache License 2.0 | 6 votes |
private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) { for (BsonValue rawDocument : bsonArray.getValues()) { if (rawDocument.isDocument()) { BsonDocument document = rawDocument.asDocument(); if (document.get("length").isInt32()) { document.put("length", new BsonInt64(document.getInt32("length").getValue())); } if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) { document.remove("metadata"); } if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) { document.remove("aliases"); } if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) { document.remove("contentType"); } documents.add(document); } } return documents; }
Example #12
Source File: ProvidedStrategy.java From mongo-kafka with Apache License 2.0 | 6 votes |
@Override public BsonValue generateId(final SinkDocument doc, final SinkRecord orig) { Optional<BsonDocument> optionalDoc = Optional.empty(); if (where.equals(ProvidedIn.KEY)) { optionalDoc = doc.getKeyDoc(); } if (where.equals(ProvidedIn.VALUE)) { optionalDoc = doc.getValueDoc(); } BsonValue id = optionalDoc .map(d -> d.get(ID_FIELD)) .orElseThrow( () -> new DataException( "Error: provided id strategy is used but the document structure either contained" + " no _id field or it was null")); if (id instanceof BsonNull) { throw new DataException( "Error: provided id strategy used but the document structure contained an _id of type BsonNull"); } return id; }
Example #13
Source File: SinkConverterTest.java From kafka-connect-mongodb with Apache License 2.0 | 6 votes |
@BeforeAll public static void initializeTestData() { JSON_STRING_1 = "{\"myField\":\"some text\"}"; OBJ_SCHEMA_1 = SchemaBuilder.struct() .field("myField", Schema.STRING_SCHEMA); OBJ_STRUCT_1 = new Struct(OBJ_SCHEMA_1) .put("myField", "some text"); OBJ_MAP_1 = new LinkedHashMap<>(); OBJ_MAP_1.put("myField", "some text"); EXPECTED_BSON_DOC = new BsonDocument("myField", new BsonString("some text")); combinations = new HashMap<>(); combinations.put(JSON_STRING_1, null); combinations.put(OBJ_STRUCT_1, OBJ_SCHEMA_1); combinations.put(OBJ_MAP_1, null); }
Example #14
Source File: MongoMetadataDaoImpl.java From eagle with Apache License 2.0 | 6 votes |
private <T> List<T> list(MongoCollection<Document> collection, Class<T> clz, String version) { BsonDocument doc = new BsonDocument(); doc.append("version", new BsonString(version)); List<T> result = new LinkedList<T>(); collection.find(doc).map(new Function<Document, T>() { @Override public T apply(Document t) { String json = t.toJson(); try { return mapper.readValue(json, clz); } catch (IOException e) { LOG.error("deserialize config item failed!", e); } return null; } }).into(result); return result; }
Example #15
Source File: MongoCopyDataManager.java From mongo-kafka with Apache License 2.0 | 5 votes |
private void copyDataFrom(final MongoNamespace namespace) { LOGGER.debug("Copying existing data from: {}", namespace.getFullName()); try { mongoClient .getDatabase(namespace.getDatabaseName()) .getCollection(namespace.getCollectionName(), BsonDocument.class) .aggregate(createPipeline(namespace)) .forEach((Consumer<? super BsonDocument>) this::putToQueue); namespacesToCopy.decrementAndGet(); } catch (Exception e) { errorException = e; } }
Example #16
Source File: ChangeEvents.java From stitch-android-sdk with Apache License 2.0 | 5 votes |
/** * Generates a change event for a local replacement of a document in the given namespace referring * to the given document _id. * * @param documentId the _id of the document that was updated. * @param document the replacement document. * @return a change event for a local replacement of a document in the given namespace referring * to the given document _id. */ static CompactChangeEvent<BsonDocument> compactChangeEventForLocalReplace( final BsonValue documentId, final BsonDocument document, final boolean writePending ) { return new CompactChangeEvent<>( OperationType.REPLACE, document, new BsonDocument("_id", documentId), null, versionForDocument(document), hashForDocument(document), writePending); }
Example #17
Source File: CaptureUtil.java From epcis with Apache License 2.0 | 5 votes |
public BsonDocument putAttributes(BsonDocument base, Map<String, String> attributes) { BsonDocument bsonAttributes = new BsonDocument(); for (String key : attributes.keySet()) { String value = attributes.get(key); bsonAttributes.put(encodeMongoObjectKey(key), new BsonString(value)); } base.put("attributes", bsonAttributes); return base; }
Example #18
Source File: RdbmsHandlerTest.java From mongo-kafka with Apache License 2.0 | 5 votes |
@Test @DisplayName("when value doc contains operation type other than string then DataException") void testInvalidCdcOperationType() { SinkDocument cdcEvent = new SinkDocument(BsonDocument.parse("{id: 1234}"), BsonDocument.parse("{op: 'c'}")); assertThrows(DataException.class, () -> RDBMS_HANDLER_DEFAULT_MAPPING.handle(cdcEvent)); }
Example #19
Source File: RdbmsHandlerTest.java From mongo-kafka with Apache License 2.0 | 5 votes |
@Test @DisplayName("when value doc contains unmapped operation type then DataException") void testUnmappedCdcOperationType() { SinkDocument cdcEvent = new SinkDocument( BsonDocument.parse("{id: 1234}"), BsonDocument.parse("{op: 'c', after: {id: 1234, foo: 'bar'}}")); assertThrows(DataException.class, () -> RDBMS_HANDLER_EMPTY_MAPPING.handle(cdcEvent)); }
Example #20
Source File: MongoQueryUtil.java From epcis with Apache License 2.0 | 5 votes |
static BsonDocument getNearQueryObject(String key, String paramValues) { try { // Prepare Query Values String[] paramValueArr = paramValues.split(","); if (paramValueArr.length < 2) return null; Double lon = Double.parseDouble(paramValueArr[0]); Double lat = Double.parseDouble(paramValueArr[1]); Integer min = null; if (paramValueArr.length > 2) min = Integer.parseInt(paramValueArr[2]); Integer max = null; if (paramValueArr.length > 3) max = Integer.parseInt(paramValueArr[3]); BsonDocument near = new BsonDocument(); BsonDocument geometry = new BsonDocument("type", new BsonString("Point")); BsonArray coordinates = new BsonArray(); coordinates.add(new BsonDouble(lon)); coordinates.add(new BsonDouble(lat)); geometry.put("coordinates", coordinates); near.put("$geometry", geometry); if (min != null) near.put("$minDistance", new BsonInt32(min)); if (max != null) near.put("$maxDistance", new BsonInt32(max)); return new BsonDocument("any." + key, new BsonDocument("$near", near)); } catch (NumberFormatException e) { e.printStackTrace(); return null; } }
Example #21
Source File: PartialKeyStrategy.java From kafka-connect-mongodb with Apache License 2.0 | 5 votes |
@Override public BsonValue generateId(SinkDocument doc, SinkRecord orig) { fieldProjector.process(doc,orig); //NOTE: If there is no key doc present the strategy //simply returns an empty BSON document per default. return doc.getKeyDoc().orElseGet(() -> new BsonDocument()); }
Example #22
Source File: MongoQueryUtil.java From epcis with Apache License 2.0 | 5 votes |
static BsonDocument getQueryObject(String[] fieldArr, BsonArray paramArray) { BsonArray orQueries = new BsonArray(); for (String field : fieldArr) { Iterator<BsonValue> paramIterator = paramArray.iterator(); BsonArray pureStringParamArray = new BsonArray(); while (paramIterator.hasNext()) { BsonValue param = paramIterator.next(); if (param instanceof BsonRegularExpression) { BsonDocument regexQuery = new BsonDocument(field, new BsonDocument("$regex", param)); orQueries.add(regexQuery); } else { pureStringParamArray.add(param); } } if (pureStringParamArray.size() != 0) { BsonDocument stringInQueries = new BsonDocument(field, new BsonDocument("$in", pureStringParamArray)); orQueries.add(stringInQueries); } } if (orQueries.size() != 0) { BsonDocument queryObject = new BsonDocument(); queryObject.put("$or", orQueries); return queryObject; } else { return null; } }
Example #23
Source File: MongoCaptureUtil.java From epcis with Apache License 2.0 | 5 votes |
public void captureJSONEvent(JSONObject event) { MongoCollection<BsonDocument> collection = Configuration.mongoDatabase.getCollection("EventData", BsonDocument.class); BsonDocument dbObject = BsonDocument.parse(event.toString()); if (Configuration.isTriggerSupported == true) { TriggerEngine.examineAndFire("EventData", dbObject); } collection.insertOne(dbObject); Configuration.logger.info(" Event Saved "); }
Example #24
Source File: TraceMongoDbAutoConfigurationTests.java From spring-cloud-sleuth with Apache License 2.0 | 5 votes |
@Override public void customize(MongoClientSettings.Builder clientSettingsBuilder) { super.customize(clientSettingsBuilder); CommandListener listener = clientSettingsBuilder.build().getCommandListeners() .get(0); listener.commandStarted(new CommandStartedEvent(0, null, "", "", BDDMockito.mock(BsonDocument.class))); listener.commandSucceeded(new CommandSucceededEvent(1, null, "", BDDMockito.mock(BsonDocument.class), 100)); }
Example #25
Source File: ChronoGraph.java From epcis with Apache License 2.0 | 5 votes |
/** * Return an iterable to all the edges in the graph. If this is not possible for * the implementation, then an UnsupportedOperationException can be thrown. * * @param labels * @return iterable Edge Identifiers having the given label sets */ public Iterable<ChronoEdge> getEdges(final BsonArray labels) { HashSet<ChronoEdge> edgeSet = new HashSet<ChronoEdge>(); BsonDocument filter = new BsonDocument(); BsonDocument inner = new BsonDocument(); inner.put(Tokens.FC.$in.toString(), labels); filter.put(Tokens.LABEL, inner); Iterator<BsonDocument> it = edges.find(filter).iterator(); while (it.hasNext()) { BsonDocument d = it.next(); edgeSet.add(new ChronoEdge(d.getString(Tokens.ID).getValue(), this)); } return edgeSet; }
Example #26
Source File: TriggerEngine.java From epcis with Apache License 2.0 | 5 votes |
private static Set<String> addEPCtoSet(Set<String> epcSet, BsonArray epcList) { Iterator<BsonValue> epcIterator = epcList.iterator(); while (epcIterator.hasNext()) { BsonDocument epcDocument = epcIterator.next().asDocument(); epcSet.add(epcDocument.getString("epc").getValue()); } return epcSet; }
Example #27
Source File: DittoBsonJsonTest.java From ditto with Eclipse Public License 2.0 | 5 votes |
@Test public void serializeJsonWithUnicodeDollarsInKeys() throws JSONException { final BsonDocument parse = BsonDocument.parse(JSON_WITH_UNICODE_DOLLAR_INKEYS); final JsonValue serialized = underTest.serialize(parse); JSONAssert.assertEquals(JSON_WITH_DOLLAR_INKEYS, serialized.toString(), true); }
Example #28
Source File: DocumentIdAdderTest.java From kafka-connect-mongodb with Apache License 2.0 | 5 votes |
@Test @DisplayName("test _id field added by IdStrategy") public void testAddingIdFieldByStrategy() { BsonValue fakeId = mock(BsonValue.class); IdStrategy ids = mock(IdStrategy.class); when(ids.generateId(any(SinkDocument.class), ArgumentMatchers.isNull())) .thenReturn(fakeId); DocumentIdAdder idAdder = new DocumentIdAdder(null,ids,""); SinkDocument sinkDocWithValueDoc = new SinkDocument(null,new BsonDocument()); SinkDocument sinkDocWithoutValueDoc = new SinkDocument(null,null); assertAll("check for _id field when processing DocumentIdAdder", () -> { idAdder.process(sinkDocWithValueDoc,null); assertAll("_id checks", () -> assertTrue(sinkDocWithValueDoc.getValueDoc().orElseGet(() -> new BsonDocument()) .keySet().contains(DBCollection.ID_FIELD_NAME), "must contain _id field in valueDoc" ), () -> assertTrue(sinkDocWithValueDoc.getValueDoc().orElseGet(() -> new BsonDocument()) .get(DBCollection.ID_FIELD_NAME) instanceof BsonValue, "_id field must be of type BsonValue") ); }, () -> { idAdder.process(sinkDocWithoutValueDoc,null); assertTrue(!sinkDocWithoutValueDoc.getValueDoc().isPresent(), "no _id added since valueDoc cannot not be present" ); } ); }
Example #29
Source File: RecordToBson.java From octarine with Apache License 2.0 | 5 votes |
@Test public void convert_basic_record_to_bson() { Record person = Record.of( Person.name.of("Dominic"), Person.age.of(39)); BsonRecordSerialiser serializer = BsonRecordSerialiser.builder() .writeString(Person.name) .writeInteger(Person.age) .get(); BsonDocument doc = (BsonDocument) serializer.apply(person); assertEquals("Invalid name", "Dominic", doc.getString("name").getValue()); assertEquals("invalid age", 39, doc.getInt32("age").getValue()); }
Example #30
Source File: DocumentVersionInfo.java From stitch-android-sdk with Apache License 2.0 | 5 votes |
/** * Returns the current version info for a provided remote document. * @param remoteDocument the remote BSON document from which to extract version info * @return a DocumentVersionInfo */ static DocumentVersionInfo getRemoteVersionInfo(final BsonDocument remoteDocument) { final BsonDocument version = getDocumentVersionDoc(remoteDocument); return new DocumentVersionInfo( version, remoteDocument != null ? BsonUtils.getDocumentId(remoteDocument) : null ); }