Search in sources :

Example 16 with KeyFormat

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);
    }
}
Also used : Iterables(com.google.common.collect.Iterables) LoadingCache(com.google.common.cache.LoadingCache) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) FormatFactory(io.confluent.ksql.serde.FormatFactory) KeyFormat(io.confluent.ksql.serde.KeyFormat) QuerySchemas(io.confluent.ksql.schema.query.QuerySchemas) Function(java.util.function.Function) WindowInfo(io.confluent.ksql.serde.WindowInfo) OptionalLong(java.util.OptionalLong) SchemaInfo(io.confluent.ksql.schema.query.QuerySchemas.SchemaInfo) Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) Duration(java.time.Duration) WindowType(io.confluent.ksql.model.WindowType) QueryId(io.confluent.ksql.query.QueryId) SerdeFeatures(io.confluent.ksql.serde.SerdeFeatures) Deserializer(org.apache.kafka.common.serialization.Deserializer) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) TestFrameworkException(io.confluent.ksql.test.TestFrameworkException) TimeWindowedDeserializer(org.apache.kafka.streams.kstream.TimeWindowedDeserializer) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) DurationParser(io.confluent.ksql.parser.DurationParser) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) ValueFormat(io.confluent.ksql.serde.ValueFormat) Collectors(java.util.stream.Collectors) AbstractKafkaSchemaSerDeConfig(io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) SerdeSupplier(io.confluent.ksql.test.serde.SerdeSupplier) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Serializer(org.apache.kafka.common.serialization.Serializer) Optional(java.util.Optional) SerdeUtil(io.confluent.ksql.test.utils.SerdeUtil) CacheBuilder(com.google.common.cache.CacheBuilder) Pattern(java.util.regex.Pattern) FormatInfo(io.confluent.ksql.serde.FormatInfo) ValueFormat(io.confluent.ksql.serde.ValueFormat) Optional(java.util.Optional) QueryId(io.confluent.ksql.query.QueryId) KeyFormat(io.confluent.ksql.serde.KeyFormat) TestFrameworkException(io.confluent.ksql.test.TestFrameworkException) QuerySchemas(io.confluent.ksql.schema.query.QuerySchemas) TestFrameworkException(io.confluent.ksql.test.TestFrameworkException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Aggregations

KeyFormat (io.confluent.ksql.serde.KeyFormat)16 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)7 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)5 KsqlException (io.confluent.ksql.util.KsqlException)5 DataSource (io.confluent.ksql.metastore.model.DataSource)4 FormatInfo (io.confluent.ksql.serde.FormatInfo)4 ValueFormat (io.confluent.ksql.serde.ValueFormat)4 KsqlConfig (io.confluent.ksql.util.KsqlConfig)3 Optional (java.util.Optional)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 GenericKey (io.confluent.ksql.GenericKey)2 Stacker (io.confluent.ksql.execution.context.QueryContext.Stacker)2 ExecutionStep (io.confluent.ksql.execution.plan.ExecutionStep)2 KStreamHolder (io.confluent.ksql.execution.plan.KStreamHolder)2 KTableHolder (io.confluent.ksql.execution.plan.KTableHolder)2 ExecutionStepFactory (io.confluent.ksql.execution.streams.ExecutionStepFactory)2 ColumnName (io.confluent.ksql.name.ColumnName)2 QueryId (io.confluent.ksql.query.QueryId)2 InternalFormats (io.confluent.ksql.serde.InternalFormats)2 WindowInfo (io.confluent.ksql.serde.WindowInfo)2