我有一个生产者,可以提供用于测试Web应用程序和使用者的数据。为此,我想问是否有办法在将主题发送给生产者之前将其删除?当前,当我在计算机上使用VM来测试系统时,可以通过CMD。生产者程序有什么办法做到这一点。 (Java)
答案 0 :(得分:0)
您可以包含一些代码,这些代码可以在Producer实现中删除主题(可能在最开始的时候)。
这里是一个example,它使用kafka.admin.AdminUtils.deleteTopic()
/**
* Deletes the named topics
* @param topicNames The names of the topics to delete
* @return A set of the names of the topics that were successfully deleted
*/
public String[] deleteTopics(final String...topicNames) {
if(!connected.get()) throw new IllegalStateException("The KafkaTestServer is not running");
if(topicNames==null || topicNames.length==0) return new String[0];
final Set<String> deleted = new LinkedHashSet<String>();
for(String topicName: topicNames) {
if(topicName==null || topicName.trim().isEmpty()) {
try {
AdminUtils.deleteTopic(zkUtils, topicName.trim());
deleted.add(topicName.trim());
} catch (Exception ex) {
log.warn("Failed to delete topic [" + topicName.trim() + "]", ex);
}
}
}
return deleted.toArray(new String[deleted.size()]);
}