confluent_kafka.common

confluent_kafka.common: Java’s org.apache.kafka.common.

The value types of org.apache.kafka.common (TopicPartition, Uuid, Node, KafkaFuture, …) and its errors (KafkaError, InvalidRecordError). Every common.* subpackage is a module of its own, holding the types of that Java package (CLAUDE.md, Python Binding Conventions, Modules): common.acl, common.config, common.errors, common.header, common.metrics, common.network, common.protocol.types, common.quota, common.record, common.requests, common.resource, common.security.auth, common.security.token.delegation and common.serialization.

AsyncKafkaFuture

class confluent_kafka.common.AsyncKafkaFuture(*args: Any, **kwargs: Any)[source]

Bases: _KafkaFutureBase[T]

The asyncio peer of KafkaFuture: the future an asyncio class hands out. get() is awaited; the inner future is an asyncio.Future on the client’s event loop, and a cancelled future raises asyncio.CancelledError.

Java: org.apache.kafka.common.KafkaFuture.

BaseFunction: TypeAlias

A function which takes objects of type A and returns objects of type B.

alias of Callable[[_A], _B]

BiConsumer: TypeAlias

A consumer of two different types of object.

alias of Callable[[_A, _B], None]

static completed_future(*, value: U) → AsyncKafkaFuture[U][source]

Returns a new AsyncKafkaFuture that is already completed with the given value.

static all_of(*, futures: tuple[AsyncKafkaFuture[Any], ...]) → AsyncKafkaFuture[None][source]

Returns a new AsyncKafkaFuture that is completed when all the given futures have completed. If any future fails, the returned future fails with it. If multiple futures fail, the first failure is returned. A None futures, or a None among them, raises TypeError, as Java’s Arrays.stream(null) and kafkaFuture.toCompletionStage() do.

to_completion_stage() → Future[T][source]

Gets the inner asyncio.Future this future waits on, on the client’s event loop (or the running loop), which completes when this future completes and in the same way.

then_apply(*, function: Callable[[T], R]) → AsyncKafkaFuture[R][source]

Returns a new AsyncKafkaFuture that, when this future completes normally, is executed with this future’s result as the argument to the supplied function.

The function runs where this future completes (the event loop of an asyncio client), or inside this call when the future is already done.

when_complete(*, action: Callable[[T | None, BaseException | None], None]) → AsyncKafkaFuture[T][source]

Returns a new AsyncKafkaFuture with the same result or error as this future, that executes the given action when this future completes; see KafkaFuture.when_complete().

cancel(*, may_interrupt_if_running: bool) → bool[source]

If not already completed, completes this future with an asyncio.CancelledError. Dependent futures that have not already completed will also be cancelled. Never cancels the source, the request or the core.

async get() → T[source]
async get(*, timeout: Duration) → T

Waits if necessary for this future to complete, and then returns its result; with timeout, for at most that long, raising confluent_kafka.TimeoutError when it elapses first (a negative or zero timeout raises at once unless the future is done). A None timeout raises TypeError, as Java’s null TimeUnit does.

A failed future raises confluent_kafka.ExecutionError from the failure; a cancelled one asyncio.CancelledError. Cancelling the awaiting task does not cancel the future.

get_now(*, value_if_absent: T) → T[source]

Returns the result value (or raises any encountered error) if completed, else returns the given value_if_absent.

is_cancelled() → bool[source]

Returns true if this future was cancelled before it completed normally.

is_completed_exceptionally() → bool[source]

Returns true if this future completed exceptionally, in any way.

is_done() → bool[source]

Returns true if completed in any fashion: normally, exceptionally, or via cancellation.

ClassicGroupState

class confluent_kafka.common.ClassicGroupState(*values)[source]

Bases: Enum

The classic group state.

Java: org.apache.kafka.common.ClassicGroupState.

UNKNOWN = 'UNKNOWN'
PREPARING_REBALANCE = 'PREPARING_REBALANCE'
COMPLETING_REBALANCE = 'COMPLETING_REBALANCE'
STABLE = 'STABLE'
DEAD = 'DEAD'
EMPTY = 'EMPTY'
static parse(*, name: str) → ClassicGroupState[source]

Case-insensitive classic group state lookup by string name.

Cluster

confluent_kafka.common.Cluster

alias of object

ElectionType

class confluent_kafka.common.ElectionType(*values)[source]

Bases: IntEnum

Options for Admin.elect_leaders.

Java: org.apache.kafka.common.ElectionType.

PREFERRED = 0
UNCLEAN = 1
static value_of(*, value: int) → ElectionType[source]

The election type whose value is value; IllegalArgumentError when there is none.

GroupState

class confluent_kafka.common.GroupState(*values)[source]

Bases: Enum

The group state.

The correspondence between the group states and types:

State

Classic

Consumer

Share

Streams

UNKNOWN

Yes

Yes

Yes

Yes

PREPARING_REBALANCE

Yes

Yes

COMPLETING_REBALANCE

Yes

Yes

STABLE

Yes

Yes

Yes

Yes

DEAD

Yes

Yes

Yes

Yes

EMPTY

Yes

Yes

Yes

Yes

ASSIGNING

Yes

Yes

RECONCILING

Yes

Yes

NOT_READY

Yes

Java: org.apache.kafka.common.GroupState.

UNKNOWN = 'UNKNOWN'
PREPARING_REBALANCE = 'PREPARING_REBALANCE'
COMPLETING_REBALANCE = 'COMPLETING_REBALANCE'
STABLE = 'STABLE'
DEAD = 'DEAD'
EMPTY = 'EMPTY'
ASSIGNING = 'ASSIGNING'
RECONCILING = 'RECONCILING'
NOT_READY = 'NOT_READY'
static parse(*, name: str) → GroupState[source]

Case-insensitive group state lookup by string name.

static group_states_for_type(*, type: GroupType) → set[GroupState][source]

The states a group of type can be in; IllegalArgumentError for GroupType.UNKNOWN.

GroupType

class confluent_kafka.common.GroupType(*values)[source]

Bases: Enum

The group type.

Java: org.apache.kafka.common.GroupType.

UNKNOWN = 'UNKNOWN'
CONSUMER = 'CONSUMER'
CLASSIC = 'CLASSIC'
SHARE = 'SHARE'
STREAMS = 'STREAMS'
static parse(*, name: str | None) → GroupType[source]

Parse a string into a consumer group type, in a case-insensitive manner; UNKNOWN for None or an unknown name.

IsolationLevel

class confluent_kafka.common.IsolationLevel(*values)[source]

Bases: IntEnum

The isolation level of a read.

Java: org.apache.kafka.common.IsolationLevel.

READ_UNCOMMITTED = 0
READ_COMMITTED = 1
id() → int[source]
static for_id(*, id: int) → IsolationLevel[source]

The isolation level whose id is id; IllegalArgumentError when there is none.

KafkaFuture

class confluent_kafka.common.KafkaFuture(*args: Any, **kwargs: Any)[source]

Bases: _KafkaFutureBase[T]

A flexible future which supports call chaining and other asynchronous programming patterns.

It is possible to obtain the inner concurrent.futures.Future from a KafkaFuture by calling to_completion_stage(). A failed KafkaFuture raises ExecutionError from get(), with the failure as its __cause__, whereas the inner future raises the failure itself.

Java: org.apache.kafka.common.KafkaFuture.

BaseFunction: TypeAlias

A function which takes objects of type A and returns objects of type B.

alias of Callable[[_A], _B]

BiConsumer: TypeAlias

A consumer of two different types of object.

alias of Callable[[_A, _B], None]

static completed_future(*, value: U) → KafkaFuture[U][source]

Returns a new KafkaFuture that is already completed with the given value.

static all_of(*, futures: tuple[KafkaFuture[Any], ...]) → KafkaFuture[None][source]

Returns a new KafkaFuture that is completed when all the given futures have completed. If any future fails, the returned future fails with it. If multiple futures fail, the first failure is returned. A None futures, or a None among them, raises TypeError, as Java’s Arrays.stream(null) and kafkaFuture.toCompletionStage() do.

to_completion_stage() → Future[T][source]

Gets the inner concurrent.futures.Future this future waits on, which completes when this future completes and in the same way (with the same result or error, or cancelled).

To wait for a KafkaFuture use get() or get_now(), rather than the inner future’s result().

then_apply(*, function: Callable[[T], R]) → KafkaFuture[R][source]

Returns a new KafkaFuture that, when this future completes normally, is executed with this future’s result as the argument to the supplied function.

The function runs where this future completes (the producer’s completion thread, the admin client’s pump thread), or inside this call when the future is already done.

when_complete(*, action: Callable[[T | None, BaseException | None], None]) → KafkaFuture[T][source]

Returns a new KafkaFuture with the same result or error as this future, that executes the given action when this future completes.

When this future is done, the given action is invoked with the result (or None if none) and the error (or None if none) of this future as arguments. The returned future is completed when the action returns. The supplied action should not raise. However, if it does, the following rules apply: if this future completed normally but the supplied action raises, then the returned future fails with the action’s error. Or, if this future failed and the supplied action raises, then the returned future fails with this future’s error.

The action runs where this future completes, or inside this call when the future is already done.

cancel(*, may_interrupt_if_running: bool) → bool[source]

If not already completed, completes this future with a concurrent.futures.CancelledError. Dependent futures that have not already completed will also be cancelled. Never cancels the source, the request or the core.

get() → T[source]
get(*, timeout: Duration) → T

Waits if necessary for this future to complete, and then returns its result; with timeout, for at most that long, raising confluent_kafka.TimeoutError when it elapses first (a negative or zero timeout raises at once unless the future is done). A None timeout raises TypeError, as Java’s null TimeUnit does.

A failed future raises confluent_kafka.ExecutionError from the failure; a cancelled one concurrent.futures.CancelledError. The wait is in short slices, so Ctrl+C ends it.

get_now(*, value_if_absent: T) → T[source]

Returns the result value (or raises any encountered error) if completed, else returns the given value_if_absent.

is_cancelled() → bool[source]

Returns true if this future was cancelled before it completed normally.

is_completed_exceptionally() → bool[source]

Returns true if this future completed exceptionally, in any way.

is_done() → bool[source]

Returns true if completed in any fashion: normally, exceptionally, or via cancellation.

Metric

class confluent_kafka.common.Metric[source]

Bases: Protocol

A metric tracked for monitoring purposes.

Java: org.apache.kafka.common.Metric.

metric_name() → MetricName[source]

A name for this metric.

metric_value() → Any[source]

The value of the metric, which may be measurable or a non-measurable gauge.

MetricName

class confluent_kafka.common.MetricName(*, name: str, group: str, description: str, tags: Mapping[str, str])[source]

Bases: object

The MetricName class encapsulates a metric’s name, logical group and its related attributes.

  • name: the name of the metric;

  • group: logical group name of the metrics to which this metric belongs;

  • description: a human-readable description to include in the metric;

  • tags: additional key/value attributes of the metric.

group and tags can be used to create unique metric names while reporting in JMX or any custom reporting. Equality and the hash are over group, name and tags.

Java: org.apache.kafka.common.MetricName (final).

name() → str[source]
group() → str[source]
tags() → dict[str, str][source]
description() → str[source]

Node

class confluent_kafka.common.Node(*, id: int, host: str, port: int)[source]
class confluent_kafka.common.Node(*, id: int, host: str, port: int, rack: str | None)
class confluent_kafka.common.Node(*, id: int, host: str, port: int, rack: str | None, is_fenced: bool)

Bases: object

Information about a Kafka node.

Java: org.apache.kafka.common.Node.

static no_node() → Node[source]
is_empty() → bool[source]

Check whether this node is empty, which may be the case if no_node() is used as a placeholder in a response payload with an error.

id() → int[source]

The node id of this node.

id_string() → str[source]

String representation of the node id. Typically the integer id is used to serialize over the wire, the string representation is used as an identifier with NetworkClient code.

host() → str[source]

The host name for this node.

port() → int[source]

The port for this node.

has_rack() → bool[source]

True if this node has a defined rack.

rack() → str | None[source]

The rack for this node.

is_fenced() → bool[source]

Returns whether this node is fenced. This applies to broker nodes only. For controller quorum nodes, this field is not relevant and is defined to be False.

PartitionInfo

class confluent_kafka.common.PartitionInfo(*, topic: str, partition: int, leader: Node | None, replicas: tuple[Node, ...], in_sync_replicas: tuple[Node, ...])[source]
class confluent_kafka.common.PartitionInfo(*, topic: str, partition: int, leader: Node | None, replicas: tuple[Node, ...], in_sync_replicas: tuple[Node, ...], offline_replicas: tuple[Node, ...])

Bases: object

This is used to describe per-partition state in the MetadataResponse.

Java: org.apache.kafka.common.PartitionInfo.

topic() → str[source]

The topic name.

partition() → int[source]

The partition id.

leader() → Node | None[source]

The node id of the node currently acting as a leader for this partition or None if there is no leader.

replicas() → tuple[Node, ...][source]

The complete set of replicas for this partition regardless of whether they are alive or up-to-date. The preferred replica is the head of the list.

in_sync_replicas() → tuple[Node, ...][source]

The subset of the replicas that are in sync, that is caught-up to the leader and ready to take over as leader if the leader should fail.

offline_replicas() → tuple[Node, ...][source]

The subset of the replicas that are offline.

TopicCollection

class confluent_kafka.common.TopicCollection(*args: Any, **kwargs: Any)[source]

Bases: object

A class used to represent a collection of topics. This collection may define topics by name or ID.

Java: org.apache.kafka.common.TopicCollection.

class TopicIdCollection(*args: Any, **kwargs: Any)

Bases: TopicCollection

A class used to represent a collection of topics defined by their topic ID. Subclassing this class beyond the classes provided here is not supported.

Java: org.apache.kafka.common.TopicCollection.TopicIdCollection.

topic_ids() → list[Uuid]

A collection of topic IDs.

class TopicNameCollection(*args: Any, **kwargs: Any)

Bases: TopicCollection

A class used to represent a collection of topics defined by their topic name. Subclassing this class beyond the classes provided here is not supported.

Java: org.apache.kafka.common.TopicCollection.TopicNameCollection.

topic_names() → list[str]

A collection of topic names.

static of_topic_ids(*, topics: Iterable[Uuid]) → TopicIdCollection[source]

A collection of topics defined by topic ID.

static of_topic_names(*, topics: Iterable[str]) → TopicNameCollection[source]

A collection of topics defined by topic name.

TopicIdPartition

class confluent_kafka.common.TopicIdPartition(*, topic_id: Uuid, topic_partition: TopicPartition)[source]
class confluent_kafka.common.TopicIdPartition(*, topic_id: Uuid, partition: int, topic: str | None)

Bases: object

This represents universally unique identifier with topic id for a topic partition. This makes sure that topics recreated with the same name will always have unique topic identifiers.

Java: org.apache.kafka.common.TopicIdPartition.

topic_id() → Uuid[source]

Universally unique id representing this topic partition.

topic() → str | None[source]

The topic name or None if it is unknown.

partition() → int[source]

The partition id.

topic_partition() → TopicPartition[source]

Topic partition representing this instance.

TopicPartition

class confluent_kafka.common.TopicPartition(*, topic: str, partition: int)[source]

Bases: object

A topic name and partition number.

Java: org.apache.kafka.common.TopicPartition. Immutable, with value equality, so it can key a dict.

partition() → int[source]
topic() → str[source]

TopicPartitionInfo

class confluent_kafka.common.TopicPartitionInfo(*, partition: int, leader: Node | None, replicas: Sequence[Node], isr: Sequence[Node], elr: Sequence[Node], last_known_elr: Sequence[Node])[source]
class confluent_kafka.common.TopicPartitionInfo(*, partition: int, leader: Node | None, replicas: Sequence[Node], isr: Sequence[Node])

Bases: object

A class containing leadership, replicas and ISR information for a topic partition.

Java: org.apache.kafka.common.TopicPartitionInfo.

partition() → int[source]

Return the partition id.

leader() → Node | None[source]

Return the leader of the partition or None if there is none.

replicas() → list[Node][source]

Return the replicas of the partition in the same order as the replica assignment. The preferred replica is the head of the list.

Brokers with version lower than 0.11.0.0 return the replicas in unspecified order due to a bug.

isr() → list[Node][source]

Return the in-sync replicas of the partition. Note that the ordering of the result is unspecified.

elr() → list[Node] | None[source]

Return the eligible leader replicas of the partition. Note that the ordering of the result is unspecified.

last_known_elr() → list[Node] | None[source]

Return the last known eligible leader replicas of the partition. Note that the ordering of the result is unspecified.

TopicPartitionReplica

class confluent_kafka.common.TopicPartitionReplica(*, topic: str, partition: int, broker_id: int)[source]

Bases: object

The topic name, partition number and the brokerId of the replica.

Java: org.apache.kafka.common.TopicPartitionReplica.

topic() → str[source]
partition() → int[source]
broker_id() → int[source]

Uuid

class confluent_kafka.common.Uuid(*, most_sig_bits: int, least_sig_bits: int)[source]

Bases: object

This class defines an immutable universally unique identifier (UUID). It represents a 128-bit value. More specifically, the random UUIDs generated by this class are variant 2 (Leach-Salz) version 4 UUIDs. This is the same type of UUID as the ones generated by java.util.UUID. The __str__ method prints using the base64 string encoding. Likewise, the from_string method expects a base64 string encoding.

Java: org.apache.kafka.common.Uuid.

ONE_UUID: ClassVar[Uuid] = <confluent_kafka.common.uuid.Uuid object>

A reserved UUID. Will never be returned by the random_uuid method.

METADATA_TOPIC_ID: ClassVar[Uuid] = <confluent_kafka.common.uuid.Uuid object>

A UUID for the metadata topic in KRaft mode. Will never be returned by the random_uuid method.

ZERO_UUID: ClassVar[Uuid] = <confluent_kafka.common.uuid.Uuid object>

A UUID that represents a null or empty UUID. Will never be returned by the random_uuid method.

RESERVED: ClassVar[frozenset[Uuid]] = frozenset({<confluent_kafka.common.uuid.Uuid object>, <confluent_kafka.common.uuid.Uuid object>})

The set of reserved UUIDs that will never be returned by the random_uuid method.

static random_uuid() → Uuid[source]

Static factory to retrieve a type 4 (pseudo randomly generated) UUID. This will not generate a UUID equal to 0, 1, or one whose string representation contains a dash (“-“).

most_significant_bits() → int[source]

Returns the most significant bits of the UUID’s 128 value.

least_significant_bits() → int[source]

Returns the least significant bits of the UUID’s 128 value.

static from_string(*, str: str) → Uuid[source]

Creates a UUID based on a base64 string encoding used in the __str__ method. (str is Java’s parameter name; it shadows the builtin only inside this method, whose body does not call str().) A None raises TypeError, as Java’s str.length().

static to_array(*, list: Sequence[Uuid] | None) → tuple[Uuid, ...] | None[source]

Convert a list of Uuid to an array of Uuid (None for None).

static to_list(*, array: tuple[Uuid, ...] | None) → list[Uuid] | None[source]

Convert an array of Uuids to a list of Uuid (None for None).

InvalidRecordError

exception confluent_kafka.common.InvalidRecordError(*, message: str | None)
exception confluent_kafka.common.InvalidRecordError(*, message: str | None, cause: BaseException | None)

Bases: InvalidConfigurationError

Java: org.apache.kafka.common.InvalidRecordException.

KafkaError

exception confluent_kafka.common.KafkaError(*, message: str | None, cause: BaseException | None)
exception confluent_kafka.common.KafkaError(*, message: str | None)
exception confluent_kafka.common.KafkaError(*, cause: BaseException | None)
exception confluent_kafka.common.KafkaError

Bases: RuntimeError

The base class of all other Kafka exceptions

Java: org.apache.kafka.common.KafkaException.