use of org.apache.kafka.common.requests.CreateTopicsResponse in project kafka by apache.
the class StreamsKafkaClient method createTopics.
/**
* Create a set of new topics using batch request.
*/
public void createTopics(final Map<InternalTopicConfig, Integer> topicsMap, final int replicationFactor, final long windowChangeLogAdditionalRetention, final MetadataResponse metadata) {
final Map<String, CreateTopicsRequest.TopicDetails> topicRequestDetails = new HashMap<>();
for (Map.Entry<InternalTopicConfig, Integer> entry : topicsMap.entrySet()) {
InternalTopicConfig internalTopicConfig = entry.getKey();
Integer partitions = entry.getValue();
final Properties topicProperties = internalTopicConfig.toProperties(windowChangeLogAdditionalRetention);
final Map<String, String> topicConfig = new HashMap<>();
for (String key : topicProperties.stringPropertyNames()) {
topicConfig.put(key, topicProperties.getProperty(key));
}
final CreateTopicsRequest.TopicDetails topicDetails = new CreateTopicsRequest.TopicDetails(partitions, (short) replicationFactor, topicConfig);
topicRequestDetails.put(internalTopicConfig.name(), topicDetails);
}
final ClientRequest clientRequest = kafkaClient.newClientRequest(getControllerReadyBrokerId(metadata), new CreateTopicsRequest.Builder(topicRequestDetails, streamsConfig.getInt(StreamsConfig.REQUEST_TIMEOUT_MS_CONFIG)), Time.SYSTEM.milliseconds(), true);
final ClientResponse clientResponse = sendRequest(clientRequest);
if (!clientResponse.hasResponse()) {
throw new StreamsException("Empty response for client request.");
}
if (!(clientResponse.responseBody() instanceof CreateTopicsResponse)) {
throw new StreamsException("Inconsistent response type for internal topic creation request. " + "Expected CreateTopicsResponse but received " + clientResponse.responseBody().getClass().getName());
}
final CreateTopicsResponse createTopicsResponse = (CreateTopicsResponse) clientResponse.responseBody();
for (InternalTopicConfig internalTopicConfig : topicsMap.keySet()) {
CreateTopicsResponse.Error error = createTopicsResponse.errors().get(internalTopicConfig.name());
if (!error.is(Errors.NONE) && !error.is(Errors.TOPIC_ALREADY_EXISTS)) {
throw new StreamsException("Could not create topic: " + internalTopicConfig.name() + " due to " + error.messageWithFallback());
}
}
}
Aggregations