Is there a way to delete all the data from a topic or delete the topic before every run?

后端 未结 13 1873
陌清茗
陌清茗 2020-12-12 10:33

Is there a way to delete all the data from a topic or delete the topic before every run?

Can I modify the KafkaConfig.scala file to change the logRetentionHour

相关标签:
13条回答
  • 2020-12-12 11:11

    Below are scripts for emptying and deleting a Kafka topic assuming localhost as the zookeeper server and Kafka_Home is set to the install directory:

    The script below will empty a topic by setting its retention time to 1 second and then removing the configuration:

    #!/bin/bash
    echo "Enter name of topic to empty:"
    read topicName
    /$Kafka_Home/bin/kafka-configs --zookeeper localhost:2181 --alter --entity-type topics --entity-name $topicName --add-config retention.ms=1000
    sleep 5
    /$Kafka_Home/bin/kafka-configs --zookeeper localhost:2181 --alter --entity-type topics --entity-name $topicName --delete-config retention.ms
    

    To fully delete topics you must stop any applicable kafka broker(s) and remove it's directory(s) from the kafka log dir (default: /tmp/kafka-logs) and then run this script to remove the topic from zookeeper. To verify it's been deleted from zookeeper the output of ls /brokers/topics should no longer include the topic:

    #!/bin/bash
    echo "Enter name of topic to delete from zookeeper:"
    read topicName
    /$Kafka_Home/bin/zookeeper-shell localhost:2181 <<EOF
    rmr /brokers/topics/$topicName
    ls /brokers/topics
    quit
    EOF
    
    0 讨论(0)
  • 2020-12-12 11:16
    1. Stop ZooKeeper and Kafka
    2. In server.properties, change log.retention.hours value. You can comment log.retention.hours and add log.retention.ms=1000. It would keep the record on Kafka Topic for only one second.
    3. Start zookeeper and kafka.
    4. Check on consumer console. When I opened the console for the first time, record was there. But when I opened the console again, the record was removed.
    5. Later on, you can set the value of log.retention.hours to your desired figure.
    0 讨论(0)
  • 2020-12-12 11:17

    We tried pretty much what the other answers are describing with moderate level of success. What really worked for us (Apache Kafka 0.8.1) is the class command

    sh kafka-run-class.sh kafka.admin.DeleteTopicCommand --topic yourtopic --zookeeper localhost:2181

    0 讨论(0)
  • 2020-12-12 11:21

    For brew users

    If you're using brew like me and wasted a lot of time searching for the infamous kafka-logs folder, fear no more. (and please do let me know if that works for you and multiple different versions of Homebrew, Kafka etc :) )

    You're probably going to find it under:

    Location:

    /usr/local/var/lib/kafka-logs


    How to actually find that path

    (this is also helpful for basically every app you install through brew)

    1) brew services list

    kafka started matbhz /Users/matbhz/Library/LaunchAgents/homebrew.mxcl.kafka.plist

    2) Open and read that plist you found above

    3) Find the line defining server.properties location open it, in my case:

    • /usr/local/etc/kafka/server.properties

    4) Look for the log.dirs line:

    log.dirs=/usr/local/var/lib/kafka-logs

    5) Go to that location and delete the logs for the topics you wish

    6) Restart Kafka with brew services restart kafka

    0 讨论(0)
  • 2020-12-12 11:24

    I use this script:

    #!/bin/bash
    topics=`kafka-topics --list --zookeeper zookeeper:2181`
    for t in $topics; do 
        for p in retention.ms retention.bytes segment.ms segment.bytes; do
            kafka-topics --zookeeper zookeeper:2181 --alter --topic $t --config ${p}=100
        done
    done
    sleep 60
    for t in $topics; do 
        for p in retention.ms retention.bytes segment.ms segment.bytes; do
            kafka-topics --zookeeper zookeeper:2181 --alter --topic $t --delete-config ${p}
        done
    done
    
    0 讨论(0)
  • 2020-12-12 11:25

    As I mentioned here Purge Kafka Queue:

    Tested in Kafka 0.8.2, for the quick-start example: First, Add one line to server.properties file under config folder:

    delete.topic.enable=true
    

    then, you can run this command:

    bin/kafka-topics.sh --zookeeper localhost:2181 --delete --topic test
    
    0 讨论(0)
提交回复
热议问题