-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-kafka-topics.sh
55 lines (48 loc) · 1.63 KB
/
create-kafka-topics.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
set -x
# Script to create the list of provided topics against the accessible Kafka service.
#
# arguments:
# KAFKA_HOST: Kafka host that runs and exposes Kafka service
# KAFKA_PORT: Kafka exposed port
# CREATE_TOPICS: List of space separated topics to create in the form of topic_name:partitions:replication_factor:cleanup_policy
# Set a trap for SIGINT and SIGTERM signals
trap "echo 'interrupted...' && exit 1" SIGTERM SIGINT
kaf="./kaf"
# wait_for_kafka polls until kafka is running
function wait_for_kafka() {
START_TIMEOUT=60
count=0
step=2
while ! nc -z $KAFKA_HOST $KAFKA_PORT 2>&1; do
echo "waiting for kafka to be ready"
sleep $step
count=$((count + step))
if [ $count -gt $START_TIMEOUT ]; then
echo "timed out waiting for kafka"
exit 1
fi
done
}
# create_topics iterates and triggers creation of all topics using the given parameters (partitions, replication factor and cleanup policy).
function create_topics() {
for topicToCreate in $CREATE_TOPICS; do
if [[ -z ${topicToCreate} ]]; then
:
else
echo "creating topics: $topicToCreate"
IFS=':' read -r -a topicConfig <<<"$topicToCreate"
config=
if [ "${topicConfig[3]}" == "compact" ]; then
config="--compact"
fi
$kaf -b $KAFKA_HOST:$KAFKA_PORT topic create ${topicConfig[0]} -p ${topicConfig[1]} -r ${topicConfig[2]} $config
status=$?
if [[ $status != 0 ]]; then
exit $status
fi
fi
done
}
wait_for_kafka
create_topics