Skip to main content

Topic names must be regex-escaped when using kafka-topics commands

·204 words·1 min· loading · loading · ·
kafka cli regex troubleshooting tips
Luke Taylor
Author
Luke Taylor
I like a lot of things

Overview
#

Per the kafka-topics --help output, the string provided to the --topic param is treated as a regex pattern by default.

As a result, the topic name must be regex-escaped before executing the command to avoid unintended consequences when performing alter, describe, or (most importantly) delete commands.

--topic <String: topic>                  The topic to create, alter, describe
                                           or delete. It also accepts a regular
                                           expression, except for --create
                                           option. Put topic name in double
                                           quotes and use the '\' prefix to
                                           escape regular expression symbols; e.
                                           g. "test\.topic".

Fix
#

Valid characters for topic names are a-z, A-Z, 0-9, . (dot), _ (underscore), and - (dash), although users should choose either underscored or dots, but not both due to how Metrics are handled.

Although the only special character allowed in topic names that is also a reserved character for regex is the dot, a general regex escape will be performed on the topic name. And the resulting escaped string is what should be used when performing operations using the kafka-topics CLI command.

Example
#

export MY_COMMAND_CONFIG="/path/to/config.conf"
export MY_BOOTSTRAP_URL="my.kafka:9092"
export TARGET_TOPIC_NAME="my-SPECIAL_topic.v1"
export TARGET_TOPIC_NAME_ESCAPED="$(printf '%s' "$TARGET_TOPIC_NAME" | sed 's/[.[\(*^$+?{|]/\\&/g')"
echo $TARGET_TOPIC_NAME_ESCAPED
# OUTPUTS my-SPECIAL_topic\.v1
kafka-topics \
    --delete \
    --topic $TARGET_TOPIC_NAME_ESCAPED \
    --bootstrap-server $MY_BOOTSTRAP_URL \
    --command-config $MY_COMMAND_CONFIG