Java Code Examples for org.apache.kafka.common.header.Header#value()

The following examples show how to use org.apache.kafka.common.header.Header#value() . 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: Record.java    From kafka-backup with Apache License 2.0 6 votes vote down vote up
private boolean headerEqualityByValue(Header a, Header b) {
    // This is an alternative implementation of ConnectHeader::equals that use proper Value equality by value
    // (even if they are byte arrays)
    if (a == b) {
        return true;
    }
    if (!Objects.equals(a.key(), b.key())) {
        return false;
    }
    try {
        // This particular case is not handled by ConnectHeader::equals
        byte[] aBytes = a.value();
        byte[] bBytes = b.value();
        return Arrays.equals(aBytes, bBytes);
    } catch (ClassCastException e) {
        return a.value() == b.value();
    }
}
 
Example 2
Source File: ConsumerLease.java    From nifi with Apache License 2.0 6 votes vote down vote up
private Map<String, String> getAttributes(final ConsumerRecord<?, ?> consumerRecord) {
    final Map<String, String> attributes = new HashMap<>();
    if (headerNamePattern == null) {
        return attributes;
    }

    for (final Header header : consumerRecord.headers()) {
        final String attributeName = header.key();
        final byte[] attributeValue = header.value();
        if (headerNamePattern.matcher(attributeName).matches() && attributeValue != null) {
            attributes.put(attributeName, new String(attributeValue, headerCharacterSet));
        }
    }

    return attributes;
}
 
Example 3
Source File: ConsumerLease.java    From nifi with Apache License 2.0 6 votes vote down vote up
private Map<String, String> getAttributes(final ConsumerRecord<?, ?> consumerRecord) {
    final Map<String, String> attributes = new HashMap<>();
    if (headerNamePattern == null) {
        return attributes;
    }

    for (final Header header : consumerRecord.headers()) {
        final String attributeName = header.key();
        final byte[] attributeValue = header.value();
        if (headerNamePattern.matcher(attributeName).matches() && attributeValue != null) {
            attributes.put(attributeName, new String(attributeValue, headerCharacterSet));
        }
    }

    return attributes;
}
 
Example 4
Source File: ConsumerLease.java    From nifi with Apache License 2.0 6 votes vote down vote up
private Map<String, String> getAttributes(final ConsumerRecord<?, ?> consumerRecord) {
    final Map<String, String> attributes = new HashMap<>();
    if (headerNamePattern == null) {
        return attributes;
    }

    for (final Header header : consumerRecord.headers()) {
        final String attributeName = header.key();
        final byte[] attributeValue = header.value();
        if (headerNamePattern.matcher(attributeName).matches() && attributeValue != null) {
            attributes.put(attributeName, new String(attributeValue, headerCharacterSet));
        }
    }

    return attributes;
}
 
Example 5
Source File: KafkaHeaders.java    From micronaut-kafka with Apache License 2.0 5 votes vote down vote up
@Override
public String get(CharSequence name) {
    Header header = headers.lastHeader(name.toString());
    if (header != null) {
        return new String(header.value());
    }
    return null;
}
 
Example 6
Source File: KafkaRecordHeaderAccessor.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Nullable
@Override
public byte[] getFirstHeader(String headerName, ConsumerRecord record) {
    Header traceParentHeader = record.headers().lastHeader(headerName);
    if (traceParentHeader != null) {
        return traceParentHeader.value();
    }
    return null;
}
 
Example 7
Source File: KafkaHeaders.java    From sdk-java with Apache License 2.0 5 votes vote down vote up
public static String getParsedKafkaHeader(Headers headers, String key) {
    Header h = headers.lastHeader(key);
    if (h == null) {
        return null;
    }
    return new String(h.value(), StandardCharsets.UTF_8);
}
 
Example 8
Source File: HeadersMapExtractAdapter.java    From java-kafka-client with Apache License 2.0 5 votes vote down vote up
public HeadersMapExtractAdapter(Headers headers) {
  for (Header header : headers) {
    byte[] headerValue = header.value();
    map.put(header.key(),
        headerValue == null ? null : new String(headerValue, StandardCharsets.UTF_8));
  }
}
 
Example 9
Source File: EventOwnerHeader.java    From nakadi with MIT License 5 votes vote down vote up
public static EventOwnerHeader deserialize(final ConsumerRecord<byte[], byte[]> record) {
    final Header nameHeader = record.headers().lastHeader(EventOwnerHeader.AUTH_PARAM_NAME);
    if (null == nameHeader) {
        return null;
    }
    final Header valueHeader = record.headers().lastHeader(EventOwnerHeader.AUTH_PARAM_VALUE);
    if (valueHeader == null) {
        return null;
    }

    return new EventOwnerHeader(
            new String(nameHeader.value(), Charsets.UTF_8),
            new String(valueHeader.value(), Charsets.UTF_8));
}
 
Example 10
Source File: KafkaHeaderConverter.java    From micronaut-kafka with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<Object> convert(Header object, Class<Object> targetType, ConversionContext context) {
    byte[] v = object.value();
    return ConversionService.SHARED.convert(new String(v), targetType, context);
}
 
Example 11
Source File: RecordSerde.java    From kafka-backup with Apache License 2.0 4 votes vote down vote up
public static void write(OutputStream outputStream, Record record) throws IOException {
    DataOutputStream dataStream = new DataOutputStream(outputStream);
    dataStream.writeLong(record.kafkaOffset());
    // There is a special case where the timestamp type eqauls `CREATE_TIME` but is actually `null`.
    // This should not happen normally and I see it as a bug in the Client implementation of pykafka
    // But as Kafka accepts that value, so should Kafka Backup. Thus, this dirty workaround: we write the
    // timestamp type `-2` if the type is CREATE_TIME but the timestamp itself is null. Otherwise we would have
    // needed to change the byte format and for now I think this is the better solution.
    if (record.timestampType() == TimestampType.CREATE_TIME && record.timestamp() == null) {
        dataStream.writeInt(-2);
    } else {
        dataStream.writeInt(record.timestampType().id);
        if (record.timestampType() != TimestampType.NO_TIMESTAMP_TYPE) {
            dataStream.writeLong(record.timestamp());
        }
    }
    if (record.key() != null) {
        dataStream.writeInt(record.key().length);
        dataStream.write(record.key());
    } else {
        dataStream.writeInt(-1);
    }
    if (record.value() != null) {
        dataStream.writeInt(record.value().length);
        dataStream.write(record.value());
    } else {
        dataStream.writeInt(-1);
    }
    Header[] headers = record.headers().toArray();
    dataStream.writeInt(headers.length);
    for (Header header : record.headers()) {
        byte[] headerKeyBytes = header.key().getBytes(StandardCharsets.UTF_8);
        dataStream.writeInt(headerKeyBytes.length);
        dataStream.write(headerKeyBytes);
        if (header.value() != null) {
            dataStream.writeInt(header.value().length);
            dataStream.write(header.value());
        } else {
            dataStream.writeInt(-1);
        }
    }
}
 
Example 12
Source File: KafkaHeaders.java    From brave with Apache License 2.0 4 votes vote down vote up
@Nullable static String lastStringHeader(Headers headers, String key) {
  Header header = headers.lastHeader(key);
  if (header == null || header.value() == null) return null;
  return new String(header.value(), UTF_8);
}
 
Example 13
Source File: KafkaHeaders.java    From brave with Apache License 2.0 4 votes vote down vote up
@Nullable static String lastStringHeader(Headers headers, String key) {
  Header header = headers.lastHeader(key);
  if (header == null || header.value() == null) return null;
  return new String(header.value(), UTF_8);
}
 
Example 14
Source File: HeaderUtils.java    From extension-kafka with Apache License 2.0 3 votes vote down vote up
/**
 * Return the {@code value} stored under a given {@code key} inside the {@link Headers}. In case of missing entry
 * {@code null} is returned.
 *
 * @param headers the Kafka {@code headers} to pull the value from
 * @param key     the key corresponding to the expected value
 * @return the value corresponding to the given {@code key} in the {@code headers}
 */
@SuppressWarnings("ConstantConditions") // Null check performed by `Assert.isTrue`
public static byte[] value(Headers headers, String key) {
    isTrue(headers != null, () -> "Headers may not be null");
    Header header = headers.lastHeader(key);
    return header != null ? header.value() : null;
}