View Source KafkaEx.Messages.Header (kafka_ex v1.0.0-rc.1)

Kafka message header (key-value pair).

Headers were introduced in Kafka 0.11 (Produce API V3+) and allow attaching additional metadata to messages without modifying the message payload. Common uses include tracing information, content types, and routing metadata.

Java equivalent: org.apache.kafka.common.header.Header

Summary

Functions

Builds a Header from keyword options.

Creates a Header from a tuple {key, value}.

Returns the header key.

Converts a list of tuples to Headers.

Converts a list of Headers to tuple format.

Creates a new Header with the given key and value.

Converts the Header to a tuple {key, value}.

Returns the header value.

Types

@type t() :: %KafkaEx.Messages.Header{key: String.t(), value: binary()}

Functions

@spec build(Keyword.t()) :: t()

Builds a Header from keyword options.

Options

  • :key - (required) The header key
  • :value - (required) The header value

Examples

iex> Header.build(key: "x-custom", value: "data")
%Header{key: "x-custom", value: "data"}
@spec from_tuple({String.t(), binary()}) :: t()

Creates a Header from a tuple {key, value}.

This is useful for converting from the common tuple format used in many APIs.

Examples

iex> Header.from_tuple({"content-type", "text/plain"})
%Header{key: "content-type", value: "text/plain"}
@spec key(t()) :: String.t()

Returns the header key.

This is provided for API compatibility with Java's Header.key().

Link to this function

list_from_tuples(tuples)

View Source
@spec list_from_tuples([{String.t(), binary()}]) :: [t()]

Converts a list of tuples to Headers.

Examples

iex> tuples = [{"a", "1"}, {"b", "2"}]
iex> Header.list_from_tuples(tuples)
[%Header{key: "a", value: "1"}, %Header{key: "b", value: "2"}]
@spec list_to_tuples([t()]) :: [{String.t(), binary()}]

Converts a list of Headers to tuple format.

Examples

iex> headers = [Header.new("a", "1"), Header.new("b", "2")]
iex> Header.list_to_tuples(headers)
[{"a", "1"}, {"b", "2"}]
@spec new(String.t(), binary()) :: t()

Creates a new Header with the given key and value.

Parameters

  • key - The header key (must be a string)
  • value - The header value (binary data)

Examples

iex> Header.new("content-type", "application/json")
%Header{key: "content-type", value: "application/json"}

iex> Header.new("trace-id", <<1, 2, 3>>)
%Header{key: "trace-id", value: <<1, 2, 3>>}
@spec to_tuple(t()) :: {String.t(), binary()}

Converts the Header to a tuple {key, value}.

This is useful for interoperability with APIs that expect tuple format.

Examples

iex> header = Header.new("x-custom", "value")
iex> Header.to_tuple(header)
{"x-custom", "value"}
@spec value(t()) :: binary()

Returns the header value.

This is provided for API compatibility with Java's Header.value().