use of org.apache.kafka.clients.admin.NewTopic in project zipkin by openzipkin.
the class KafkaExtension method prepareTopics.
void prepareTopics(String topics, int partitions) {
Properties config = new Properties();
config.put(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServer());
List<NewTopic> newTopics = new ArrayList<>();
for (String topic : topics.split(",")) {
if ("".equals(topic))
continue;
newTopics.add(new NewTopic(topic, partitions, (short) 1));
}
try (AdminClient adminClient = AdminClient.create(config)) {
adminClient.createTopics(newTopics).all().get();
} catch (InterruptedException | ExecutionException e) {
if (e.getCause() != null && e.getCause() instanceof TopicExistsException)
return;
throw new TestAbortedException("Topics could not be created " + newTopics + ": " + e.getMessage(), e);
}
}
use of org.apache.kafka.clients.admin.NewTopic in project hono by eclipse.
the class KafkaBasedInternalCommandConsumer method createTopic.
private Future<Void> createTopic() {
final Promise<Void> promise = Promise.promise();
final String topicName = getTopicName();
// create topic with unspecified replication factor - broker "default.replication.factor" should be used
final NewTopic newTopic = new NewTopic(topicName, Optional.of(NUM_PARTITIONS), Optional.empty());
adminClient.createTopics(List.of(newTopic)).all().whenComplete((v, ex) -> {
context.runOnContext(v1 -> Optional.ofNullable(ex).filter(e -> !(e instanceof TopicExistsException)).ifPresentOrElse(promise::fail, promise::complete));
});
return promise.future().onSuccess(v -> LOG.debug("created topic [{}]", topicName)).onFailure(thr -> LOG.error("error creating topic [{}]", topicName, thr));
}
use of org.apache.kafka.clients.admin.NewTopic in project thingsboard by thingsboard.
the class TbKafkaAdmin method createTopicIfNotExists.
@Override
public void createTopicIfNotExists(String topic) {
if (topics.contains(topic)) {
return;
}
try {
NewTopic newTopic = new NewTopic(topic, numPartitions, replicationFactor).configs(topicConfigs);
createTopic(newTopic).values().get(topic).get();
topics.add(topic);
} catch (ExecutionException ee) {
if (ee.getCause() instanceof TopicExistsException) {
// do nothing
} else {
log.warn("[{}] Failed to create topic", topic, ee);
throw new RuntimeException(ee);
}
} catch (Exception e) {
log.warn("[{}] Failed to create topic", topic, e);
throw new RuntimeException(e);
}
}
use of org.apache.kafka.clients.admin.NewTopic in project cruise-control by linkedin.
the class LoadMonitorTaskRunnerTest method setUp.
/**
* Setup the test.
*/
@Before
public void setUp() {
super.setUp();
Properties adminProps = new Properties();
adminProps.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers());
AdminClient adminClient = AdminClient.create(adminProps);
Set<NewTopic> newTopics = new HashSet<>();
for (int i = 0; i < NUM_TOPICS; i++) {
newTopics.add(new NewTopic(TOPIC_PREFIX + i, NUM_PARTITIONS, (short) 1));
}
CreateTopicsResult createTopicsResult = adminClient.createTopics(newTopics);
AtomicInteger adminFailed = new AtomicInteger(0);
createTopicsResult.all().whenComplete((v, e) -> {
if (e != null) {
adminFailed.incrementAndGet();
}
});
assertEquals(0, adminFailed.get());
}
use of org.apache.kafka.clients.admin.NewTopic in project cruise-control by linkedin.
the class KafkaCruiseControlUtils method wrapTopic.
/**
* Build a wrapper around the topic with the given desired properties and {@link #DEFAULT_CLEANUP_POLICY}.
*
* @param topic The name of the topic.
* @param partitionCount Desired partition count.
* @param replicationFactor Desired replication factor.
* @param retentionMs Desired retention in milliseconds.
* @return A wrapper around the topic with the given desired properties.
*/
public static NewTopic wrapTopic(String topic, int partitionCount, short replicationFactor, long retentionMs) {
if (partitionCount <= 0 || replicationFactor <= 0 || retentionMs <= 0) {
throw new IllegalArgumentException(String.format("Partition count (%d), replication factor (%d), and retention ms (%d)" + " must be positive for the topic (%s).", partitionCount, replicationFactor, retentionMs, topic));
}
NewTopic newTopic = new NewTopic(topic, partitionCount, replicationFactor);
Map<String, String> config = new HashMap<>();
config.put(RetentionMsProp(), Long.toString(retentionMs));
config.put(CleanupPolicyProp(), DEFAULT_CLEANUP_POLICY);
newTopic.configs(config);
return newTopic;
}
Aggregations