This is a simple package for using Kafka in Golang with the Observer-pattern.
We have two Kafka-topics in JSON and want to serialize the events in a pipeline.
in order to serialize our events
type BookRequest struct {
Book string `json:"book"`
Person string `json:"person"`
}
const config = &kafkaesque.Config{
AutoOffsetReset: kafkaesque.AutoOffsetReset.Latest,
BootstrapServers: []string{"localhost:9092"},
IsolationLevel: kafkaesque.IsolationLevel.ReadCommitted,
MessageMaxBytes: 1048588,
ClientID: "test_1",
GroupID: "test_group",
SecurityProtocol: kafkaesque.SecurityProtocol.Plaintext,
}
testConsumer, err := kafkaesque.NewConsumer(config, "book-requests", "external-book-requests")
if err != nil {
panic(err)
}
bookRequests := testConsumer.Events.
Map(func(_ context.Context, item interface{}) (interface{}, error) {
var request BookRequest
err := json.Unmarshal(kafkaesque.ValueToBytes(item), &request)
return request, err
})
for event := range bookRequests.Observe() {
log.Println(fmt.Sprintf("%T", event.V))
}
-
Start your Confluent Plattform:
cd example docker-compose up
-
Start the program:
go run . -name test_consumer -topics test
-
In a new terminal run:
docker exec -it broker bin/kafka-console-producer --topic test --bootstrap-server localhost:9092
-
Insert a BookRequest in JSON:
{"book": "Moby Dick", "person": "Max Musterknabe"}
The output should be:
2020/10/03 15:32:08 Showing events for [test test1 test2] on [localhost:9092]: 2020/10/03 15:32:16 Book: Moby Dick Person: Max Musterknabe
Or start the example as producer:
go run . -mode producer -name test_producer -topics test
The output should be:
2020/10/03 15:32:08 Showing events for [test test1 test2] on [localhost:9092]: 2020/10/03 15:32:16 Book: 10 Awesome Ways to Photograph Blue Bottles Person: Pamela Osborne 2020/10/03 15:33:02 Book: 10 Awesome Ways to Photograph Blue Bottles Person: Pamela Osborne 2020/10/03 15:33:02 Book: 21 Myths About Blue bottles Debunked Person: Inayah Brown 2020/10/03 15:33:02 Book: How to Make Your Own Vast Hat for less than £5 Person: Ikrah Blair 2020/10/03 15:33:03 Book: An analysis of handsome jugs Person: Aurora Stafford ...