Kafka provides a series of Application Programming Interfaces (APIs). These make it easier to write applications that use Kafka.
Kafka maintains a set of client libraries for Java, although there are open-source projects providing similar support to a variety of other languages.
Include these client libraries in your application to easily interact with Kafka!
- Producer API: This allows you to build producers that publish messages to Kafka
- Consumer API: This allows you to build consumers that read Kafka messages.
- Streams API: This allows you to read from input topics, transform data, and output it to output topics
- Connect API: This allows you to build custom connectors, which pull from or push to specific external systems.
- AdminClient API: This allows you to manage and inspect higher-level objects like topics and brokers.
Producer data to Kafka
package kafka.client;
import org.apache.kafka.clients.producer.*;
import java.util.Properties;
public class Main {
public static void main(String[] args) {
Properties props = new Properties();
props.put("bootstrap.servers", "localhost:9092");
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
Producer<String, String> producer = new KafkaProducer<>(props);
for (int i = 0; i < 100; i++) {
producer.send(new ProducerRecord<String, String>("count-topic", "count", Integer.toString(i)));
}
producer.close();
}
}
dependencies {
implementation 'org.apache.kafka:kafka-clients:2.2.1'
testImplementation 'junit:junit:4.12'
}
- dependencies
Execute
./gradlew run
kafka-console-consumer --bootstrap-server localhost:9092 --topic count-topic --from-beginning