kafka.common.TopicExistsException Java Examples

The following examples show how to use kafka.common.TopicExistsException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaComponent.java    From metron with Apache License 2.0 6 votes vote down vote up
public void createTopic(String name, int numPartitions, long waitThisLongForMetadataToPropagate) throws InterruptedException {
  ZkUtils zkUtils = null;
  Level oldLevel = UnitTestHelper.getJavaLoggingLevel();
  try {
    UnitTestHelper.setJavaLoggingLevel(Level.OFF);
    zkUtils = ZkUtils.apply(zookeeperConnectString, 30000, 30000, false);
    AdminUtilsWrapper.createTopic(zkUtils, name, numPartitions, 1, new Properties());
    if (waitThisLongForMetadataToPropagate > 0) {
      waitUntilMetadataIsPropagated(name, numPartitions, waitThisLongForMetadataToPropagate);
    }
  }catch(TopicExistsException tee) {
  }finally {
    if(zkUtils != null){
      zkUtils.close();
    }
    UnitTestHelper.setJavaLoggingLevel(oldLevel);
  }
}
 
Example #2
Source File: KafkaTestUtil.java    From AthenaX with Apache License 2.0 5 votes vote down vote up
public static boolean createKafkaTopicIfNecessary(String brokerUri, int replFactor, int numPartitions, String topic)
    throws IOException {
  URI zkUri = URI.create(brokerUri);
  Preconditions.checkArgument("zk".equals(zkUri.getScheme()));
  String zkServerList = zkUri.getAuthority() + zkUri.getPath();

  ZkUtils zkUtils = ZkUtils.apply(zkServerList, ZK_SESSION_TIMEOUT_MS,
      ZK_CONNECTION_TIMEOUT_MS, JaasUtils.isZkSecurityEnabled());
  try {
    if (AdminUtils.topicExists(zkUtils, topic)) {
      return false;
    }

    try {
      AdminUtils.createTopic(zkUtils, topic, numPartitions, replFactor, new Properties());
    } catch (TopicExistsException ignored) {
      return false;
    } catch (RuntimeException e) {
      throw new IOException(e);
    }
  } finally {
    if (zkUtils != null) {
      zkUtils.close();
    }
  }
  return true;
}
 
Example #3
Source File: KafkaTestUtil.java    From siddhi-io-kafka with Apache License 2.0 5 votes vote down vote up
public static void createTopic(String connectionString, String topics[], int numOfPartitions) {
    ZkClient zkClient = new ZkClient(connectionString, 30000, 30000, ZKStringSerializer$.MODULE$);
    ZkConnection zkConnection = new ZkConnection(connectionString);
    ZkUtils zkUtils = new ZkUtils(zkClient, zkConnection, false);
    for (String topic : topics) {
        try {
            AdminUtils.createTopic(zkUtils, topic, numOfPartitions, 1, new Properties(),
                    RackAwareMode.Enforced$.MODULE$);
        } catch (TopicExistsException e) {
            log.warn("topic exists for: " + topic);
        }
    }
    zkClient.close();
}