Apache Kafka Tutorial: An Introduction to Distributed Messaging Systems

  15   0   0   0   18 tuteeHUB earn credit +10 pts
Apache Kafka Tutorial: An Introduction to Distributed Messaging Systems

Creating Topics in Kafka



Kafka is a distributed streaming platform that allows you to publish and subscribe to streams of records. A record is a key-value pair that can contain any data. A stream is a sequence of records that are produced and consumed by applications.

One of the core concepts in Kafka is a topic. A topic is a logical name for a stream of records. You can create topics in Kafka using the command-line tool kafka-topics.sh or the Admin API.

To create a topic using the command-line tool, you need to specify the name of the topic, the number of partitions, and the replication factor. For example, to create a topic named "test" with 3 partitions and 2 replicas, you can run:

kafka-topics.sh --create --topic test --partitions 3 --replication-factor 2 --bootstrap-server localhost:9092

To create a topic using the Admin API, you need to use the AdminClient class and call the createTopics method with a collection of NewTopic objects. For example, to create the same topic as above in Java, you can write:

AdminClient admin = AdminClient.create(props);
NewTopic newTopic = new NewTopic("test", 3, (short) 2);
admin.createTopics(Collections.singleton(newTopic));

Conclusion

Creating topics in Kafka is easy and flexible. You can use either the command-line tool or the Admin API to create topics with different configurations. Topics are essential for producing and consuming data streams in Kafka.

FAQs

Q: How do I list all the topics in Kafka?

A: You can use the command-line tool kafka-topics.sh with the --list option or call the listTopics method of the AdminClient class.

Q: How do I delete a topic in Kafka?

A: You can use the command-line tool kafka-topics.sh with the --delete option or call the deleteTopics method of the AdminClient class. Note that deleting a topic may not be immediate and may depend on some broker configurations.

Q: How do I change the configuration of a topic in Kafka?

A: You can use the command-line tool kafka-configs.sh with the --alter option or call the alterConfigs method of the AdminClient class. You can change various parameters such as retention time, compression type, segment size,etc.


Previous Next

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community