Java Code Examples for org.apache.kafka.common.header.internals.RecordHeaders#add()

The following examples show how to use org.apache.kafka.common.header.internals.RecordHeaders#add() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecordTest.java    From kafka-backup with Apache License 2.0 6 votes vote down vote up
@Test
public void equalsValueTrueTest() {
    // GIVEN
    Record a = new Record(TOPIC, PARTITION, KEY_BYTES, VALUE_BYTES, OFFSET, TIMESTAMP, TIMESTAMP_TYPE, HEADERS);

    RecordHeaders bHeaders = new RecordHeaders();
    bHeaders.add("", new byte[0]);
    bHeaders.add("null", null);
    bHeaders.add("value0", Arrays.copyOf(HEADER_0_VALUE_BYTES, HEADER_0_VALUE_BYTES.length));
    bHeaders.add("value1", Arrays.copyOf(HEADER_1_VALUE_BYTES, HEADER_1_VALUE_BYTES.length));
    Record b = new Record(TOPIC, PARTITION, KEY_BYTES, VALUE_BYTES, OFFSET, TIMESTAMP, TIMESTAMP_TYPE, bHeaders);

    // THEN
    assertEquals(a, b);
    assertEquals(b, a);
}
 
Example 2
Source File: RecordTest.java    From kafka-backup with Apache License 2.0 6 votes vote down vote up
@Test
public void equalsFalseBecauseHeadersStrictSubsetTest() {
    // GIVEN
    RecordHeaders aHeaders = new RecordHeaders();
    aHeaders.add("header0-key", Arrays.copyOf(HEADER_0_VALUE_BYTES, HEADER_0_VALUE_BYTES.length));
    aHeaders.add("header1-key", Arrays.copyOf(HEADER_1_VALUE_BYTES, HEADER_1_VALUE_BYTES.length));
    Record a = new Record(TOPIC, PARTITION, KEY_BYTES, VALUE_BYTES, OFFSET, TIMESTAMP, TIMESTAMP_TYPE, aHeaders);

    RecordHeaders bHeaders = new RecordHeaders();
    bHeaders.add("header0-key", Arrays.copyOf(HEADER_0_VALUE_BYTES, HEADER_0_VALUE_BYTES.length));
    Record b = new Record(TOPIC, PARTITION, KEY_BYTES, VALUE_BYTES, OFFSET, TIMESTAMP, TIMESTAMP_TYPE, bHeaders);


    RecordHeaders cHeaders = new RecordHeaders();
    cHeaders.add("header1-key", Arrays.copyOf(HEADER_0_VALUE_BYTES, HEADER_0_VALUE_BYTES.length));
    cHeaders.add("header1-key", Arrays.copyOf(HEADER_1_VALUE_BYTES, HEADER_1_VALUE_BYTES.length));
    Record c = new Record(TOPIC, PARTITION, KEY_BYTES, VALUE_BYTES, OFFSET, TIMESTAMP, TIMESTAMP_TYPE, cHeaders);

    // THEN
    assertNotEquals(a, b);
    assertNotEquals(b, a);
    assertNotEquals(a, c);
    assertNotEquals(b, c);
}
 
Example 3
Source File: KafkaSourceTaskTest.java    From MirrorTool-for-Kafka-Connect with Apache License 2.0 6 votes vote down vote up
private ConsumerRecords<byte[], byte[]> createTestRecordsWithHeaders() {
  RecordHeader header = new RecordHeader("testHeader", new byte[0]);
  RecordHeaders headers = new RecordHeaders();
  headers.add(header);
  TimestampType timestampType = TimestampType.NO_TIMESTAMP_TYPE;

  byte testByte = 0;
  byte[] testKey = { testByte };
  byte[] testValue = { testByte };

  ConnectHeaders destinationHeaders = new ConnectHeaders();
  destinationHeaders.add(header.key(), header.value(), Schema.OPTIONAL_BYTES_SCHEMA);
  ConsumerRecord<byte[], byte[]> testConsumerRecord = new ConsumerRecord<byte[], byte[]>(FIRST_TOPIC, FIRST_PARTITION,
      FIRST_OFFSET, System.currentTimeMillis(), timestampType, 0L, 0, 0, testKey, testValue, headers);

  TopicPartition topicPartition = new TopicPartition(FIRST_TOPIC, FIRST_PARTITION);
  List<ConsumerRecord<byte[], byte[]>> consumerRecords = new ArrayList<>();
  consumerRecords.add(testConsumerRecord);

  Map<TopicPartition, List<ConsumerRecord<byte[], byte[]>>> consumerRecordMap = new HashMap<>(1);
  consumerRecordMap.put(topicPartition, consumerRecords);
  ConsumerRecords<byte[], byte[]> testRecords = new ConsumerRecords<>(consumerRecordMap);
  return testRecords;
}
 
Example 4
Source File: Record.java    From kafka-backup with Apache License 2.0 5 votes vote down vote up
public static Record fromSinkRecord(SinkRecord sinkRecord) {
    byte[] key = connectDataToBytes(sinkRecord.keySchema(), sinkRecord.key());
    byte[] value = connectDataToBytes(sinkRecord.valueSchema(), sinkRecord.value());
    RecordHeaders recordHeaders = new RecordHeaders();
    for (org.apache.kafka.connect.header.Header connectHeader : sinkRecord.headers()) {
        byte[] headerValue = connectDataToBytes(connectHeader.schema(), connectHeader.value());
        recordHeaders.add(connectHeader.key(), headerValue);
    }
    return new Record(sinkRecord.topic(), sinkRecord.kafkaPartition(), key, value, sinkRecord.kafkaOffset(), sinkRecord.timestamp(), sinkRecord.timestampType(), recordHeaders);
}
 
Example 5
Source File: BaseRecordWeigherTest.java    From kafka-workers with Apache License 2.0 5 votes vote down vote up
private WorkerRecord<byte[], byte[]> emptyWorkerRecordWithHeaders(String[] headers) {
    RecordHeaders recordHeaders = new RecordHeaders();
    for (String headerStr: headers) {
        String[] split = headerStr.split(":");
        recordHeaders.add(new RecordHeader(split[0], split[1].getBytes(ISO_8859_1)));
    }
    ConsumerRecord<byte[], byte[]> consumerRecord = new ConsumerRecord<>(EMPTY_TOPIC, SOME_PARTITION, SOME_OFFSET,
            ConsumerRecord.NO_TIMESTAMP, TimestampType.NO_TIMESTAMP_TYPE, (long) ConsumerRecord.NULL_CHECKSUM,
            0, 0,
            new byte[0], new byte[0],
            recordHeaders);

    return new WorkerRecord<>(consumerRecord, SOME_SUBPARTITION);
}
 
Example 6
Source File: KafkaUtils.java    From sdk-java with Apache License 2.0 5 votes vote down vote up
static RecordHeaders kafkaHeaders(RecordHeader... headers) {
    RecordHeaders hs = new RecordHeaders();
    for (RecordHeader h : headers) {
        hs.add(h);
    }
    return hs;
}
 
Example 7
Source File: ProducerRecordCoderTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: ProducerRecordCoderTest.java    From beam with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example 9
Source File: KafkaRecordCoderTest.java    From DataflowTemplates with Apache License 2.0 4 votes vote down vote up
@Test
public void testKafkaRecordSerializableWithHeaders() throws IOException {
  RecordHeaders headers = new RecordHeaders();
  headers.add("headerKey", "headerVal".getBytes(StandardCharsets.UTF_8));
  verifySerialization(headers);
}
 
Example 10
Source File: KafkaRecordCoderTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void testKafkaRecordSerializableWithHeaders() throws IOException {
  RecordHeaders headers = new RecordHeaders();
  headers.add("headerKey", "headerVal".getBytes(StandardCharsets.UTF_8));
  verifySerialization(headers);
}
 
Example 11
Source File: ProducerRecordCoderTest.java    From beam with Apache License 2.0 4 votes vote down vote up
@Test
public void testProducerRecordSerializableWithHeaders() throws IOException {
  RecordHeaders headers = new RecordHeaders();
  headers.add("headerKey", "headerVal".getBytes(UTF_8));
  verifySerialization(headers, 0, System.currentTimeMillis());
}