use of io.confluent.ksql.serde.KeyFormat in project ksql by confluentinc.
the class TopicInfoCache method load.
private TopicInfo load(final String topicName) {
try {
final Optional<InternalTopic> internalTopic = INTERNAL_TOPIC_PATTERNS.stream().map(e -> e.match(topicName)).filter(Optional::isPresent).map(Optional::get).findFirst();
if (internalTopic.isPresent()) {
// Internal topic:
final QueryId queryId = internalTopic.get().queryId();
final PersistentQueryMetadata query = ksqlEngine.getPersistentQuery(queryId).orElseThrow(() -> new TestFrameworkException("Unknown queryId for internal topic: " + queryId));
final QuerySchemas.MultiSchemaInfo schemasInfo = query.getQuerySchemas().getTopicInfo(topicName);
final Set<KeyFormat> keyFormats = schemasInfo.getKeyFormats();
final Set<ValueFormat> valueFormats = schemasInfo.getValueFormats();
// foreign key joins once the above github issue is implemented.
if (keyFormats.size() != 1) {
final String result = keyFormats.size() == 0 ? "Zero" : "Multiple";
throw new Exception(result + " key formats registered for topic." + System.lineSeparator() + "topic: " + topicName + "formats: " + keyFormats.stream().map(KeyFormat::getFormat).sorted().collect(Collectors.toList()));
}
// for stream-stream left/outer joins once the above github issue is implemented.
if (valueFormats.size() > 1) {
throw new Exception("Multiple value formats registered for topic." + System.lineSeparator() + "topic: " + topicName + "formats: " + valueFormats.stream().map(ValueFormat::getFormat).sorted().collect(Collectors.toList()));
}
return new TopicInfo(topicName, query.getLogicalSchema(), internalTopic.get().keyFormat(Iterables.getOnlyElement(keyFormats), query), Iterables.getOnlyElement(valueFormats, NONE_VALUE_FORMAT), internalTopic.get().changeLogWindowSize(query));
}
// Source / sink topic:
final Set<TopicInfo> keyTypes = ksqlEngine.getMetaStore().getAllDataSources().values().stream().filter(source -> source.getKafkaTopicName().equals(topicName)).map(source -> new TopicInfo(topicName, source.getSchema(), source.getKsqlTopic().getKeyFormat(), source.getKsqlTopic().getValueFormat(), OptionalLong.empty())).collect(Collectors.toSet());
if (keyTypes.isEmpty()) {
throw new TestFrameworkException("No information found for topic '" + topicName + "'. Available topics: " + cache.asMap().keySet());
}
return Iterables.get(keyTypes, 0);
} catch (final Exception e) {
throw new TestFrameworkException("Failed to determine key type for" + System.lineSeparator() + "topic: " + topicName + System.lineSeparator() + "reason: " + e.getMessage(), e);
}
}
Aggregations