Search in sources :

Example 1 with ConnectHeaders

use of org.apache.kafka.connect.header.ConnectHeaders 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 ConnectHeaders

use of org.apache.kafka.connect.header.ConnectHeaders 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 3 with ConnectHeaders

use of org.apache.kafka.connect.header.ConnectHeaders 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)

Example 4 with ConnectHeaders

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

the class HeaderFromTest method withSchema.

@ParameterizedTest
@MethodSource("data")
public void withSchema(String description, boolean keyTransform, RecordBuilder originalBuilder, List<String> transformFields, List<String> headers1, HeaderFrom.Operation operation, RecordBuilder expectedBuilder) {
    HeaderFrom<SourceRecord> xform = keyTransform ? new HeaderFrom.Key<>() : new HeaderFrom.Value<>();
    xform.configure(config(headers1, transformFields, operation));
    ConnectHeaders headers = new ConnectHeaders();
    headers.addString("existing", "existing-value");
    Headers expect = headers.duplicate();
    for (int i = 0; i < headers1.size(); i++) {
        expect.add(headers1.get(i), originalBuilder.fieldValues.get(i), originalBuilder.fieldSchemas.get(i));
    }
    SourceRecord originalRecord = originalBuilder.withSchema(keyTransform);
    SourceRecord expectedRecord = expectedBuilder.withSchema(keyTransform);
    SourceRecord xformed = xform.apply(originalRecord);
    assertSameRecord(expectedRecord, xformed);
}
Also used : ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) Headers(org.apache.kafka.connect.header.Headers) SourceRecord(org.apache.kafka.connect.source.SourceRecord) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with ConnectHeaders

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

the class WorkerSourceTaskTest method testHeaders.

@Test
public void testHeaders() throws Exception {
    Headers headers = new RecordHeaders();
    headers.add("header_key", "header_value".getBytes());
    org.apache.kafka.connect.header.Headers connectHeaders = new ConnectHeaders();
    connectHeaders.add("header_key", new SchemaAndValue(Schema.STRING_SCHEMA, "header_value"));
    createWorkerTask();
    List<SourceRecord> records = new ArrayList<>();
    records.add(new SourceRecord(PARTITION, OFFSET, TOPIC, null, KEY_SCHEMA, KEY, RECORD_SCHEMA, RECORD, null, connectHeaders));
    expectTopicCreation(TOPIC);
    Capture<ProducerRecord<byte[], byte[]>> sent = expectSendRecord(TOPIC, true, true, true, true, headers);
    PowerMock.replayAll();
    Whitebox.setInternalState(workerTask, "toSend", records);
    Whitebox.invokeMethod(workerTask, "sendRecords");
    assertEquals(SERIALIZED_KEY, sent.getValue().key());
    assertEquals(SERIALIZED_RECORD, sent.getValue().value());
    assertEquals(headers, sent.getValue().headers());
    PowerMock.verifyAll();
}
Also used : Headers(org.apache.kafka.common.header.Headers) ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ArrayList(java.util.ArrayList) SourceRecord(org.apache.kafka.connect.source.SourceRecord) SchemaAndValue(org.apache.kafka.connect.data.SchemaAndValue) ConnectHeaders(org.apache.kafka.connect.header.ConnectHeaders) RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ThreadedTest(org.apache.kafka.connect.util.ThreadedTest) RetryWithToleranceOperatorTest(org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperatorTest) ParameterizedTest(org.apache.kafka.connect.util.ParameterizedTest) Test(org.junit.Test)

Aggregations

ConnectHeaders (org.apache.kafka.connect.header.ConnectHeaders)20 Headers (org.apache.kafka.connect.header.Headers)14 SourceRecord (org.apache.kafka.connect.source.SourceRecord)10 Test (org.junit.jupiter.api.Test)10 Test (org.junit.Test)6 SchemaAndValue (org.apache.kafka.connect.data.SchemaAndValue)3 ArrayList (java.util.ArrayList)2 ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)2 RetryWithToleranceOperatorTest (org.apache.kafka.connect.runtime.errors.RetryWithToleranceOperatorTest)2 ParameterizedTest (org.apache.kafka.connect.util.ParameterizedTest)2 ThreadedTest (org.apache.kafka.connect.util.ThreadedTest)2 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)2 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 Headers (org.apache.kafka.common.header.Headers)1 RecordHeaders (org.apache.kafka.common.header.internals.RecordHeaders)1 StringConverter (org.apache.kafka.connect.storage.StringConverter)1