Java Code Examples for org.bson.BsonNull#VALUE

The following examples show how to use org.bson.BsonNull#VALUE . 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: AvroJsonSchemafulRecordConverter.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
private BsonValue toBsonArray(final Schema schema, final Object value) {
  if (value == null) {
    return BsonNull.VALUE;
  }
  Schema fieldSchema = schema.valueSchema();
  BsonArray bsonArray = new BsonArray();
  List<?> myList = (List) value;
  myList.forEach(
      v -> {
        if (fieldSchema.type().isPrimitive()) {
          if (v == null) {
            bsonArray.add(BsonNull.VALUE);
          } else {
            bsonArray.add(getConverter(fieldSchema).toBson(v));
          }
        } else if (fieldSchema.type().equals(ARRAY)) {
          bsonArray.add(toBsonArray(fieldSchema, v));
        } else {
          bsonArray.add(toBsonDoc(fieldSchema, v));
        }
      });
  return bsonArray;
}
 
Example 2
Source File: SinkFieldConverter.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
public BsonValue toBson(final Object data, final Schema fieldSchema) {
  if (!fieldSchema.isOptional()) {
    if (data == null) {
      throw new DataException("Error: schema not optional but data was null");
    }
    LOGGER.trace("field not optional and data is '{}'", data.toString());
    return toBson(data);
  }

  if (data != null) {
    LOGGER.trace("field optional and data is '{}'", data.toString());
    return toBson(data);
  }

  if (fieldSchema.defaultValue() != null) {
    LOGGER.trace(
        "field optional and no data but default value is '{}'",
        fieldSchema.defaultValue().toString());
    return toBson(fieldSchema.defaultValue());
  }

  LOGGER.trace("field optional, no data and no default value thus '{}'", BsonNull.VALUE);
  return BsonNull.VALUE;
}
 
Example 3
Source File: AvroJsonSchemafulRecordConverter.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
private BsonValue handleArrayField(List list, Field field) {
    logger.trace("handling complex type 'array' of types '{}'",
       field.schema().valueSchema().type());
    if(list==null) {
        logger.trace("no array -> adding null");
        return BsonNull.VALUE;
    }
    BsonArray array = new BsonArray();
    Schema.Type st = field.schema().valueSchema().type();
    for(Object element : list) {
        if(st.isPrimitive()) {
            array.add(getConverter(field.schema().valueSchema()).toBson(element,field.schema()));
        } else if(st == Schema.Type.ARRAY) {
            Field elementField = new Field("first", 0, field.schema().valueSchema());
            array.add(handleArrayField((List)element,elementField));
        } else {
            array.add(toBsonDoc(field.schema().valueSchema(), element));
        }
    }
    return array;
}
 
Example 4
Source File: SinkFieldConverter.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
public BsonValue toBson(Object data, Schema fieldSchema) {
    if(!fieldSchema.isOptional()) {

        if(data == null)
            throw new DataException("error: schema not optional but data was null");

        logger.trace("field not optional and data is '{}'",data.toString());
        return toBson(data);
    }

    if(data != null) {
        logger.trace("field optional and data is '{}'",data.toString());
        return toBson(data);
    }

    if(fieldSchema.defaultValue() != null) {
        logger.trace("field optional and no data but default value is '{}'",fieldSchema.defaultValue().toString());
        return toBson(fieldSchema.defaultValue());
    }

    logger.trace("field optional, no data and no default value thus '{}'", BsonNull.VALUE);
    return BsonNull.VALUE;
}
 
Example 5
Source File: TupleCodecProvider.java    From immutables with Apache License 2.0 6 votes vote down vote up
private static BsonValue resolveOrNull(BsonValue value, List<String> paths) {
  if (paths.isEmpty()) {
    return value;
  }

  if (!value.isDocument()) {
    return BsonNull.VALUE;
  }

  BsonDocument document = value.asDocument();
  final String first = paths.get(0);
  if (!document.containsKey(first)) {
    return BsonNull.VALUE;
  }

  return resolveOrNull(document.get(first), paths.subList(1, paths.size()));
}
 
Example 6
Source File: FindVisitor.java    From immutables with Apache License 2.0 6 votes vote down vote up
@Override
public BsonValue visit(Constant constant) {
  Object value = constant.value();
  if (value == null) {
    return BsonNull.VALUE;
  }

  if (value instanceof Iterable) {
    return Filters.in("ignore", (Iterable<?>) value)
            .toBsonDocument(BsonDocument.class, codecRegistry)
            .get("ignore").asDocument()
            .get("$in").asArray();
  }

  return Filters.eq("ignore", value)
          .toBsonDocument(BsonDocument.class, codecRegistry)
          .get("ignore");
}
 
Example 7
Source File: AvroJsonSchemafulRecordConverter.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
private BsonValue toBsonDoc(final Schema schema, final Object value) {
  if (value == null) {
    return BsonNull.VALUE;
  }
  BsonDocument doc = new BsonDocument();
  if (schema.type() == MAP) {
    Schema fieldSchema = schema.valueSchema();
    Map m = (Map) value;
    for (Object entry : m.keySet()) {
      String key = (String) entry;
      if (fieldSchema.type().isPrimitive()) {
        doc.put(key, getConverter(fieldSchema).toBson(m.get(key), fieldSchema));
      } else if (fieldSchema.type().equals(ARRAY)) {
        doc.put(key, toBsonArray(fieldSchema, m.get(key)));
      } else {
        if (m.get(key) == null) {
          doc.put(key, BsonNull.VALUE);
        } else {
          doc.put(key, toBsonDoc(fieldSchema, m.get(key)));
        }
      }
    }
  } else {
    schema.fields().forEach(f -> doc.put(f.name(), processField((Struct) value, f)));
  }
  return doc;
}
 
Example 8
Source File: AvroJsonSchemafulRecordConverter.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
private BsonValue processField(final Struct struct, final Field field) {
  LOGGER.trace("processing field '{}'", field.name());

  if (struct.get(field.name()) == null) {
    LOGGER.trace("no field in struct -> adding null");
    return BsonNull.VALUE;
  }

  if (isSupportedLogicalType(field.schema())) {
    return getConverter(field.schema()).toBson(struct.get(field), field.schema());
  }

  try {
    switch (field.schema().type()) {
      case BOOLEAN:
      case FLOAT32:
      case FLOAT64:
      case INT8:
      case INT16:
      case INT32:
      case INT64:
      case STRING:
      case BYTES:
        return handlePrimitiveField(struct, field);
      case STRUCT:
      case MAP:
        return toBsonDoc(field.schema(), struct.get(field));
      case ARRAY:
        return toBsonArray(field.schema(), struct.get(field));
      default:
        throw new DataException("unexpected / unsupported schema type " + field.schema().type());
    }
  } catch (Exception exc) {
    throw new DataException("error while processing field " + field.name(), exc);
  }
}
 
Example 9
Source File: MongoDbHandlerTest.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when value doc is missing operation type then DataException")
void testMissingCdcOperationType() {
  SinkDocument cdcEvent =
      new SinkDocument(
          new BsonDocument("id", new BsonInt32(1234)), new BsonDocument("po", BsonNull.VALUE));

  assertThrows(DataException.class, () -> HANDLER_DEFAULT_MAPPING.handle(cdcEvent));
}
 
Example 10
Source File: RdbmsHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when value doc is missing operation type then DataException")
public void testMissingCdcOperationType() {
    SinkDocument cdcEvent = new SinkDocument(
            new BsonDocument("id",new BsonInt32(1234)),
            new BsonDocument("po",BsonNull.VALUE)
    );
    assertThrows(DataException.class, () ->
            RDBMS_HANDLER_DEFAULT_MAPPING.handle(cdcEvent)
    );
}
 
Example 11
Source File: MongoDbHandlerTest.java    From kafka-connect-mongodb with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("when value doc is missing operation type then DataException")
public void testMissingCdcOperationType() {
    SinkDocument cdcEvent = new SinkDocument(
            new BsonDocument("id",new BsonInt32(1234)),
            new BsonDocument("po", BsonNull.VALUE)
    );
    assertThrows(DataException.class, () ->
            MONGODB_HANDLER_DEFAULT_MAPPING.handle(cdcEvent)
    );
}
 
Example 12
Source File: JsonToBson.java    From ditto with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public BsonValue nullValue() {
    return BsonNull.VALUE;
}