use of org.apache.kafka.connect.header.Headers in project kafka by apache.
the class WorkerSourceTask method convertHeaderFor.
private RecordHeaders convertHeaderFor(SourceRecord record) {
Headers headers = record.headers();
RecordHeaders result = new RecordHeaders();
if (headers != null) {
String topic = record.topic();
for (Header header : headers) {
String key = header.key();
byte[] rawHeader = headerConverter.fromConnectHeader(topic, key, header.schema(), header.value());
result.add(key, rawHeader);
}
}
return result;
}
use of org.apache.kafka.connect.header.Headers in project kafka by apache.
the class InsertHeader method apply.
@Override
public R apply(R record) {
Headers updatedHeaders = record.headers().duplicate();
updatedHeaders.add(header, literalValue);
return record.newRecord(record.topic(), record.kafkaPartition(), record.keySchema(), record.key(), record.valueSchema(), record.value(), record.timestamp(), updatedHeaders);
}
use of org.apache.kafka.connect.header.Headers in project kafka by apache.
the class HeaderFrom method applyWithSchema.
private R applyWithSchema(R record, Object operatingValue, Schema operatingSchema) {
Headers updatedHeaders = record.headers().duplicate();
Struct value = Requirements.requireStruct(operatingValue, "header " + operation);
final Schema updatedSchema;
final Struct updatedValue;
if (operation == Operation.MOVE) {
updatedSchema = moveSchema(operatingSchema);
updatedValue = new Struct(updatedSchema);
for (Field field : updatedSchema.fields()) {
updatedValue.put(field, value.get(field.name()));
}
} else {
updatedSchema = operatingSchema;
updatedValue = value;
}
for (int i = 0; i < fields.size(); i++) {
String fieldName = fields.get(i);
String headerName = headers.get(i);
Object fieldValue = value.schema().field(fieldName) != null ? value.get(fieldName) : null;
Schema fieldSchema = operatingSchema.field(fieldName).schema();
updatedHeaders.add(headerName, fieldValue, fieldSchema);
}
return newRecord(record, updatedSchema, updatedValue, updatedHeaders);
}
use of org.apache.kafka.connect.header.Headers in project kafka by apache.
the class SourceRecordTest method shouldCreateSinkRecordWithHeaders.
@Test
public void shouldCreateSinkRecordWithHeaders() {
Headers headers = new ConnectHeaders().addString("h1", "hv1").addBoolean("h2", true);
record = new SourceRecord(SOURCE_PARTITION, SOURCE_OFFSET, TOPIC_NAME, PARTITION_NUMBER, Schema.STRING_SCHEMA, "key", Schema.BOOLEAN_SCHEMA, false, KAFKA_TIMESTAMP, headers);
assertNotNull(record.headers());
assertSame(headers, record.headers());
assertFalse(record.headers().isEmpty());
}
use of org.apache.kafka.connect.header.Headers in project kafka by apache.
the class SinkRecordTest method shouldCreateSinkRecordWithHeaders.
@Test
public void shouldCreateSinkRecordWithHeaders() {
Headers headers = new ConnectHeaders().addString("h1", "hv1").addBoolean("h2", true);
record = new SinkRecord(TOPIC_NAME, PARTITION_NUMBER, Schema.STRING_SCHEMA, "key", Schema.BOOLEAN_SCHEMA, false, KAFKA_OFFSET, KAFKA_TIMESTAMP, TS_TYPE, headers);
assertNotNull(record.headers());
assertSame(headers, record.headers());
assertFalse(record.headers().isEmpty());
}
Aggregations