Search in sources :

Example 16 with Headers

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;
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) Header(org.apache.kafka.connect.header.Header) Headers(org.apache.kafka.connect.header.Headers) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders)

Example 17 with Headers

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);
}
Also used : Headers(org.apache.kafka.connect.header.Headers)

Example 18 with Headers

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);
}
Also used : Field(org.apache.kafka.connect.data.Field) Headers(org.apache.kafka.connect.header.Headers) Schema(org.apache.kafka.connect.data.Schema) Struct(org.apache.kafka.connect.data.Struct)

Example 19 with Headers

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());
}
Also used : ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) Headers(org.apache.kafka.connect.header.Headers) Test(org.junit.jupiter.api.Test)

Example 20 with Headers

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());
}
Also used : ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) Headers(org.apache.kafka.connect.header.Headers) Test(org.junit.jupiter.api.Test)

Aggregations

Headers (org.apache.kafka.connect.header.Headers)22 ConnectHeaders (org.apache.kafka.connect.header.ConnectHeaders)17 Test (org.junit.jupiter.api.Test)7 SourceRecord (org.apache.kafka.connect.source.SourceRecord)5 SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)4 Test (org.junit.Test)4 TopicPartition (org.apache.kafka.common.TopicPartition)2 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)2 Header (org.apache.kafka.connect.header.Header)2 SinkRecord (org.apache.kafka.connect.sink.SinkRecord)2 HashMap (java.util.HashMap)1 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)1 Field (org.apache.kafka.connect.data.Field)1 Schema (org.apache.kafka.connect.data.Schema)1 Struct (org.apache.kafka.connect.data.Struct)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1