Updated on 2024-08-12 GMT+08:00

Java API

Versions of interfaces adopted by Kafka are consistent with those in Open Source Community. For details, see https://kafka.apache.org/24/documentation.html.

Major Interfaces of a Producer

Table 1 Major parameters of a Producer

Parameter

Description

Remarks

bootstrap.servers

Broker address list

The Producer creates connections with the Broker based on this parameter.

sasl.kerberos.service.name

Service name

This parameter specifies the Kerberos user name used by the Kafka cluster for running. This parameter needs to be configured as kafka.

key.serializer

Serialization of key values in messages

This parameter specifies how to serialize the key values in messages.

value.serializer

Serialization of messages

This parameter specifies how to serialize transmitted messages.

Table 2 Major interface functions of a Producer

Return Value

Interface Function

Description

java.util.concurrent.Future<RecordMetadata>

send(ProducerRecord<K,V> record)

Indicates a TX interface without a callback function. Generally, the get() function of Future is used for synchronous transmission.

java.util.concurrent.Future<RecordMetadata>

send(ProducerRecord<K,V> record, Callback callback)

Indicates a TX interface with a callback function. Generally, this interface uses the callback function to process transmission results after asynchronous transmission.

void

onCompletion(RecordMetadata metadata, Exception exception);

Indicates the interface method for a callback function. This method is used to process asynchronous transmission results.

Major Interfaces of a Consumer

Table 3 Major parameters of a Consumer

Parameter

Description

Remarks

bootstrap.servers

Broker address list

The Consumer creates connections with the Broker based on this parameter.

sasl.kerberos.service.name

Service name

This parameter specifies the Kerberos user name used by the Kafka cluster for running. This parameter needs to be configured as kafka.

key.deserializer

Deserialization of key values in messages

This parameter specifies how to deserialize the key values in messages.

value.deserializer

Deserialization of messages

This parameter specifies how to deserialize received messages.

Table 4 Major interface functions of aConsumer

Return Value

Interface Function

Description

void

close()

Indicates the interface method for closing the Consumer.

void

subscribe(java.util.Collection<java.lang.String> topics)

Indicates the interface method for subscribing topics.

ConsumerRecords<K,V>

poll(final Duration timeout)

Indicates the interface method for requesting messages.