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

后端 未结 13 1877
陌清茗
陌清茗 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:27

    I use the utility below to cleanup after my integration test run.

    It uses the latest AdminZkClient api. The older api has been deprecated.

    import javax.inject.Inject
    import kafka.zk.{AdminZkClient, KafkaZkClient}
    import org.apache.kafka.common.utils.Time
    
    class ZookeeperUtils @Inject() (config: AppConfig) {
    
      val testTopic = "users_1"
    
      val zkHost = config.KafkaConfig.zkHost
      val sessionTimeoutMs = 10 * 1000
      val connectionTimeoutMs = 60 * 1000
      val isSecure = false
      val maxInFlightRequests = 10
      val time: Time = Time.SYSTEM
    
      def cleanupTopic(config: AppConfig) = {
    
        val zkClient = KafkaZkClient.apply(zkHost, isSecure, sessionTimeoutMs, connectionTimeoutMs, maxInFlightRequests, time)
        val zkUtils = new AdminZkClient(zkClient)
    
        val pp = new Properties()
        pp.setProperty("delete.retention.ms", "10")
        pp.setProperty("file.delete.delay.ms", "1000")
        zkUtils.changeTopicConfig(testTopic , pp)
        //    zkUtils.deleteTopic(testTopic)
    
        println("Waiting for topic to be purged. Then reset to retain records for the run")
        Thread.sleep(60000L)
    
        val resetProps = new Properties()
        resetProps.setProperty("delete.retention.ms", "3000000")
        resetProps.setProperty("file.delete.delay.ms", "4000000")
        zkUtils.changeTopicConfig(testTopic , resetProps)
    
      }
    
    
    }
    

    There is an option delete topic. But, it marks the topic for deletion. Zookeeper later deletes the topic. Since this can be unpredictably long, I prefer the retention.ms approach

提交回复
热议问题