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());
}
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"));
}
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);
}
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());
}
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());
}
Aggregations