https://github.com/kuper-tech/protokaf
Kafka producer and consumer tool in protobuf format.
https://github.com/kuper-tech/protokaf
Last synced: about 1 year ago
JSON representation
Kafka producer and consumer tool in protobuf format.
- Host: GitHub
- URL: https://github.com/kuper-tech/protokaf
- Owner: Kuper-Tech
- License: apache-2.0
- Created: 2021-09-06T15:08:37.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-08-01T10:10:38.000Z (almost 2 years ago)
- Last Synced: 2025-03-24T08:47:45.858Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 74.2 KB
- Stars: 148
- Watchers: 4
- Forks: 7
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# protokaf
Kafka producer and consumer tool in protobuf format.
## Features
- Consume and produce messages using Protobuf protocol
- Trace messages with Jaeger
- Create custom templates for one or multiple messages and produce them to Kafka
## Install
```sh
go install github.com/kuper-tech/protokaf@latest
```
## Configuration
Configuration file is optional, so you can skip this section.
In order for Protokaf to work, it needs to know how to reach your Kafka broker. First option is to provide `--broker` each time you invoke Protokaf. Another option is to use a configuration file. You can provide Protokaf with a configuration file with option `-F ..` on the command line. Or by default Protokaf will search its config files in `.protokaf.yaml` and `$HOME/.protokaf.yaml` respectively.
**Example of `.protokaf.yaml`**
```yaml
debug: true
broker: ":"
kafka-auth-dsn: "SCRAM-SHA-512::"
proto: "/"
```
## Help
```sh
$ protokaf help
```
## List metadata
```sh
$ protokaf list [-t (,...)]
1 brokers:
broker 1 "127.0.0.1:9093"
2 topics:
topic "test-topic", partitions: 1
partition 0, leader 1, replicas: [1] (offline: []), isrs: [1]
topic "test", partitions: 1
partition 0, leader 1, replicas: [1] (offline: []), isrs: [1]
```
## Produce
### Help
```sh
$ protokaf produce -h
```
### Examples
This proto file will be used in the examples below.
`api/example.proto`
```protobuf
syntax = "proto3";
package example;
message HelloRequest {
string name = 1;
int32 age = 2;
}
```
**A simple produce message**
```sh
$ protokaf produce HelloRequest \
--broker kafka:9092 \
--proto internal/proto/testdata/example.proto \
--topic test \
--data '{"name": "Alice", "age": 11}'
```
**Produce message with headers**
```sh
$ protokaf produce HelloRequest \
--broker kafka:9092 \
--proto internal/proto/testdata/example.proto \
--topic test \
--header "priority=high" \
--header "application=protokaf" \
--data '{"name": "Alice", "age": 11}'
```
**Produce message with template**
```sh
$ protokaf produce HelloRequest \
--broker kafka:9092 \
--proto internal/proto/testdata/example.proto \
--topic test \
--data '{"name": {{randomFemaleName | quote}}, "age": {{randomNumber 10 20}}}' \
--count 10 \
--seed 42
```
**Produce message with Kafka auth**
```sh
$ protokaf produce HelloRequest \
--broker kafka:9093 \
--kafka-auth-dsn "SCRAM-SHA-512:login:passwd" \
--proto internal/proto/testdata/example.proto \
--topic test \
--data '{"name": "Alice", "age": 11}'
```
**Read data from stdin or flag**
Read message `HelloRequest` from `stdin`, produce to `test` topic
```sh
$ echo '{"name": "Alice", "age": 11}' | protokaf produce HelloRequest -t test
```
Read message `HelloRequest` from `-d` value, produce to `test` topic
```sh
$ protokaf produce HelloRequest -t test -d '{"name": "Alice", "age": 11}'
```
### Template
**Template options**
* `--seed ` You can set number greater then zero to produce the same pseudo-random sequence of messages
* `--count ` Useful for generating messages with random data
* `--concurrency ` Number of message senders to run concurrently for const concurrency producing
**Show all template functions**
```sh
$ protokaf produce --template-functions-print
```
## Build json template by proto file
This can be useful for creating body for produce command
```sh
$ protokaf build HelloRequest --proto internal/proto/testdata/example.proto
```
For proto file
```protobuf
syntax = "proto3";
package example;
message HelloRequest {
enum Status {
PENDING = 0;
COMPLETED = 1;
}
string name = 1;
int32 age = 2;
optional float amount = 4;
Status status = 5;
repeated string keys = 6;
}
```
command will print
```json
{
"name": "",
"age": 0,
"amount": 0,
"status": "PENDING",
"keys": [
""
]
}
```
## Consume
### Help
```sh
$ protokaf help consume
```
### Examples
```sh
$ protokaf consume HelloRequest \
--broker kafka:9092 \
--proto internal/proto/testdata/example.proto \
--group mygroup \
--topic test
```
**Read messages from Kafka `test` topic, use group `mygroup`, print to `stdout`**
```sh
$ protokaf consume HelloRequest -G mygroup -t test
```
**Read the last `10` messages from `test` topic, then exit**
```sh
$ protokaf consume HelloRequest -G mygroup -t test -c 10
```
**Read from offset `5` messages from `test` topic**
```sh
$ protokaf consume HelloRequest -G mygroup -t test -o 5
```
## Testing
### Prepare test environment and running tests
```sh
make docker-dev-up
make kafka-users
make test
make install # optional (you can use 'go run . ')
```