An open API service indexing awesome lists of open source software.

https://github.com/bewaremypower/kafkaclientdemo

Kafka client demos
https://github.com/bewaremypower/kafkaclientdemo

Last synced: over 1 year ago
JSON representation

Kafka client demos

Awesome Lists containing this project

README

          

## KafkaClientDemo

Kafka client demos.

## Usage

Build the jar with dependencies and run:

```bash
$ mvn clean package
$ java -jar target/KafkaClientDemo-1.0-SNAPSHOT-jar-with-dependencies.jar
```

The program will start a background consumer that consumes and print messages and a console producer that accepts input from standard input and send each line as a message.

e.g. send `hello` and `world`:

```
hello
Send line: hello
2020-09-08 19:19:31:554 [kafka-producer-network-thread | producer-1] INFO org.apache.kafka.clients.Metadata - Cluster ID: ZtF869vnQHiChoclRBaBOw
[0] ConsumerRecord(topic = test_xyz, partition = 2, offset = 2, CreateTime = 1599563971562, serialized key size = -1, serialized value size = 5, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = hello)
world
Send line: world
[1] ConsumerRecord(topic = test_xyz, partition = 1, offset = 2, CreateTime = 1599563972996, serialized key size = -1, serialized value size = 5, headers = RecordHeaders(headers = [], isReadOnly = false), key = null, value = world)
```

If you want to change the properties files and rerun, use Maven to execute the class instead of run the `*.jar`:

```bash
# Make sure that you have build the classes into target/classes/ before, use `mvn compile`
$ mvn exec:java -Dexec.mainClass=io.github.bewaremypower.Starter
```

## Config

Here're three properties files in [resources](src/main/resources/) directory.

In `kafka.properties` you can configure the topic name.

The `producer.properties` and `consumer.properties` are the properties to construct [`KafkaProducer`](https://kafka.apache.org/20/javadoc/org/apache/kafka/clients/producer/KafkaProducer.html) and [`KafkaConsumer`](https://kafka.apache.org/20/javadoc/org/apache/kafka/clients/consumer/KafkaConsumer.html).

In addition, the `consumer.properties` has extra properties:

| Key | Value |
| ------------------ | ------------------------------------------------------------ |
| `poll.timeout.ms` | The poll timeout in milliseconds each time the consumer call `poll` |
| `max.num.messages` | The maximum number of messages that the consumer could receive |