apache-kafkakafka-topic

I want to validate if the created topic name is in the specified format like [a-z]*_[1-2]* in kafka when creating in terminal, how to achieve this?


bin/kafka-topics.sh --create --topic topic name --bootstrap-server localhost:9092 in here, I wanted to check if the topic name is in the format -[a-z]_[0-9]. Example: topic name - demo_123


Solution

  • kafka-topics.sh is just a wrapper to the class kafka.admin.TopicCommand. I don't think you can pass any parameter to kafka-topics.sh to achieve what you want, but you can create your own kafka-topics.sh that parses the arguments and checks the topic argument format. Something along these lines:

    Note that if it supports short args or --topic=topic_name you'll need to account for these in the script.

    #!/bin/bash
    
    while :; do
      case $1 in
        --topic)
          if [[ ! $2 =~ ^[a-z]+_[0-9]+$ ]]; then
            echo "Invalid format for topic $2"
            exit 1
          fi
          break
          ;;
        *)
          break
      esac
    done
    
    exec $(dirname $0)/kafka-run-class.sh kafka.admin.TopicCommand "$@"