Java Code Examples for org.apache.kafka.connect.transforms.Transformation#configure()

The following examples show how to use org.apache.kafka.connect.transforms.Transformation#configure() . 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: CamelTypeConverterTransformTest.java    From camel-kafka-connector with Apache License 2.0 6 votes vote down vote up
@Test
public void testIfItConvertsConnectRecordCorrectly() {
    final SourceRecord connectRecord = new SourceRecord(Collections.emptyMap(), Collections.emptyMap(), "topic", Schema.STRING_SCHEMA, "1234", Schema.STRING_SCHEMA, "TRUE");

    final Map<String, Object> propsForKeySmt = new HashMap<>();
    propsForKeySmt.put(CamelTypeConverterTransform.FIELD_TARGET_TYPE_CONFIG, Integer.class.getName());

    final Map<String, Object> propsForValueSmt = new HashMap<>();
    propsForValueSmt.put(CamelTypeConverterTransform.FIELD_TARGET_TYPE_CONFIG, "java.lang.Boolean");

    final Transformation<SourceRecord> transformationKey = new CamelTypeConverterTransform.Key<>();
    final Transformation<SourceRecord> transformationValue = new CamelTypeConverterTransform.Value<>();

    transformationKey.configure(propsForKeySmt);
    transformationValue.configure(propsForValueSmt);

    final SourceRecord transformedKeySourceRecord = transformationKey.apply(connectRecord);
    final SourceRecord transformedValueSourceRecord = transformationValue.apply(connectRecord);

    assertEquals(1234, transformedKeySourceRecord.key());
    assertEquals(Schema.INT32_SCHEMA, transformedKeySourceRecord.keySchema());

    assertEquals(true, transformedValueSourceRecord.value());
    assertEquals(Schema.BOOLEAN_SCHEMA, transformedValueSourceRecord.valueSchema());
}
 
Example 2
Source File: CamelTypeConverterTransformTest.java    From camel-kafka-connector with Apache License 2.0 5 votes vote down vote up
@Test
public void testIfHandlesTypeConvertersFromCamelComponents() {
    // we know we have a type converter from struct to map in dbz component, so we use this for testing
    final Schema schema = SchemaBuilder.struct()
            .field("id", Schema.INT32_SCHEMA)
            .field("name", Schema.STRING_SCHEMA)
            .field("valid", Schema.BOOLEAN_SCHEMA)
            .field("extra", Schema.STRING_SCHEMA)
            .build();

    final Struct value = new Struct(schema);
    value.put("id", 12);
    value.put("name", "test-name");
    value.put("valid", true);

    final SourceRecord connectRecord = new SourceRecord(Collections.emptyMap(), Collections.emptyMap(), "topic", Schema.STRING_SCHEMA, "1234", schema, value);

    final Map<String, Object> props = new HashMap<>();
    props.put(CamelTypeConverterTransform.FIELD_TARGET_TYPE_CONFIG, Map.class.getName());

    final Transformation<SourceRecord> transformationValue = new CamelTypeConverterTransform.Value<>();

    transformationValue.configure(props);

    final SourceRecord transformedValueSourceRecord = transformationValue.apply(connectRecord);

    // assert
    assertNotNull(transformedValueSourceRecord);

    final Map<String, Object> outputValue = (Map<String, Object>) transformedValueSourceRecord.value();

    assertEquals(12, outputValue.get("id"));
    assertEquals("test-name", outputValue.get("name"));
    assertNull(outputValue.get("extra"));
    assertTrue((boolean)outputValue.get("valid"));
    assertEquals(Schema.Type.MAP, transformedValueSourceRecord.valueSchema().type());
}
 
Example 3
Source File: SourceConfig.java    From mirus with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private List<Transformation<SourceRecord>> buildTransformations() {
  List<Transformation<SourceRecord>> transformations = new ArrayList<>();
  List<String> transformNames = simpleConfig.getList(ConnectorConfig.TRANSFORMS_CONFIG);
  for (String name : transformNames) {
    String configPrefix = ConnectorConfig.TRANSFORMS_CONFIG + "." + name + ".";

    // We don't have access to Plugins to properly add loaded classes' configs to the definition,
    // so retrieve it based on the transform prefix.
    Map<String, Object> transformConfig = simpleConfig.originalsWithPrefix(configPrefix);
    String transformClassName = (String) transformConfig.get("type");

    Transformation<SourceRecord> transform;
    try {
      Class<?> transformClass =
          (Class<?>)
              ConfigDef.parseType(
                  configPrefix + "type", transformClassName, ConfigDef.Type.CLASS);
      transform = transformClass.asSubclass(Transformation.class).newInstance();
      transform.configure(transformConfig);
    } catch (RuntimeException | InstantiationException | IllegalAccessException e) {
      // If we couldn't build and configure the Transformation properly we can't verify
      // that we'll be looking for the right target topics, so throw an error.
      throw new ConnectException(
          String.format("Error building transformation %s from config", name), e);
    }
    transformations.add(transform);
  }
  return transformations;
}