Apache Kafka 101: Your First Kafka Application in 10 Minutes or Less (Hands On - 2023)
Introduction to Apache Kafka
In this section, we will learn about what Apache Kafka is and how to set up a Kafka cluster using Confluent Cloud.
Setting Up a Kafka Cluster with Confluent Cloud
- To get started with Apache Kafka, you'll need to create a Confluent Cloud account.
- Once you've created an account, select the "Topics" tab on the left-hand side of the screen and choose "Create Topic".
- Enter a name for your topic and select "Create with Defaults".
Understanding Partitions in Kafka
- Topics in Kafka are broken down into smaller components called partitions. When you write a message to a topic, it is stored in one of the topic's partitions based on its key.
- Messages with different keys may be distributed among different partitions of the same topic.
Producing and Consuming Messages in Kafka
In this section, we will learn how to produce and consume messages in Apache Kafka.
Producing Messages
- To produce messages in Kafka, select the "Messages" tab on your topic's overview screen and then click "Produce a New Message".
- Enter a key-value pair for your message (a message or event in kafka is treated as a key-value pair).
Consuming Messages
- To consume messages from your topic, select the "Messages" tab on your topic's overview screen and then click "Consume".
- You can also use command-line tools like
kafka-console-consumerto consume messages from your topic.
Conclusion
In this section, we will wrap up our introduction to Apache Kafka.
Best Practices
- Remember to stop and delete any resources you created in Confluent Cloud to avoid exhausting your free usage.
- Use the
kafka-console-consumertool to consume messages from your topic.
Final Thoughts
- Apache Kafka is a powerful distributed streaming platform that can be used for a variety of use cases. With Confluent Cloud, it's easy to get started with Kafka and start producing and consuming messages in just a few minutes.
Producing and Consuming Data from a Kafka Topic
This section covers the different methods to produce data to a Kafka topic, including using the producer API or Kafka Connect. It also introduces another manual method for writing data using the Confluent Cloud command line interface.
Using the Confluent Cloud Command Line Interface
- To use the Confluent Cloud command line interface, download and update the command line tools.
- Run
confluent login saveto prompt for your username and password.
- Select your environment by running
confluent environment list.
- Set your environment by running
confluent use <environment-id>.
- Choose which Kafka cluster within your environment you'd like to use by running
confluent kafka cluster listand thenconfluent use <cluster-resource-id>.
- Provide an API key in order to connect to the cluster by running
confluent create api key. Save this securely somewhere.
- Use that key by running
confluent use api-key.
Interacting with Your Confluent Cloud Cluster
- List all of the topics available to you by running
confluent kafka topic list.
- To consume messages from the beginning of a topic, run
topic consume --from-beginning.
- To produce messages, run
topic produce --parse-keyfollowed by a colon-delimited string that will be parsed into both a key and value.
- View newly produced messages in the web interface on Confluent Cloud by specifying an offset for a given partition from which to consume.
Conclusion
This section concludes the tutorial on getting started with Confluent Cloud and Kafka. It encourages viewers to continue their Kafka journey and learn more about its features.
Key Takeaways
- By following this exercise, viewers have accomplished several things, including signing up for Confluent Cloud and creating their first Kafka cluster, adding messages to a topic using the web console, installing the CLI and creating an API key, and using the CLI producer and consumer.
- Viewers are encouraged to continue their Kafka journey and learn more about its features.