Code Monkey home page Code Monkey logo

kafka_training's Introduction

Running a Solo Kafka Contain Hello World example

After you finish the followig steps will will have successfully

  • Installed and started a Kafka single node cluster on your computer. This will include one ZooKeeper and one Kafka Broker
  • Create one Kafka Topic
  • Create one Kafka Producer
  • Create one Kafka Consumer
  • Send Messages from the Kafka Producer to the Kafka Consumer

Setting up Kafka Docker Container

The following line will start a kafka docker container on your local

docker run \
-p 2181:2181 \
-p 9092:9092 \
--env ADVERTISED_HOST=127.0.0.1 \
--env ADVERTISED_PORT=9092 \
--name mykafka \
--detach \
spotify/kafka

The following line will list out of the docker containers you have running on your local

docker ps

You should see something like the following

   CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                            NAMES
   a7b9cf39eb05        spotify/kafka       "supervisord -n"    39 seconds ago      Up 38 seconds       0.0.0.0:2181->2181/tcp, 0.0.0.0:9092->9092/tcp   mykafka

Create a Topic

The following commands will get you into the container using bash and there we will create and list topics

docker exec -it mykafka bash

cd /opt/kafka_2.11-0.10.1.0/bin

./kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 2 --topic training

./kafka-topics.sh --list --zookeeper localhost:2181

Produce to a Topic and Consume it

In the same cmd terminal use the following commands to set up a producer

./kafka-console-producer.sh --broker-list localhost:9092 --topic training

Then open up a new cmd terminal and execute the following commands to set up a consumer

docker exec -it mykafka bash

cd /opt/kafka_2.11-0.10.1.0/bin

./kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic training --from-beginning

Then in the producer window type in some random text and press enter and repeat a couple of times.
Then look at the output in the consumer terminal.

Now produce and consume from you local Kafka from a Java Application

The following steps will show how you can interact with you newly created Kafka container with a Java application running on your local. After you finish these steps you will have finished the following:

  • Build a Java application with Maven with Kafka dependencies
  • Execute a Java application with a production to Kafka
  • Execute a Java application will a consumer from Kafka
  • See how the following api codes work -- Production Listener -- Consumer Listener

Build the Java Code

First we need to build the project. To do this just go into this director with a terminal and execute the following command

mvn package

If you are successful you should see jars in a newly created directory called target

Execute the Basic Java Code Examples

Now we want to execute our java applications. First we will execute the consumer example with the following line

java -cp target/KafkaTraining.jar com.malaska.kafka.training.BasicConsumerExample 127.0.0.1 9092 training

The parameters are as follows:

  1. Broker Host
  2. Broker Port
  3. Topic to consume from

Then in a different terminal we will execute the producer with the following line

java -cp target/KafkaTraining.jar com.malaska.kafka.training.BasicProducerExample \
127.0.0.1 9092 training \
1000 10 100 50

There parameters are as follows:

  1. Broker Host
  2. Broker Port
  3. Topic to produce too
  4. Number of messages to send
  5. Number of messages between thread waits
  6. Wait time to slow down publishing
  7. Execute a call back every N messages

Now look at the consumer terminal to see the output

Execute the Latency Java Code Examples

Now we want to execute our java applications. First we will execute the consumer example with the following line

java -cp target/KafkaTraining.jar com.malaska.kafka.training.LatencyConsumerExample 127.0.0.1 9092 training

The parameters are as follows:

  1. Broker Host
  2. Broker Port
  3. Topic to consume from

Then in a different terminal we will execute the producer with the following line

java -cp target/KafkaTraining.jar com.malaska.kafka.training.LatencyProducerExample \
127.0.0.1 9092 training 1000 10 100 1 100

There parameters are as follows:

  1. Broker Host
  2. Broker Port
  3. Topic to produce too
  4. Number of messages to send
  5. Number of messages between thread waits
  6. Wait time to slow down publishing
  7. lingerMs
  8. batchSize (in Messages)

Now look at the consumer terminal to see the output

#Extra notes

Checking out the consumer group

./kafka-consumer-groups.sh --zookeeper localhost:2181 --list

./kafka-consumer-groups.sh --zookeeper localhost:2181 --describe --group test-consumer-group

kafka_training's People

Contributors

tedbear42 avatar medale avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.