From a381884d323b67844d0ed7910b37171560a2d9d3 Mon Sep 17 00:00:00 2001 From: Starfall Date: Tue, 2 Jul 2024 17:26:40 -0500 Subject: kafka-send: new python script to produce kafka messages --- kafka-send/README.md | 15 +++++++++++++++ kafka-send/kafka-send.py | 16 ++++++++++++++++ kafka-send/requirements.txt | 3 +++ 3 files changed, 34 insertions(+) create mode 100644 kafka-send/README.md create mode 100755 kafka-send/kafka-send.py create mode 100644 kafka-send/requirements.txt diff --git a/kafka-send/README.md b/kafka-send/README.md new file mode 100644 index 0000000..297046e --- /dev/null +++ b/kafka-send/README.md @@ -0,0 +1,15 @@ +Tiny Python script to send a message on a Kafka topic, because the command line tools couldn't accept as big a message as we needed. + +Setup: +``` +uv venv +source .venv/bin/activate +uv pip sync requirements.txt +``` + +Usage: +``` +kafka-send.py topic path/to/message.json +``` + +If you want to use something other than a local Kafka server, add `-s server.host:9092`. diff --git a/kafka-send/kafka-send.py b/kafka-send/kafka-send.py new file mode 100755 index 0000000..b8ef414 --- /dev/null +++ b/kafka-send/kafka-send.py @@ -0,0 +1,16 @@ +#!/usr/bin/env python3 +import argparse +import kafka + +parser = argparse.ArgumentParser() +parser.add_argument('-s', '--server', default='localhost:9092', help='kafka bootstrap server (default localhost:9092)') +parser.add_argument('topic', help='kafka topic') +parser.add_argument('filename', help='file containing the message to be sent') +args = parser.parse_args() + +with open(args.filename, 'r') as file: + msg = ''.join(file.readlines()).encode() + +producer = kafka.KafkaProducer(bootstrap_servers=args.server) +producer.send(args.topic, msg) +producer.flush() diff --git a/kafka-send/requirements.txt b/kafka-send/requirements.txt new file mode 100644 index 0000000..6d6d501 --- /dev/null +++ b/kafka-send/requirements.txt @@ -0,0 +1,3 @@ +# This file was autogenerated by uv via the following command: +# uv pip compile - -o requirements.txt +kafka-python-ng>=2.0.0 -- cgit