Search in sources :

Example 1 with KeyFormat

use of io.confluent.ksql.serde.KeyFormat in project ksql by confluentinc.

the class DataSourceNodeTest method givenWindowedSource.

private void givenWindowedSource(final boolean windowed) {
    final FormatInfo format = FormatInfo.of(FormatFactory.KAFKA.name());
    final KeyFormat keyFormat = windowed ? KeyFormat.windowed(format, SerdeFeatures.of(), WindowInfo.of(WindowType.SESSION, Optional.empty())) : KeyFormat.nonWindowed(format, SerdeFeatures.of());
    when(topic.getKeyFormat()).thenReturn(keyFormat);
}
Also used : FormatInfo(io.confluent.ksql.serde.FormatInfo) KeyFormat(io.confluent.ksql.serde.KeyFormat)

Example 2 with KeyFormat

use of io.confluent.ksql.serde.KeyFormat in project ksql by confluentinc.

the class SchemaKGroupedStream method aggregate.

@SuppressWarnings("unchecked")
public SchemaKTable<?> aggregate(final List<ColumnName> nonAggregateColumns, final List<FunctionCall> aggregations, final Optional<WindowExpression> windowExpression, final FormatInfo valueFormat, final Stacker contextStacker) {
    final ExecutionStep<? extends KTableHolder<?>> step;
    final KeyFormat keyFormat;
    if (windowExpression.isPresent()) {
        keyFormat = getKeyFormat(windowExpression.get());
        step = ExecutionStepFactory.streamWindowedAggregate(contextStacker, sourceStep, InternalFormats.of(keyFormat, valueFormat), nonAggregateColumns, aggregations, windowExpression.get().getKsqlWindowExpression());
    } else {
        keyFormat = SerdeFeaturesFactory.sanitizeKeyFormat(this.keyFormat, toSqlTypes(schema.key()), false);
        step = ExecutionStepFactory.streamAggregate(contextStacker, sourceStep, InternalFormats.of(keyFormat, valueFormat), nonAggregateColumns, aggregations);
    }
    return new SchemaKTable(step, resolveSchema(step), keyFormat, ksqlConfig, functionRegistry);
}
Also used : KeyFormat(io.confluent.ksql.serde.KeyFormat)

Example 3 with KeyFormat

use of io.confluent.ksql.serde.KeyFormat in project ksql by confluentinc.

the class QueryBuilder method buildPersistentQueryInSharedRuntime.

@SuppressWarnings("ParameterNumber")
PersistentQueryMetadata buildPersistentQueryInSharedRuntime(final KsqlConfig ksqlConfig, final KsqlConstants.PersistentQueryType persistentQueryType, final String statementText, final QueryId queryId, final Optional<DataSource> sinkDataSource, final Set<DataSource> sources, final ExecutionStep<?> physicalPlan, final String planSummary, final QueryMetadata.Listener listener, final Supplier<List<PersistentQueryMetadata>> allPersistentQueries, final String applicationId, final MetricCollectors metricCollectors) {
    final SharedKafkaStreamsRuntime sharedKafkaStreamsRuntime = getKafkaStreamsInstance(applicationId, sources.stream().map(DataSource::getName).collect(Collectors.toSet()), queryId, metricCollectors);
    final Map<String, Object> queryOverrides = sharedKafkaStreamsRuntime.getStreamProperties();
    final LogicalSchema logicalSchema;
    final KeyFormat keyFormat;
    final ValueFormat valueFormat;
    final KsqlTopic ksqlTopic;
    switch(persistentQueryType) {
        // CREATE_SOURCE does not have a sink, so the schema is obtained from the query source
        case CREATE_SOURCE:
            final DataSource dataSource = Iterables.getOnlyElement(sources);
            logicalSchema = dataSource.getSchema();
            keyFormat = dataSource.getKsqlTopic().getKeyFormat();
            valueFormat = dataSource.getKsqlTopic().getValueFormat();
            ksqlTopic = dataSource.getKsqlTopic();
            break;
        default:
            logicalSchema = sinkDataSource.get().getSchema();
            keyFormat = sinkDataSource.get().getKsqlTopic().getKeyFormat();
            valueFormat = sinkDataSource.get().getKsqlTopic().getValueFormat();
            ksqlTopic = sinkDataSource.get().getKsqlTopic();
            break;
    }
    final PhysicalSchema querySchema = PhysicalSchema.from(logicalSchema, keyFormat.getFeatures(), valueFormat.getFeatures());
    final NamedTopologyBuilder namedTopologyBuilder = sharedKafkaStreamsRuntime.getKafkaStreams().newNamedTopologyBuilder(queryId.toString(), PropertiesUtil.asProperties(queryOverrides));
    final RuntimeBuildContext runtimeBuildContext = buildContext(applicationId, queryId, namedTopologyBuilder);
    final Object result = buildQueryImplementation(physicalPlan, runtimeBuildContext);
    final NamedTopology topology = namedTopologyBuilder.build();
    final Optional<MaterializationProviderBuilderFactory.MaterializationProviderBuilder> materializationProviderBuilder = getMaterializationInfo(result).map(info -> materializationProviderBuilderFactory.materializationProviderBuilder(info, querySchema, keyFormat, queryOverrides, applicationId, queryId.toString()));
    final Optional<ScalablePushRegistry> scalablePushRegistry = applyScalablePushProcessor(querySchema.logicalSchema(), result, allPersistentQueries, queryOverrides, applicationId, ksqlConfig, ksqlTopic, serviceContext);
    final BinPackedPersistentQueryMetadataImpl binPackedPersistentQueryMetadata = new BinPackedPersistentQueryMetadataImpl(persistentQueryType, statementText, querySchema, sources.stream().map(DataSource::getName).collect(Collectors.toSet()), planSummary, applicationId, topology, sharedKafkaStreamsRuntime, runtimeBuildContext.getSchemas(), config.getOverrides(), queryId, materializationProviderBuilder, physicalPlan, getUncaughtExceptionProcessingLogger(queryId), sinkDataSource, listener, queryOverrides, scalablePushRegistry, (streamsRuntime) -> getNamedTopology(streamsRuntime, queryId, applicationId, queryOverrides, physicalPlan));
    if (real) {
        return binPackedPersistentQueryMetadata;
    } else {
        return SandboxedBinPackedPersistentQueryMetadataImpl.of(binPackedPersistentQueryMetadata, listener);
    }
}
Also used : ValueFormat(io.confluent.ksql.serde.ValueFormat) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) KeyFormat(io.confluent.ksql.serde.KeyFormat) DataSource(io.confluent.ksql.metastore.model.DataSource) ScalablePushRegistry(io.confluent.ksql.physical.scalablepush.ScalablePushRegistry) SharedKafkaStreamsRuntime(io.confluent.ksql.util.SharedKafkaStreamsRuntime) RuntimeBuildContext(io.confluent.ksql.execution.runtime.RuntimeBuildContext) PhysicalSchema(io.confluent.ksql.schema.ksql.PhysicalSchema) NamedTopologyBuilder(org.apache.kafka.streams.processor.internals.namedtopology.NamedTopologyBuilder) NamedTopology(org.apache.kafka.streams.processor.internals.namedtopology.NamedTopology) KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic) BinPackedPersistentQueryMetadataImpl(io.confluent.ksql.util.BinPackedPersistentQueryMetadataImpl) SandboxedBinPackedPersistentQueryMetadataImpl(io.confluent.ksql.util.SandboxedBinPackedPersistentQueryMetadataImpl)

Example 4 with KeyFormat

use of io.confluent.ksql.serde.KeyFormat in project ksql by confluentinc.

the class KsqlAuthorizationValidatorImpl method getCreateAsSelectSinkTopic.

private KsqlTopic getCreateAsSelectSinkTopic(final MetaStore metaStore, final CreateAsSelect createAsSelect) {
    final CreateSourceAsProperties properties = createAsSelect.getProperties();
    final String sinkTopicName;
    final KeyFormat sinkKeyFormat;
    final ValueFormat sinkValueFormat;
    if (!properties.getKafkaTopic().isPresent()) {
        final DataSource dataSource = metaStore.getSource(createAsSelect.getName());
        if (dataSource != null) {
            sinkTopicName = dataSource.getKafkaTopicName();
            sinkKeyFormat = dataSource.getKsqlTopic().getKeyFormat();
            sinkValueFormat = dataSource.getKsqlTopic().getValueFormat();
        } else {
            throw new KsqlException("Cannot validate for topic access from an unknown stream/table: " + createAsSelect.getName());
        }
    } else {
        sinkTopicName = properties.getKafkaTopic().get();
        // If no format is specified for the sink topic, then use the format from the primary
        // source topic.
        final SourceTopicsExtractor extractor = new SourceTopicsExtractor(metaStore);
        extractor.process(createAsSelect.getQuery(), null);
        final KsqlTopic primaryKsqlTopic = extractor.getPrimarySourceTopic();
        final Optional<Format> keyFormat = properties.getKeyFormat().map(formatName -> FormatFactory.fromName(formatName));
        final Optional<Format> valueFormat = properties.getValueFormat().map(formatName -> FormatFactory.fromName(formatName));
        sinkKeyFormat = keyFormat.map(format -> KeyFormat.of(FormatInfo.of(format.name()), format.supportsFeature(SerdeFeature.SCHEMA_INFERENCE) ? SerdeFeatures.of(SerdeFeature.SCHEMA_INFERENCE) : SerdeFeatures.of(), Optional.empty())).orElse(primaryKsqlTopic.getKeyFormat());
        sinkValueFormat = valueFormat.map(format -> ValueFormat.of(FormatInfo.of(format.name()), format.supportsFeature(SerdeFeature.SCHEMA_INFERENCE) ? SerdeFeatures.of(SerdeFeature.SCHEMA_INFERENCE) : SerdeFeatures.of())).orElse(primaryKsqlTopic.getValueFormat());
    }
    return new KsqlTopic(sinkTopicName, sinkKeyFormat, sinkValueFormat);
}
Also used : ValueFormat(io.confluent.ksql.serde.ValueFormat) KeyFormat(io.confluent.ksql.serde.KeyFormat) ValueFormat(io.confluent.ksql.serde.ValueFormat) Format(io.confluent.ksql.serde.Format) CreateSourceAsProperties(io.confluent.ksql.parser.properties.with.CreateSourceAsProperties) SourceTopicsExtractor(io.confluent.ksql.topic.SourceTopicsExtractor) KeyFormat(io.confluent.ksql.serde.KeyFormat) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource) KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic)

Example 5 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