org.bson.BSONException Java Examples

The following examples show how to use org.bson.BSONException. 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: ReplaceOneBusinessKeyStrategy.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
@Override
public WriteModel<BsonDocument> createWriteModel(final SinkDocument document) {
  BsonDocument vd =
      document
          .getValueDoc()
          .orElseThrow(
              () ->
                  new DataException(
                      "Error: cannot build the WriteModel since the value document was missing unexpectedly"));

  try {
    BsonDocument businessKey = vd.getDocument(ID_FIELD);
    vd.remove(ID_FIELD);
    return new ReplaceOneModel<>(businessKey, vd, REPLACE_OPTIONS);
  } catch (BSONException e) {
    throw new DataException(
        "Error: cannot build the WriteModel since the value document does not contain an _id field of"
            + " type BsonDocument which holds the business key fields");
  }
}
 
Example #2
Source File: UpdateOneBusinessKeyTimestampStrategy.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public WriteModel<BsonDocument> createWriteModel(final SinkDocument document) {
  BsonDocument vd =
      document
          .getValueDoc()
          .orElseThrow(
              () ->
                  new DataException(
                      "Error: cannot build the WriteModel since the value document was missing unexpectedly"));

  BsonDateTime dateTime = new BsonDateTime(Instant.now().toEpochMilli());

  try {
    BsonDocument businessKey = vd.getDocument(ID_FIELD);
    vd.remove(ID_FIELD);

    return new UpdateOneModel<>(
        businessKey,
        new BsonDocument("$set", vd.append(FIELD_NAME_MODIFIED_TS, dateTime))
            .append("$setOnInsert", new BsonDocument(FIELD_NAME_INSERTED_TS, dateTime)),
        UPDATE_OPTIONS);

  } catch (BSONException e) {
    throw new DataException(
        "Error: cannot build the WriteModel since the value document does not contain an _id field of"
            + " type BsonDocument which holds the business key fields");
  }
}
 
Example #3
Source File: MongoSink.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void flush() {
    final List<Document> docsToInsert = new ArrayList<>();
    final List<Record<byte[]>> recordsToInsert;

    synchronized (this) {
        if (incomingList.isEmpty()) {
            return;
        }

        recordsToInsert = incomingList;
        incomingList = Lists.newArrayList();
    }

    final Iterator<Record<byte[]>> iter = recordsToInsert.iterator();

    while (iter.hasNext()) {
        final Record<byte[]> record = iter.next();

        try {
            final byte[] docAsBytes = record.getValue();
            final Document doc = Document.parse(new String(docAsBytes, StandardCharsets.UTF_8));
            docsToInsert.add(doc);
        }
        catch (JsonParseException | BSONException e) {
            log.error("Bad message", e);
            record.fail();
            iter.remove();
        }
    }

    if (docsToInsert.size() > 0) {
        collection.insertMany(docsToInsert).subscribe(new DocsToInsertSubscriber(docsToInsert,recordsToInsert));
    }
}
 
Example #4
Source File: URICodec.java    From morphia with Apache License 2.0 5 votes vote down vote up
@Override
public void encode(final BsonWriter writer, final URI value, final EncoderContext encoderContext) {
    try {
        writer.writeString(value.toURL().toExternalForm());
    } catch (MalformedURLException e) {
        throw new BSONException("Could not serialize the URI: " + value);
    }
}