Search in sources :

Example 91 with SourceRecord

use of org.apache.kafka.connect.source.SourceRecord in project kafka by apache.

the class InsertFieldTest method copySchemaAndInsertConfiguredFields.

@Test
public void copySchemaAndInsertConfiguredFields() {
    final Map<String, Object> props = new HashMap<>();
    props.put("topic.field", "topic_field!");
    props.put("partition.field", "partition_field");
    props.put("timestamp.field", "timestamp_field?");
    props.put("static.field", "instance_id");
    props.put("static.value", "my-instance-id");
    xformValue.configure(props);
    final Schema simpleStructSchema = SchemaBuilder.struct().name("name").version(1).doc("doc").field("magic", Schema.OPTIONAL_INT64_SCHEMA).build();
    final Struct simpleStruct = new Struct(simpleStructSchema).put("magic", 42L);
    final SourceRecord record = new SourceRecord(null, null, "test", 0, null, null, simpleStructSchema, simpleStruct, 789L);
    final SourceRecord transformedRecord = xformValue.apply(record);
    assertEquals(simpleStructSchema.name(), transformedRecord.valueSchema().name());
    assertEquals(simpleStructSchema.version(), transformedRecord.valueSchema().version());
    assertEquals(simpleStructSchema.doc(), transformedRecord.valueSchema().doc());
    assertEquals(Schema.OPTIONAL_INT64_SCHEMA, transformedRecord.valueSchema().field("magic").schema());
    assertEquals(42L, ((Struct) transformedRecord.value()).getInt64("magic").longValue());
    assertEquals(Schema.STRING_SCHEMA, transformedRecord.valueSchema().field("topic_field").schema());
    assertEquals("test", ((Struct) transformedRecord.value()).getString("topic_field"));
    assertEquals(Schema.OPTIONAL_INT32_SCHEMA, transformedRecord.valueSchema().field("partition_field").schema());
    assertEquals(0, ((Struct) transformedRecord.value()).getInt32("partition_field").intValue());
    assertEquals(Timestamp.builder().optional().build(), transformedRecord.valueSchema().field("timestamp_field").schema());
    assertEquals(789L, ((Date) ((Struct) transformedRecord.value()).get("timestamp_field")).getTime());
    assertEquals(Schema.OPTIONAL_STRING_SCHEMA, transformedRecord.valueSchema().field("instance_id").schema());
    assertEquals("my-instance-id", ((Struct) transformedRecord.value()).getString("instance_id"));
    // Exercise caching
    final SourceRecord transformedRecord2 = xformValue.apply(new SourceRecord(null, null, "test", 1, simpleStructSchema, new Struct(simpleStructSchema)));
    assertSame(transformedRecord.valueSchema(), transformedRecord2.valueSchema());
}
Also used : HashMap(java.util.HashMap) Schema(org.apache.kafka.connect.data.Schema) SourceRecord(org.apache.kafka.connect.source.SourceRecord) Struct(org.apache.kafka.connect.data.Struct) Test(org.junit.jupiter.api.Test)

Example 92 with SourceRecord

use of org.apache.kafka.connect.source.SourceRecord in project kafka by apache.

the class InsertFieldTest method schemalessInsertConfiguredFields.

@Test
public void schemalessInsertConfiguredFields() {
    final Map<String, Object> props = new HashMap<>();
    props.put("topic.field", "topic_field!");
    props.put("partition.field", "partition_field");
    props.put("timestamp.field", "timestamp_field?");
    props.put("static.field", "instance_id");
    props.put("static.value", "my-instance-id");
    xformValue.configure(props);
    final SourceRecord record = new SourceRecord(null, null, "test", 0, null, null, null, Collections.singletonMap("magic", 42L), 123L);
    final SourceRecord transformedRecord = xformValue.apply(record);
    assertEquals(42L, ((Map<?, ?>) transformedRecord.value()).get("magic"));
    assertEquals("test", ((Map<?, ?>) transformedRecord.value()).get("topic_field"));
    assertEquals(0, ((Map<?, ?>) transformedRecord.value()).get("partition_field"));
    assertEquals(123L, ((Map<?, ?>) transformedRecord.value()).get("timestamp_field"));
    assertEquals("my-instance-id", ((Map<?, ?>) transformedRecord.value()).get("instance_id"));
}
Also used : HashMap(java.util.HashMap) SourceRecord(org.apache.kafka.connect.source.SourceRecord) Test(org.junit.jupiter.api.Test)

Example 93 with SourceRecord

use of org.apache.kafka.connect.source.SourceRecord in project kafka by apache.

the class InsertFieldTest method insertIntoNullKeyLeavesRecordUnchanged.

@Test
public void insertIntoNullKeyLeavesRecordUnchanged() {
    final Map<String, Object> props = new HashMap<>();
    props.put("topic.field", "topic_field!");
    props.put("partition.field", "partition_field");
    props.put("timestamp.field", "timestamp_field?");
    props.put("static.field", "instance_id");
    props.put("static.value", "my-instance-id");
    xformKey.configure(props);
    final SourceRecord record = new SourceRecord(null, null, "test", 0, null, null, null, Collections.singletonMap("magic", 42L));
    final SourceRecord transformedRecord = xformKey.apply(record);
    assertSame(record, transformedRecord);
}
Also used : HashMap(java.util.HashMap) SourceRecord(org.apache.kafka.connect.source.SourceRecord) Test(org.junit.jupiter.api.Test)

Example 94 with SourceRecord

use of org.apache.kafka.connect.source.SourceRecord in project kafka by apache.

the class InsertFieldTest method insertConfiguredFieldsIntoTombstoneEventWithoutSchemaLeavesValueUnchanged.

@Test
public void insertConfiguredFieldsIntoTombstoneEventWithoutSchemaLeavesValueUnchanged() {
    final Map<String, Object> props = new HashMap<>();
    props.put("topic.field", "topic_field!");
    props.put("partition.field", "partition_field");
    props.put("timestamp.field", "timestamp_field?");
    props.put("static.field", "instance_id");
    props.put("static.value", "my-instance-id");
    xformValue.configure(props);
    final SourceRecord record = new SourceRecord(null, null, "test", 0, null, null);
    final SourceRecord transformedRecord = xformValue.apply(record);
    assertNull(transformedRecord.value());
    assertNull(transformedRecord.valueSchema());
}
Also used : HashMap(java.util.HashMap) SourceRecord(org.apache.kafka.connect.source.SourceRecord) Test(org.junit.jupiter.api.Test)

Example 95 with SourceRecord

use of org.apache.kafka.connect.source.SourceRecord in project kafka by apache.

the class CastTest method castWholeRecordValueWithSchemaFloat64.

@Test
public void castWholeRecordValueWithSchemaFloat64() {
    xformValue.configure(Collections.singletonMap(Cast.SPEC_CONFIG, "float64"));
    SourceRecord transformed = xformValue.apply(new SourceRecord(null, null, "topic", 0, Schema.INT32_SCHEMA, 42));
    assertEquals(Schema.Type.FLOAT64, transformed.valueSchema().type());
    assertEquals(42., transformed.value());
}
Also used : SourceRecord(org.apache.kafka.connect.source.SourceRecord) Test(org.junit.jupiter.api.Test)

Aggregations

SourceRecord (org.apache.kafka.connect.source.SourceRecord)308 Test (org.junit.Test)148 Test (org.junit.jupiter.api.Test)98 Struct (org.apache.kafka.connect.data.Struct)68 HashMap (java.util.HashMap)60 Schema (org.apache.kafka.connect.data.Schema)45 ThreadedTest (org.apache.kafka.connect.util.ThreadedTest)27 ParameterizedTest (org.apache.kafka.connect.util.ParameterizedTest)23 ArrayList (java.util.ArrayList)22 RetryWithToleranceOperatorTest (org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperatorTest)21 Map (java.util.Map)15 SchemaBuilder (org.apache.kafka.connect.data.SchemaBuilder)13 ConnectException (org.apache.kafka.connect.errors.ConnectException)13 Document (org.bson.Document)13 FixFor (io.debezium.doc.FixFor)12 List (java.util.List)12 RecordsForCollection (io.debezium.connector.mongodb.RecordMakers.RecordsForCollection)11 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)11 ConnectHeaders (org.apache.kafka.connect.header.ConnectHeaders)11 BsonTimestamp (org.bson.BsonTimestamp)11