Search in sources :

Example 1 with Headers

use of org.apache.kafka.connect.header.Headers in project apache-kafka-on-k8s by banzaicloud.

the class WorkerSinkTask method convertHeadersFor.

private Headers convertHeadersFor(ConsumerRecord<byte[], byte[]> record) {
    Headers result = new ConnectHeaders();
    org.apache.kafka.common.header.Headers recordHeaders = record.headers();
    if (recordHeaders != null) {
        String topic = record.topic();
        for (org.apache.kafka.common.header.Header recordHeader : recordHeaders) {
            SchemaAndValue schemaAndValue = headerConverter.toConnectHeader(topic, recordHeader.key(), recordHeader.value());
            result.add(recordHeader.key(), schemaAndValue);
        }
    }
    return result;
}
Also used : ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) Headers(org.apache.kafka.connect.header.Headers) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue)

Example 2 with Headers

use of org.apache.kafka.connect.header.Headers in project apache-kafka-on-k8s by banzaicloud.

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 3 with Headers

use of org.apache.kafka.connect.header.Headers in project apache-kafka-on-k8s by banzaicloud.

the class SourceRecordTest method shouldDuplicateRecordUsingNewHeaders.

@Test
public void shouldDuplicateRecordUsingNewHeaders() {
    Headers newHeaders = new ConnectHeaders().addString("h3", "hv3");
    SourceRecord duplicate = record.newRecord(TOPIC_NAME, PARTITION_NUMBER, Schema.STRING_SCHEMA, "key", Schema.BOOLEAN_SCHEMA, false, KAFKA_TIMESTAMP, newHeaders);
    assertEquals(SOURCE_PARTITION, duplicate.sourcePartition());
    assertEquals(SOURCE_OFFSET, duplicate.sourceOffset());
    assertEquals(TOPIC_NAME, duplicate.topic());
    assertEquals(PARTITION_NUMBER, duplicate.kafkaPartition());
    assertEquals(Schema.STRING_SCHEMA, duplicate.keySchema());
    assertEquals("key", duplicate.key());
    assertEquals(Schema.BOOLEAN_SCHEMA, duplicate.valueSchema());
    assertEquals(false, duplicate.value());
    assertEquals(KAFKA_TIMESTAMP, duplicate.timestamp());
    assertNotNull(duplicate.headers());
    assertEquals(newHeaders, duplicate.headers());
    assertSame(newHeaders, duplicate.headers());
    assertNotSame(record.headers(), duplicate.headers());
    assertNotEquals(record.headers(), duplicate.headers());
}
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.Test)

Example 4 with Headers

use of org.apache.kafka.connect.header.Headers in project kafka by apache.

the class MirrorSourceTask method convertRecord.

// visible for testing
SourceRecord convertRecord(ConsumerRecord<byte[], byte[]> record) {
    String targetTopic = formatRemoteTopic(record.topic());
    Headers headers = convertHeaders(record);
    return new SourceRecord(MirrorUtils.wrapPartition(new TopicPartition(record.topic(), record.partition()), sourceClusterAlias), MirrorUtils.wrapOffset(record.offset()), targetTopic, record.partition(), Schema.OPTIONAL_BYTES_SCHEMA, record.key(), Schema.BYTES_SCHEMA, record.value(), record.timestamp(), headers);
}
Also used : ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) Headers(org.apache.kafka.connect.header.Headers) TopicPartition(org.apache.kafka.common.TopicPartition) SourceRecord(org.apache.kafka.connect.source.SourceRecord)

Example 5 with Headers

use of org.apache.kafka.connect.header.Headers in project kafka by apache.

the class SourceRecordTest method shouldDuplicateRecordUsingNewHeaders.

@Test
public void shouldDuplicateRecordUsingNewHeaders() {
    Headers newHeaders = new ConnectHeaders().addString("h3", "hv3");
    SourceRecord duplicate = record.newRecord(TOPIC_NAME, PARTITION_NUMBER, Schema.STRING_SCHEMA, "key", Schema.BOOLEAN_SCHEMA, false, KAFKA_TIMESTAMP, newHeaders);
    assertEquals(SOURCE_PARTITION, duplicate.sourcePartition());
    assertEquals(SOURCE_OFFSET, duplicate.sourceOffset());
    assertEquals(TOPIC_NAME, duplicate.topic());
    assertEquals(PARTITION_NUMBER, duplicate.kafkaPartition());
    assertEquals(Schema.STRING_SCHEMA, duplicate.keySchema());
    assertEquals("key", duplicate.key());
    assertEquals(Schema.BOOLEAN_SCHEMA, duplicate.valueSchema());
    assertEquals(false, duplicate.value());
    assertEquals(KAFKA_TIMESTAMP, duplicate.timestamp());
    assertNotNull(duplicate.headers());
    assertEquals(newHeaders, duplicate.headers());
    assertSame(newHeaders, duplicate.headers());
    assertNotSame(record.headers(), duplicate.headers());
    assertNotEquals(record.headers(), duplicate.headers());
}
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