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

The following examples show how to use org.apache.kafka.common.header.Header#key() . 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: LiKafkaClientsUtils.java    From li-apache-kafka-clients with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * Special header keys have a "_" prefix and are managed internally by the clients.
 * @param headers kafka headers object
 * @return any "special" headers container in the argument map
 */
public static Map<String, byte[]> fetchSpecialHeaders(Headers headers) {
  Map<String, byte[]> map = new HashMap<>();
  for (Header header : headers) {

    if (!header.key().startsWith("_")) {
      // skip any non special header
      continue;
    }

    if (map.containsKey(header.key())) {
      throw new IllegalStateException("Duplicate special header found " + header.key());
    }
    map.put(header.key(), header.value());
  }
  return map;
}
 
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: ConsumerRecordsIteratorWrapper.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
public ConsumerRecord next() {
    endCurrentTransaction();
    ConsumerRecord record = delegate.next();
    try {
        String topic = record.topic();
        if (!WildcardMatcher.isAnyMatch(messagingConfiguration.getIgnoreMessageQueues(), topic)) {
            Transaction transaction = tracer.startChildTransaction(record, KafkaRecordHeaderAccessor.instance(), ConsumerRecordsIteratorWrapper.class.getClassLoader());
            if (transaction != null) {
                transaction.withType("messaging").withName("Kafka record from " + topic).activate();
                transaction.setFrameworkName(FRAMEWORK_NAME);

                Message message = transaction.getContext().getMessage();
                message.withQueue(topic);
                if (record.timestampType() == TimestampType.CREATE_TIME) {
                    message.withAge(System.currentTimeMillis() - record.timestamp());
                }

                if (transaction.isSampled() && coreConfiguration.isCaptureHeaders()) {
                    for (Header header : record.headers()) {
                        String key = header.key();
                        if (!TraceContext.TRACE_PARENT_BINARY_HEADER_NAME.equals(key) &&
                            WildcardMatcher.anyMatch(coreConfiguration.getSanitizeFieldNames(), key) == null) {
                            message.addHeader(key, header.value());
                        }
                    }
                }

                if (transaction.isSampled() && coreConfiguration.getCaptureBody() != CoreConfiguration.EventType.OFF) {
                    message.appendToBody("key=").appendToBody(String.valueOf(record.key())).appendToBody("; ")
                        .appendToBody("value=").appendToBody(String.valueOf(record.value()));
                }
            }
        }
    } catch (Exception e) {
        logger.error("Error in transaction creation based on Kafka record", e);
    }
    return record;
}