use of org.apache.kafka.clients.admin.ConfigEntry in project strimzi by strimzi.
the class TopicSerialization method toTopicConfig.
/**
* Return a singleton map from the topic {@link ConfigResource} for the given topic,
* to the {@link Config} of the given topic.
*/
public static Map<ConfigResource, Config> toTopicConfig(Topic topic) {
Set<ConfigEntry> configEntries = new HashSet<>();
for (Map.Entry<String, String> entry : topic.getConfig().entrySet()) {
configEntries.add(new ConfigEntry(entry.getKey(), entry.getValue()));
}
Config config = new Config(configEntries);
return Collections.singletonMap(new ConfigResource(ConfigResource.Type.TOPIC, topic.getTopicName().toString()), config);
}
use of org.apache.kafka.clients.admin.ConfigEntry in project ksql by confluentinc.
the class KafkaTopicClientImplTest method withResourceConfig.
/*
* Config has broken hashCode & equals method:
* https://issues.apache.org/jira/browse/KAFKA-6727
*/
private static Map<ConfigResource, Config> withResourceConfig(final ConfigResource resource, final ConfigEntry... entries) {
final Set<ConfigEntry> expected = Arrays.stream(entries).collect(Collectors.toSet());
class ConfigMatcher implements IArgumentMatcher {
@SuppressWarnings("unchecked")
@Override
public boolean matches(final Object argument) {
final Map<ConfigResource, Config> request = (Map<ConfigResource, Config>) argument;
if (request.size() != 1) {
return false;
}
final Config config = request.get(resource);
if (config == null) {
return false;
}
final Set<ConfigEntry> actual = new HashSet<>(config.entries());
return actual.equals(expected);
}
@Override
public void appendTo(final StringBuffer buffer) {
buffer.append(resource).append("->").append("Config{").append(expected).append("}");
}
}
EasyMock.reportMatcher(new ConfigMatcher());
return null;
}
use of org.apache.kafka.clients.admin.ConfigEntry in project ksql by confluentinc.
the class KafkaTopicClientImplTest method describeBrokerResult.
private DescribeConfigsResult describeBrokerResult() {
DescribeConfigsResult describeConfigsResult = mock(DescribeConfigsResult.class);
ConfigEntry configEntryDeleteEnable = new ConfigEntry("delete.topic.enable", "true");
List<ConfigEntry> configEntries = new ArrayList<>();
configEntries.add(configEntryDeleteEnable);
Map<ConfigResource, Config> config = ImmutableMap.of(new ConfigResource(ConfigResource.Type.BROKER, node.idString()), new Config(configEntries));
expect(describeConfigsResult.all()).andReturn(KafkaFuture.completedFuture(config));
replay(describeConfigsResult);
return describeConfigsResult;
}
use of org.apache.kafka.clients.admin.ConfigEntry in project ksql by confluentinc.
the class KafkaTopicClientImpl method addTopicConfig.
@Override
public boolean addTopicConfig(final String topicName, final Map<String, ?> overrides) {
final ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName);
try {
final Map<String, String> existingConfig = topicConfig(topicName, false);
final boolean changed = overrides.entrySet().stream().anyMatch(e -> !Objects.equals(existingConfig.get(e.getKey()), e.getValue()));
if (!changed) {
return false;
}
existingConfig.putAll(toStringConfigs(overrides));
final Set<ConfigEntry> entries = existingConfig.entrySet().stream().map(e -> new ConfigEntry(e.getKey(), e.getValue())).collect(Collectors.toSet());
final Map<ConfigResource, Config> request = Collections.singletonMap(resource, new Config(entries));
executeWithRetries(() -> adminClient.alterConfigs(request).all());
return true;
} catch (final Exception e) {
throw new KafkaResponseGetFailedException("Failed to set config for Kafka Topic " + topicName, e);
}
}
use of org.apache.kafka.clients.admin.ConfigEntry in project ksql by confluentinc.
the class KafkaTopicClientImpl method topicConfig.
private Map<String, String> topicConfig(final String topicName, final boolean includeDefaults) {
final ConfigResource resource = new ConfigResource(ConfigResource.Type.TOPIC, topicName);
final List<ConfigResource> request = Collections.singletonList(resource);
try {
final Config config = executeWithRetries(() -> adminClient.describeConfigs(request).all()).get(resource);
return config.entries().stream().filter(e -> includeDefaults || e.source().equals(ConfigEntry.ConfigSource.DYNAMIC_TOPIC_CONFIG)).collect(Collectors.toMap(ConfigEntry::name, ConfigEntry::value));
} catch (final Exception e) {
throw new KafkaResponseGetFailedException("Failed to get config for Kafka Topic " + topicName, e);
}
}
Aggregations