Search in sources :

Example 91 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project beam by apache.

the class KafkaIOTest method testKafkaWriteHeaders.

@Test
public void testKafkaWriteHeaders() throws Exception {
    // Set different output topic names
    int numElements = 1;
    SimpleEntry<String, String> header = new SimpleEntry<>("header_key", "header_value");
    try (MockProducerWrapper producerWrapper = new MockProducerWrapper()) {
        ProducerSendCompletionThread completionThread = new ProducerSendCompletionThread(producerWrapper.mockProducer).start();
        String defaultTopic = "test";
        p.apply(mkKafkaReadTransform(numElements, new ValueAsTimestampFn()).withoutMetadata()).apply(ParDo.of(new KV2ProducerRecord(defaultTopic, true, System.currentTimeMillis(), header))).setCoder(ProducerRecordCoder.of(VarIntCoder.of(), VarLongCoder.of())).apply(KafkaIO.<Integer, Long>writeRecords().withBootstrapServers("none").withKeySerializer(IntegerSerializer.class).withValueSerializer(LongSerializer.class).withInputTimestamp().withProducerFactoryFn(new ProducerFactoryFn(producerWrapper.producerKey)));
        p.run();
        completionThread.shutdown();
        // Verify that appropriate header is written with producer record
        List<ProducerRecord<Integer, Long>> sent = producerWrapper.mockProducer.history();
        for (int i = 0; i < numElements; i++) {
            ProducerRecord<Integer, Long> record = sent.get(i);
            Headers headers = record.headers();
            assertNotNull(headers);
            Header[] headersArray = headers.toArray();
            assertEquals(1, headersArray.length);
            assertEquals(header.getKey(), headersArray[0].key());
            assertEquals(header.getValue(), new String(headersArray[0].value(), StandardCharsets.UTF_8));
        }
    }
}
Also used : LongSerializer(org.apache.kafka.common.serialization.LongSerializer) SimpleEntry(java.util.AbstractMap.SimpleEntry) Headers(org.apache.kafka.common.header.Headers) Matchers.containsString(org.hamcrest.Matchers.containsString) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test)

Example 92 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project beam by apache.

the class ProducerRecordCoder method decode.

@Override
public ProducerRecord<K, V> decode(InputStream inStream) throws IOException {
    String topic = stringCoder.decode(inStream);
    Integer partition = intCoder.decode(inStream);
    if (partition == -1) {
        partition = null;
    }
    Long timestamp = longCoder.decode(inStream);
    if (timestamp == Long.MAX_VALUE) {
        timestamp = null;
    }
    Headers headers = (Headers) toHeaders(headerCoder.decode(inStream));
    KV<K, V> kv = kvCoder.decode(inStream);
    if (ConsumerSpEL.hasHeaders()) {
        return new ProducerRecord<>(topic, partition, timestamp, kv.getKey(), kv.getValue(), headers);
    }
    return new ProducerRecord<>(topic, partition, timestamp, kv.getKey(), kv.getValue());
}
Also used : Headers(org.apache.kafka.common.header.Headers) KV(org.apache.beam.sdk.values.KV) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord)

Example 93 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project beam by apache.

the class ProducerRecordCoderTest method testProducerRecordStructuralValueWithoutHeadersApi.

@Test
public void testProducerRecordStructuralValueWithoutHeadersApi() throws IOException {
    RecordHeaders headers = new RecordHeaders();
    headers.add("headerKey", "headerVal".getBytes(UTF_8));
    ProducerRecordCoder producerRecordCoder = ProducerRecordCoder.of(ByteArrayCoder.of(), ByteArrayCoder.of());
    ProducerRecord<byte[], byte[]> producerRecord = new ProducerRecord<>("topic", 1, null, "key".getBytes(UTF_8), "value".getBytes(UTF_8), headers);
    mockStatic(ConsumerSpEL.class);
    when(ConsumerSpEL.hasHeaders()).thenReturn(false);
    ProducerRecord testProducerRecord = (ProducerRecord) producerRecordCoder.structuralValue(producerRecord);
    assertEquals(testProducerRecord.headers(), new RecordHeaders());
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 94 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project beam by apache.

the class ProducerRecordCoderTest method testProducerRecordStructuralValueWithHeadersApi.

@Test
public void testProducerRecordStructuralValueWithHeadersApi() throws IOException {
    RecordHeaders headers = new RecordHeaders();
    headers.add("headerKey", "headerVal".getBytes(UTF_8));
    ProducerRecordCoder producerRecordCoder = ProducerRecordCoder.of(ByteArrayCoder.of(), ByteArrayCoder.of());
    ProducerRecord<byte[], byte[]> producerRecord = new ProducerRecord<>("topic", 1, null, "key".getBytes(UTF_8), "value".getBytes(UTF_8), headers);
    ProducerRecord testProducerRecord = (ProducerRecord) producerRecordCoder.structuralValue(producerRecord);
    assertEquals(testProducerRecord.headers(), headers);
}
Also used : RecordHeaders(org.apache.kafka.common.header.internals.RecordHeaders) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 95 with ProducerRecord

use of org.apache.kafka.clients.producer.ProducerRecord in project beam by apache.

the class NestedPayloadKafkaTable method transformOutput.

// Suppress nullability warnings: ProducerRecord is supposed to accept null arguments.
@SuppressWarnings("argument.type.incompatible")
@VisibleForTesting
ProducerRecord<byte[], byte[]> transformOutput(Row row) {
    row = castRow(row, row.getSchema(), schema);
    String topic = Iterables.getOnlyElement(getTopics());
    byte[] key = null;
    byte[] payload;
    List<Header> headers = ImmutableList.of();
    Long timestampMillis = null;
    if (schema.hasField(Schemas.MESSAGE_KEY_FIELD)) {
        key = row.getBytes(Schemas.MESSAGE_KEY_FIELD);
    }
    if (schema.hasField(Schemas.EVENT_TIMESTAMP_FIELD)) {
        ReadableDateTime time = row.getDateTime(Schemas.EVENT_TIMESTAMP_FIELD);
        if (time != null) {
            timestampMillis = time.getMillis();
        }
    }
    if (schema.hasField(Schemas.HEADERS_FIELD)) {
        Collection<Row> headerRows = checkArgumentNotNull(row.getArray(Schemas.HEADERS_FIELD));
        ImmutableList.Builder<Header> headersBuilder = ImmutableList.builder();
        headerRows.forEach(entry -> {
            String headerKey = checkArgumentNotNull(entry.getString(Schemas.HEADERS_KEY_FIELD));
            Collection<byte[]> values = checkArgumentNotNull(entry.getArray(Schemas.HEADERS_VALUES_FIELD));
            values.forEach(value -> headersBuilder.add(new RecordHeader(headerKey, value)));
        });
        headers = headersBuilder.build();
    }
    if (payloadSerializer == null) {
        payload = row.getBytes(Schemas.PAYLOAD_FIELD);
    } else {
        payload = payloadSerializer.serialize(checkArgumentNotNull(row.getRow(Schemas.PAYLOAD_FIELD)));
    }
    return new ProducerRecord<>(topic, null, timestampMillis, key, payload, headers);
}
Also used : ReadableDateTime(org.joda.time.ReadableDateTime) ImmutableList(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) Header(org.apache.kafka.common.header.Header) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Cast.castRow(org.apache.beam.sdk.schemas.transforms.Cast.castRow) Row(org.apache.beam.sdk.values.Row) RecordHeader(org.apache.kafka.common.header.internals.RecordHeader) VisibleForTesting(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.annotations.VisibleForTesting)

Aggregations

ProducerRecord (org.apache.kafka.clients.producer.ProducerRecord)193 Test (org.junit.Test)90 KafkaProducer (org.apache.kafka.clients.producer.KafkaProducer)57 Properties (java.util.Properties)50 RecordMetadata (org.apache.kafka.clients.producer.RecordMetadata)40 ArrayList (java.util.ArrayList)39 Callback (org.apache.kafka.clients.producer.Callback)30 Future (java.util.concurrent.Future)26 TopicPartition (org.apache.kafka.common.TopicPartition)24 StringSerializer (org.apache.kafka.common.serialization.StringSerializer)21 HashMap (java.util.HashMap)20 Random (java.util.Random)19 IOException (java.io.IOException)16 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)16 KafkaConsumer (org.apache.kafka.clients.consumer.KafkaConsumer)16 KafkaException (org.apache.kafka.common.KafkaException)16 List (java.util.List)13 MockProducer (org.apache.kafka.clients.producer.MockProducer)13 DefaultPartitioner (org.apache.kafka.clients.producer.internals.DefaultPartitioner)12 StreamsException (org.apache.kafka.streams.errors.StreamsException)12