Java Code Examples for org.apache.kafka.connect.data.Schema#defaultValue()

The following examples show how to use org.apache.kafka.connect.data.Schema#defaultValue() . 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: 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 2
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 3
Source File: SchemaSerializationModule.java    From connect-utils with Apache License 2.0 6 votes vote down vote up
Storage(Schema schema) {
  this.name = schema.name();
  this.doc = schema.doc();
  this.type = schema.type();
  this.defaultValue = schema.defaultValue();
  this.version = schema.version();
  this.parameters = schema.parameters();
  this.isOptional = schema.isOptional();

  if (Schema.Type.MAP == this.type) {
    this.keySchema = schema.keySchema();
    this.valueSchema = schema.valueSchema();
  } else if (Schema.Type.ARRAY == this.type) {
    this.keySchema = null;
    this.valueSchema = schema.valueSchema();
  } else if (Schema.Type.STRUCT == this.type) {
    this.fieldSchemas = new LinkedHashMap<>();
    for (Field field : schema.fields()) {
      this.fieldSchemas.put(field.name(), field.schema());
    }
  }
}
 
Example 4
Source File: AvroData.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
private void addAvroRecordField(
    List<org.apache.avro.Schema.Field> fields,
    String fieldName, Schema fieldSchema,
    FromConnectContext fromConnectContext) {

    Object defaultVal = null;
    if (fieldSchema.defaultValue() != null) {
        defaultVal = fieldSchema.defaultValue();

        // If this is a logical, convert to the primitive form for the Avro default value
        defaultVal = toAvroLogical(fieldSchema, defaultVal);

        // Avro doesn't handle a few types that Connect uses, so convert those explicitly here
        if (defaultVal instanceof Byte) {
            // byte are mapped to integers in Avro
            defaultVal = ((Byte) defaultVal).intValue();
        } else if (defaultVal instanceof Short) {
            // Shorts are mapped to integers in Avro
            defaultVal = ((Short) defaultVal).intValue();
        } else if (defaultVal instanceof ByteBuffer) {
            // Avro doesn't handle ByteBuffer directly, but does handle 'byte[]'
            // Copy the contents of the byte buffer without side effects on the buffer
            ByteBuffer buffer = (ByteBuffer) defaultVal;
            byte[] bytes = new byte[buffer.remaining()];
            buffer.duplicate().get(bytes);
            defaultVal = bytes;
        }
    } else if (fieldSchema.isOptional()) {
        defaultVal = JsonProperties.NULL_VALUE;
    }
    org.apache.avro.Schema.Field field = new org.apache.avro.Schema.Field(
        fieldName,
        fromConnectSchema(fieldSchema, fromConnectContext, false),
        fieldSchema.doc(),
        defaultVal);
    fields.add(field);
}
 
Example 5
Source File: AvroData.java    From apicurio-registry with Apache License 2.0 5 votes vote down vote up
public static Schema nonOptional(Schema schema) {
    return new ConnectSchema(schema.type(), false, schema.defaultValue(), schema.name(),
                             schema.version(), schema.doc(),
                             schema.parameters(),
                             fields(schema),
                             keySchema(schema),
                             valueSchema(schema));
}
 
Example 6
Source File: CamelTypeConverterTransform.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
private Schema getOrBuildRecordSchema(final Schema originalSchema, final Object value) {
    final SchemaBuilder builder = SchemaUtil.copySchemaBasics(originalSchema, SchemaHelper.buildSchemaBuilderForType(value));

    if (originalSchema.isOptional()) {
        builder.optional();
    }
    if (originalSchema.defaultValue() != null) {
        builder.defaultValue(convertValueWithCamelTypeConverter(originalSchema.defaultValue()));
    }

    return builder.build();
}
 
Example 7
Source File: DataConverter.java    From jkes with Apache License 2.0 5 votes vote down vote up
private static SchemaBuilder copySchemaBasics(Schema source, SchemaBuilder target) {
  if (source.isOptional()) {
    target.optional();
  }
  if (source.defaultValue() != null && source.type() != Schema.Type.STRUCT) {
    final Object preProcessedDefaultValue = preProcessValue(source.defaultValue(), source, target);
    target.defaultValue(preProcessedDefaultValue);
  }
  return target;
}
 
Example 8
Source File: PatternRename.java    From kafka-connect-transform-common with Apache License 2.0 5 votes vote down vote up
@Override
protected SchemaAndValue processStruct(R record, Schema inputSchema, Struct inputStruct) {
  final SchemaBuilder outputSchemaBuilder = SchemaBuilder.struct();
  outputSchemaBuilder.name(inputSchema.name());
  outputSchemaBuilder.doc(inputSchema.doc());
  if (null != inputSchema.defaultValue()) {
    outputSchemaBuilder.defaultValue(inputSchema.defaultValue());
  }
  if (null != inputSchema.parameters() && !inputSchema.parameters().isEmpty()) {
    outputSchemaBuilder.parameters(inputSchema.parameters());
  }
  if (inputSchema.isOptional()) {
    outputSchemaBuilder.optional();
  }
  Map<String, String> fieldMappings = new HashMap<>(inputSchema.fields().size());
  for (final Field inputField : inputSchema.fields()) {
    log.trace("process() - Processing field '{}'", inputField.name());
    final Matcher fieldMatcher = this.config.pattern.matcher(inputField.name());
    final String outputFieldName;
    if (fieldMatcher.find()) {
      outputFieldName = fieldMatcher.replaceAll(this.config.replacement);
    } else {
      outputFieldName = inputField.name();
    }
    log.trace("process() - Mapping field '{}' to '{}'", inputField.name(), outputFieldName);
    fieldMappings.put(inputField.name(), outputFieldName);
    outputSchemaBuilder.field(outputFieldName, inputField.schema());
  }
  final Schema outputSchema = outputSchemaBuilder.build();
  final Struct outputStruct = new Struct(outputSchema);
  for (Map.Entry<String, String> entry : fieldMappings.entrySet()) {
    final String inputField = entry.getKey(), outputField = entry.getValue();
    log.trace("process() - Copying '{}' to '{}'", inputField, outputField);
    final Object value = inputStruct.get(inputField);
    outputStruct.put(outputField, value);
  }
  return new SchemaAndValue(outputSchema, outputStruct);
}
 
Example 9
Source File: AssertSchema.java    From connect-utils with Apache License 2.0 4 votes vote down vote up
public static void assertSchema(final Schema expected, final Schema actual, String message) {
  final String prefix = Strings.isNullOrEmpty(message) ? "" : message + ": ";

  if (null == expected) {
    assertNull(actual, prefix + "actual should not be null.");
    return;
  }

  assertNotNull(expected, prefix + "expected schema should not be null.");
  assertNotNull(actual, prefix + "actual schema should not be null.");
  assertEquals(expected.name(), actual.name(), prefix + "schema.name() should match.");
  assertEquals(expected.type(), actual.type(), prefix + "schema.type() should match.");
  assertEquals(expected.defaultValue(), actual.defaultValue(), prefix + "schema.defaultValue() should match.");
  assertEquals(expected.isOptional(), actual.isOptional(), prefix + "schema.isOptional() should match.");
  assertEquals(expected.doc(), actual.doc(), prefix + "schema.doc() should match.");
  assertEquals(expected.version(), actual.version(), prefix + "schema.version() should match.");
  assertMap(expected.parameters(), actual.parameters(), prefix + "schema.parameters() should match.");

  if (null != expected.defaultValue()) {
    assertNotNull(actual.defaultValue(), "actual.defaultValue() should not be null.");

    Class<?> expectedType = null;

    switch (expected.type()) {
      case INT8:
        expectedType = Byte.class;
        break;
      case INT16:
        expectedType = Short.class;
        break;
      case INT32:
        expectedType = Integer.class;
        break;
      case INT64:
        expectedType = Long.class;
        break;
      case FLOAT32:
        expectedType = Float.class;
        break;
      case FLOAT64:
        expectedType = Float.class;
        break;
      default:
        break;
    }
    if (null != expectedType) {
      assertTrue(
          actual.defaultValue().getClass().isAssignableFrom(expectedType),
          String.format("actual.defaultValue() should be a %s", expectedType.getSimpleName())
      );
    }
  }

  switch (expected.type()) {
    case ARRAY:
      assertSchema(expected.valueSchema(), actual.valueSchema(), message + "valueSchema does not match.");
      break;
    case MAP:
      assertSchema(expected.keySchema(), actual.keySchema(), message + "keySchema does not match.");
      assertSchema(expected.valueSchema(), actual.valueSchema(), message + "valueSchema does not match.");
      break;
    case STRUCT:
      List<Field> expectedFields = expected.fields();
      List<Field> actualFields = actual.fields();
      assertEquals(expectedFields.size(), actualFields.size(), prefix + "Number of fields do not match.");
      for (int i = 0; i < expectedFields.size(); i++) {
        Field expectedField = expectedFields.get(i);
        Field actualField = actualFields.get(i);
        assertField(expectedField, actualField, "index " + i);
      }
      break;
  }
}
 
Example 10
Source File: AssertSchema.java    From connect-utils with Apache License 2.0 4 votes vote down vote up
public static void assertSchema(final Schema expected, final Schema actual, String message) {
  final String prefix = Strings.isNullOrEmpty(message) ? "" : message + ": ";

  if (null == expected) {
    assertNull(actual, prefix + "actual should not be null.");
    return;
  }

  assertNotNull(expected, prefix + "expected schema should not be null.");
  assertNotNull(actual, prefix + "actual schema should not be null.");
  assertEquals(expected.name(), actual.name(), prefix + "schema.name() should match.");
  assertEquals(expected.type(), actual.type(), prefix + "schema.type() should match.");
  assertEquals(expected.defaultValue(), actual.defaultValue(), prefix + "schema.defaultValue() should match.");
  assertEquals(expected.isOptional(), actual.isOptional(), prefix + "schema.isOptional() should match.");
  assertEquals(expected.doc(), actual.doc(), prefix + "schema.doc() should match.");
  assertEquals(expected.version(), actual.version(), prefix + "schema.version() should match.");
  assertMap(expected.parameters(), actual.parameters(), prefix + "schema.parameters() should match.");

  if (null != expected.defaultValue()) {
    assertNotNull(actual.defaultValue(), "actual.defaultValue() should not be null.");

    Class<?> expectedType = null;

    switch (expected.type()) {
      case INT8:
        expectedType = Byte.class;
        break;
      case INT16:
        expectedType = Short.class;
        break;
      case INT32:
        expectedType = Integer.class;
        break;
      case INT64:
        expectedType = Long.class;
        break;
      case FLOAT32:
        expectedType = Float.class;
        break;
      case FLOAT64:
        expectedType = Float.class;
        break;
      default:
        break;
    }
    if (null != expectedType) {
      assertTrue(
          actual.defaultValue().getClass().isAssignableFrom(expectedType),
          String.format("actual.defaultValue() should be a %s", expectedType.getSimpleName())
      );
    }
  }

  switch (expected.type()) {
    case ARRAY:
      assertSchema(expected.valueSchema(), actual.valueSchema(), message + "valueSchema does not match.");
      break;
    case MAP:
      assertSchema(expected.keySchema(), actual.keySchema(), message + "keySchema does not match.");
      assertSchema(expected.valueSchema(), actual.valueSchema(), message + "valueSchema does not match.");
      break;
    case STRUCT:
      List<Field> expectedFields = expected.fields();
      List<Field> actualFields = actual.fields();
      assertEquals(expectedFields.size(), actualFields.size(), prefix + "Number of fields do not match.");
      for (int i = 0; i < expectedFields.size(); i++) {
        Field expectedField = expectedFields.get(i);
        Field actualField = actualFields.get(i);
        assertField(expectedField, actualField, "index " + i);
      }
      break;
  }
}