From the course: Apache Kafka Essential Training: Building Scalable Applications

Unlock the full course today

Join today to access over 24,900 courses taught by industry experts.

Consuming data in Java

Consuming data in Java

- [Instructor] As the final exercise, let's build a consumer in Java for the kafka.usecase.students topic. The code for this exercise is in the class UseCaseConsumerWithOptions.java. Let's review the code now. We start off by setting the required parameters including the bootstrap servers, these serializers for key and value, and a group ID. We also set the offset reset to receive data from the earliest messages available. As required by the exercise, we set the batching parameters here. We set fetch max bytes to 20 bytes and FETCH_MAX_WAIT_MS to 200. We also turn off auto-commit. We can now proceed to create a consumer and subscribe to the kafka.usecase.students topic. We then start a while loop and poll for new messages every 100 milliseconds. We print the records received as part of the processing. Finally, we use the commitAsync method to commit the data. Let's execute this code and review the results. The…

Contents