Search in sources :

Example 21 with SinkRecord

use of org.apache.kafka.connect.sink.SinkRecord in project kafka by apache.

the class HoistFieldTest method schemaless.

@Test
public void schemaless() {
    final HoistField<SinkRecord> xform = new HoistField.Key<>();
    xform.configure(Collections.singletonMap("field", "magic"));
    final SinkRecord record = new SinkRecord("test", 0, null, 42, null, null, 0);
    final SinkRecord transformedRecord = xform.apply(record);
    assertNull(transformedRecord.keySchema());
    assertEquals(Collections.singletonMap("magic", 42), transformedRecord.key());
}
Also used : SinkRecord(org.apache.kafka.connect.sink.SinkRecord) Test(org.junit.Test)

Example 22 with SinkRecord

use of org.apache.kafka.connect.sink.SinkRecord in project kafka by apache.

the class ReplaceFieldTest method schemaless.

@Test
public void schemaless() {
    final ReplaceField<SinkRecord> xform = new ReplaceField.Value<>();
    final Map<String, String> props = new HashMap<>();
    props.put("blacklist", "dont");
    props.put("renames", "abc:xyz,foo:bar");
    xform.configure(props);
    final Map<String, Object> value = new HashMap<>();
    value.put("dont", "whatever");
    value.put("abc", 42);
    value.put("foo", true);
    value.put("etc", "etc");
    final SinkRecord record = new SinkRecord("test", 0, null, null, null, value, 0);
    final SinkRecord transformedRecord = xform.apply(record);
    final Map updatedValue = (Map) transformedRecord.value();
    assertEquals(3, updatedValue.size());
    assertEquals(42, updatedValue.get("xyz"));
    assertEquals(true, updatedValue.get("bar"));
    assertEquals("etc", updatedValue.get("etc"));
}
Also used : HashMap(java.util.HashMap) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 23 with SinkRecord

use of org.apache.kafka.connect.sink.SinkRecord in project kafka by apache.

the class ReplaceFieldTest method withSchema.

@Test
public void withSchema() {
    final ReplaceField<SinkRecord> xform = new ReplaceField.Value<>();
    final Map<String, String> props = new HashMap<>();
    props.put("whitelist", "abc,foo");
    props.put("renames", "abc:xyz,foo:bar");
    xform.configure(props);
    final Schema schema = SchemaBuilder.struct().field("dont", Schema.STRING_SCHEMA).field("abc", Schema.INT32_SCHEMA).field("foo", Schema.BOOLEAN_SCHEMA).field("etc", Schema.STRING_SCHEMA).build();
    final Struct value = new Struct(schema);
    value.put("dont", "whatever");
    value.put("abc", 42);
    value.put("foo", true);
    value.put("etc", "etc");
    final SinkRecord record = new SinkRecord("test", 0, null, null, schema, value, 0);
    final SinkRecord transformedRecord = xform.apply(record);
    final Struct updatedValue = (Struct) transformedRecord.value();
    assertEquals(2, updatedValue.schema().fields().size());
    assertEquals(new Integer(42), updatedValue.getInt32("xyz"));
    assertEquals(true, updatedValue.getBoolean("bar"));
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.kafka.connect.data.Schema) SinkRecord(org.apache.kafka.connect.sink.SinkRecord) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.Test)

Example 24 with SinkRecord

use of org.apache.kafka.connect.sink.SinkRecord in project kafka by apache.

the class SetSchemaMetadataTest method schemaNameUpdate.

@Test
public void schemaNameUpdate() {
    final SetSchemaMetadata<SinkRecord> xform = new SetSchemaMetadata.Value<>();
    xform.configure(Collections.singletonMap("schema.name", "foo"));
    final SinkRecord record = new SinkRecord("", 0, null, null, SchemaBuilder.struct().build(), null, 0);
    final SinkRecord updatedRecord = xform.apply(record);
    assertEquals("foo", updatedRecord.valueSchema().name());
}
Also used : SinkRecord(org.apache.kafka.connect.sink.SinkRecord) Test(org.junit.Test)

Example 25 with SinkRecord

use of org.apache.kafka.connect.sink.SinkRecord in project kafka by apache.

the class SetSchemaMetadataTest method schemaVersionUpdate.

@Test
public void schemaVersionUpdate() {
    final SetSchemaMetadata<SinkRecord> xform = new SetSchemaMetadata.Value<>();
    xform.configure(Collections.singletonMap("schema.version", 42));
    final SinkRecord record = new SinkRecord("", 0, null, null, SchemaBuilder.struct().build(), null, 0);
    final SinkRecord updatedRecord = xform.apply(record);
    assertEquals(new Integer(42), updatedRecord.valueSchema().version());
}
Also used : SinkRecord(org.apache.kafka.connect.sink.SinkRecord) Test(org.junit.Test)

Aggregations

SinkRecord (org.apache.kafka.connect.sink.SinkRecord)27 Test (org.junit.Test)19 HashMap (java.util.HashMap)11 TopicPartition (org.apache.kafka.common.TopicPartition)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)6 Collection (java.util.Collection)4 Schema (org.apache.kafka.connect.data.Schema)3 Struct (org.apache.kafka.connect.data.Struct)3 ConnectException (org.apache.kafka.connect.errors.ConnectException)3 Map (java.util.Map)2 ConsumerRecords (org.apache.kafka.clients.consumer.ConsumerRecords)2 OffsetCommitCallback (org.apache.kafka.clients.consumer.OffsetCommitCallback)2 SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)1 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)1 WakeupException (org.apache.kafka.common.errors.WakeupException)1 TimestampType (org.apache.kafka.common.record.TimestampType)1 RetriableException (org.apache.kafka.connect.errors.RetriableException)1