Search in sources :

Example 66 with DataSource

use of io.confluent.ksql.metastore.model.DataSource in project ksql by confluentinc.

the class SchemaKSourceFactory method buildTable.

private static SchemaKTable<?> buildTable(final PlanBuildContext buildContext, final DataSource dataSource, final Stacker contextStacker) {
    final KeyFormat keyFormat = dataSource.getKsqlTopic().getKeyFormat();
    if (keyFormat.isWindowed()) {
        throw new IllegalArgumentException("windowed");
    }
    final SourceStep<KTableHolder<GenericKey>> step;
    final int pseudoColumnVersionToUse = determinePseudoColumnVersionToUse(buildContext);
    // If the old query has a v1 table step, continue to use it.
    // See https://github.com/confluentinc/ksql/pull/7990
    boolean useOldExecutionStepVersion = false;
    if (buildContext.getPlanInfo().isPresent()) {
        final Set<ExecutionStep<?>> sourceSteps = buildContext.getPlanInfo().get().getSources();
        useOldExecutionStepVersion = sourceSteps.stream().anyMatch(executionStep -> executionStep instanceof TableSourceV1);
    }
    if (useOldExecutionStepVersion && pseudoColumnVersionToUse != SystemColumns.LEGACY_PSEUDOCOLUMN_VERSION_NUMBER) {
        throw new IllegalStateException("TableSourceV2 was released in conjunction with pseudocolumn" + "version 1. Something has gone very wrong");
    }
    if (buildContext.getKsqlConfig().getBoolean(KsqlConfig.KSQL_ROWPARTITION_ROWOFFSET_ENABLED) && !useOldExecutionStepVersion) {
        step = ExecutionStepFactory.tableSource(contextStacker, dataSource.getSchema(), dataSource.getKafkaTopicName(), Formats.from(dataSource.getKsqlTopic()), dataSource.getTimestampColumn(), InternalFormats.of(keyFormat, Formats.from(dataSource.getKsqlTopic()).getValueFormat()), pseudoColumnVersionToUse);
    } else {
        step = ExecutionStepFactory.tableSourceV1(contextStacker, dataSource.getSchema(), dataSource.getKafkaTopicName(), Formats.from(dataSource.getKsqlTopic()), dataSource.getTimestampColumn(), pseudoColumnVersionToUse);
    }
    return schemaKTable(buildContext, resolveSchema(buildContext, step, dataSource), dataSource.getKsqlTopic().getKeyFormat(), step);
}
Also used : ExecutionStep(io.confluent.ksql.execution.plan.ExecutionStep) DataSource(io.confluent.ksql.metastore.model.DataSource) ExecutionStep(io.confluent.ksql.execution.plan.ExecutionStep) SystemColumns(io.confluent.ksql.schema.ksql.SystemColumns) PlanBuildContext(io.confluent.ksql.planner.plan.PlanBuildContext) KeyFormat(io.confluent.ksql.serde.KeyFormat) QueryContext(io.confluent.ksql.execution.context.QueryContext) Set(java.util.Set) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Formats(io.confluent.ksql.execution.plan.Formats) InternalFormats(io.confluent.ksql.serde.InternalFormats) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) StreamSource(io.confluent.ksql.execution.plan.StreamSource) StepSchemaResolver(io.confluent.ksql.execution.streams.StepSchemaResolver) WindowInfo(io.confluent.ksql.serde.WindowInfo) KStreamHolder(io.confluent.ksql.execution.plan.KStreamHolder) KTableHolder(io.confluent.ksql.execution.plan.KTableHolder) SourceStep(io.confluent.ksql.execution.plan.SourceStep) WindowedStreamSource(io.confluent.ksql.execution.plan.WindowedStreamSource) TableSourceV1(io.confluent.ksql.execution.plan.TableSourceV1) Windowed(org.apache.kafka.streams.kstream.Windowed) Stacker(io.confluent.ksql.execution.context.QueryContext.Stacker) GenericKey(io.confluent.ksql.GenericKey) ExecutionStepFactory(io.confluent.ksql.execution.streams.ExecutionStepFactory) TableSourceV1(io.confluent.ksql.execution.plan.TableSourceV1) KTableHolder(io.confluent.ksql.execution.plan.KTableHolder) KeyFormat(io.confluent.ksql.serde.KeyFormat)

Example 67 with DataSource

use of io.confluent.ksql.metastore.model.DataSource in project ksql by confluentinc.

the class InsertValuesExecutor method getDataSource.

private static DataSource getDataSource(final KsqlConfig ksqlConfig, final MetaStore metaStore, final InsertValues insertValues) {
    final DataSource dataSource = metaStore.getSource(insertValues.getTarget());
    if (dataSource == null) {
        throw new KsqlException("Cannot insert values into an unknown stream/table: " + insertValues.getTarget());
    }
    if (dataSource.getKsqlTopic().getKeyFormat().isWindowed()) {
        throw new KsqlException("Cannot insert values into windowed stream/table!");
    }
    final ReservedInternalTopics internalTopics = new ReservedInternalTopics(ksqlConfig);
    if (internalTopics.isReadOnly(dataSource.getKafkaTopicName())) {
        throw new KsqlException("Cannot insert values into read-only topic: " + dataSource.getKafkaTopicName());
    }
    if (dataSource.isSource()) {
        throw new KsqlException(String.format("Cannot insert values into read-only %s: %s", dataSource.getDataSourceType().getKsqlType().toLowerCase(), dataSource.getName().text()));
    }
    return dataSource;
}
Also used : ReservedInternalTopics(io.confluent.ksql.util.ReservedInternalTopics) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 68 with DataSource

use of io.confluent.ksql.metastore.model.DataSource in project ksql by confluentinc.

the class ListSourceExecutor method sourceDescriptionList.

private static Optional<KsqlEntity> sourceDescriptionList(final ConfiguredStatement<? extends StatementWithExtendedClause> statement, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final ServiceContext serviceContext, final List<? extends DataSource> sources, final boolean extended) {
    final RemoteHostExecutor remoteHostExecutor = RemoteHostExecutor.create(statement, sessionProperties, executionContext, serviceContext.getKsqlClient());
    final Multimap<String, SourceDescription> remoteSourceDescriptions = extended ? RemoteSourceDescriptionExecutor.fetchSourceDescriptions(remoteHostExecutor) : ImmutableMultimap.of();
    final List<SourceDescriptionWithWarnings> descriptions = sources.stream().map(s -> describeSource(statement.getSessionConfig().getConfig(false), executionContext, serviceContext, s.getName(), extended, statement, sessionProperties, remoteSourceDescriptions.get(s.getName().toString()))).collect(Collectors.toList());
    return Optional.of(new SourceDescriptionList(statement.getStatementText(), descriptions.stream().map(d -> d.description).collect(Collectors.toList()), descriptions.stream().flatMap(d -> d.warnings.stream()).collect(Collectors.toList())));
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) StreamsList(io.confluent.ksql.rest.entity.StreamsList) SessionProperties(io.confluent.ksql.rest.SessionProperties) SourceName(io.confluent.ksql.name.SourceName) QueryOffsetSummary(io.confluent.ksql.rest.entity.QueryOffsetSummary) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) DescribeTables(io.confluent.ksql.parser.tree.DescribeTables) ServiceContext(io.confluent.ksql.services.ServiceContext) KafkaException(org.apache.kafka.common.KafkaException) QueryTopicOffsetSummary(io.confluent.ksql.rest.entity.QueryTopicOffsetSummary) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription) Map(java.util.Map) QueryId(io.confluent.ksql.query.QueryId) SourceDescriptionList(io.confluent.ksql.rest.entity.SourceDescriptionList) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) KsqlTable(io.confluent.ksql.metastore.model.KsqlTable) TopicPartition(org.apache.kafka.common.TopicPartition) ImmutableSet(com.google.common.collect.ImmutableSet) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) KafkaResponseGetFailedException(io.confluent.ksql.exception.KafkaResponseGetFailedException) Collectors(java.util.stream.Collectors) List(java.util.List) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) Entry(java.util.Map.Entry) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) Optional(java.util.Optional) KsqlConstants(io.confluent.ksql.util.KsqlConstants) KsqlRestApplication(io.confluent.ksql.rest.server.KsqlRestApplication) HashMap(java.util.HashMap) Multimap(com.google.common.collect.Multimap) ArrayList(java.util.ArrayList) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) HashSet(java.util.HashSet) QueryStatusCount(io.confluent.ksql.rest.entity.QueryStatusCount) ImmutableList(com.google.common.collect.ImmutableList) SourceDescriptionEntity(io.confluent.ksql.rest.entity.SourceDescriptionEntity) MetaStore(io.confluent.ksql.metastore.MetaStore) ShowColumns(io.confluent.ksql.parser.tree.ShowColumns) DescribeStreams(io.confluent.ksql.parser.tree.DescribeStreams) TablesList(io.confluent.ksql.rest.entity.TablesList) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) LinkedList(java.util.LinkedList) ListTables(io.confluent.ksql.parser.tree.ListTables) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) Table(io.confluent.ksql.rest.entity.SourceInfo.Table) ListStreams(io.confluent.ksql.parser.tree.ListStreams) QueryApplicationId(io.confluent.ksql.util.QueryApplicationId) SourceDescriptionFactory(io.confluent.ksql.rest.entity.SourceDescriptionFactory) KsqlStream(io.confluent.ksql.metastore.model.KsqlStream) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) StatementWithExtendedClause(io.confluent.ksql.parser.tree.StatementWithExtendedClause) ConsumerPartitionOffsets(io.confluent.ksql.rest.entity.ConsumerPartitionOffsets) Stream(io.confluent.ksql.rest.entity.SourceInfo.Stream) Collections(java.util.Collections) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) SourceDescriptionList(io.confluent.ksql.rest.entity.SourceDescriptionList) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription)

Example 69 with DataSource

use of io.confluent.ksql.metastore.model.DataSource in project ksql by confluentinc.

the class KsqlTesterTest method pipeInput.

private void pipeInput(final ConfiguredStatement<InsertValues> statement) {
    final InsertValues insertValues = statement.getStatement();
    final DataSource dataSource = engine.getMetaStore().getSource(insertValues.getTarget());
    if (dataSource == null) {
        throw new KsqlException("Unknown data source " + insertValues.getTarget());
    }
    final KsqlGenericRecord record = new GenericRecordFactory(config, engine.getMetaStore(), System::currentTimeMillis).build(insertValues.getColumns(), insertValues.getValues(), dataSource.getSchema(), dataSource.getDataSourceType());
    driverPipeline.pipeInput(dataSource.getKafkaTopicName(), record.key, record.value, record.ts);
}
Also used : GenericRecordFactory(io.confluent.ksql.engine.generic.GenericRecordFactory) InsertValues(io.confluent.ksql.parser.tree.InsertValues) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource) KsqlGenericRecord(io.confluent.ksql.engine.generic.KsqlGenericRecord)

Example 70 with DataSource

use of io.confluent.ksql.metastore.model.DataSource in project ksql by confluentinc.

the class KsqlTesterTest method execute.

@SuppressWarnings("unchecked")
private void execute(final ParsedStatement parsedStatement) {
    final PreparedStatement<?> engineStatement = engine.prepare(parsedStatement);
    final ConfiguredStatement<?> configured = ConfiguredStatement.of(engineStatement, SessionConfig.of(config, overrides));
    createTopics(engineStatement);
    if (engineStatement.getStatement() instanceof InsertValues) {
        pipeInput((ConfiguredStatement<InsertValues>) configured);
        return;
    } else if (engineStatement.getStatement() instanceof SetProperty) {
        PropertyOverrider.set((ConfiguredStatement<SetProperty>) configured, overrides);
        return;
    } else if (engineStatement.getStatement() instanceof UnsetProperty) {
        PropertyOverrider.unset((ConfiguredStatement<UnsetProperty>) configured, overrides);
        return;
    }
    final ConfiguredStatement<?> injected = formatInjector.inject(configured);
    final ExecuteResult result = engine.execute(serviceContext, injected);
    // is DDL statement
    if (!result.getQuery().isPresent()) {
        return;
    }
    final PersistentQueryMetadata query = (PersistentQueryMetadata) result.getQuery().get();
    final Topology topology = query.getTopology();
    final Properties properties = new Properties();
    properties.putAll(query.getStreamsProperties());
    properties.put(StreamsConfig.STATE_DIR_CONFIG, tmpFolder.getRoot().getAbsolutePath());
    final TopologyTestDriver driver = new TopologyTestDriver(topology, properties);
    final List<TopicInfo> inputTopics = query.getSourceNames().stream().map(sn -> engine.getMetaStore().getSource(sn)).map(ds -> new TopicInfo(ds.getKafkaTopicName(), keySerde(ds), valueSerde(ds))).collect(Collectors.toList());
    // Sink may be Optional for source tables. Once source table query execution is supported, then
    // we would need have a condition to not create an output topic info
    final DataSource output = engine.getMetaStore().getSource(query.getSinkName().get());
    final TopicInfo outputInfo = new TopicInfo(output.getKafkaTopicName(), keySerde(output), valueSerde(output));
    driverPipeline.addDriver(driver, inputTopics, outputInfo);
    drivers.put(query.getQueryId(), new DriverAndProperties(driver, properties));
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) NoSuchFileException(java.nio.file.NoSuchFileException) SequentialQueryIdGenerator(io.confluent.ksql.query.id.SequentialQueryIdGenerator) ServiceContext(io.confluent.ksql.services.ServiceContext) LoggerFactory(org.slf4j.LoggerFactory) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) InsertValues(io.confluent.ksql.parser.tree.InsertValues) Serde(org.apache.kafka.common.serialization.Serde) Map(java.util.Map) After(org.junit.After) DefaultFormatInjector(io.confluent.ksql.format.DefaultFormatInjector) QueryId(io.confluent.ksql.query.QueryId) FakeKafkaTopicClient(io.confluent.ksql.services.FakeKafkaTopicClient) Path(java.nio.file.Path) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Parameterized(org.junit.runners.Parameterized) QueryMetadata(io.confluent.ksql.util.QueryMetadata) MockSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient) ImmutableMap(com.google.common.collect.ImmutableMap) UnsetProperty(io.confluent.ksql.parser.tree.UnsetProperty) TestDirective(io.confluent.ksql.test.parser.TestDirective) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) KsqlConfig(io.confluent.ksql.util.KsqlConfig) Collectors(java.util.stream.Collectors) TestServiceContext(io.confluent.ksql.services.TestServiceContext) Objects(java.util.Objects) MetaStoreImpl(io.confluent.ksql.metastore.MetaStoreImpl) List(java.util.List) SetProperty(io.confluent.ksql.parser.tree.SetProperty) NoopProcessingLogContext(io.confluent.ksql.logging.processing.NoopProcessingLogContext) PropertyOverrider(io.confluent.ksql.properties.PropertyOverrider) KsqlException(io.confluent.ksql.util.KsqlException) Optional(java.util.Optional) TopicInfo(io.confluent.ksql.test.driver.TestDriverPipeline.TopicInfo) Topology(org.apache.kafka.streams.Topology) StreamsConfig(org.apache.kafka.streams.StreamsConfig) Iterables(com.google.common.collect.Iterables) FormatOptions(io.confluent.ksql.schema.utils.FormatOptions) AssertTable(io.confluent.ksql.parser.AssertTable) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) KsqlGenericRecord(io.confluent.ksql.engine.generic.KsqlGenericRecord) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) AssertStream(io.confluent.ksql.parser.tree.AssertStream) AssertStatement(io.confluent.ksql.parser.tree.AssertStatement) SessionConfig(io.confluent.ksql.config.SessionConfig) ImmutableList(com.google.common.collect.ImmutableList) Injector(io.confluent.ksql.statement.Injector) KsqlTestException(io.confluent.ksql.test.KsqlTestException) CreateSource(io.confluent.ksql.parser.tree.CreateSource) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) AssertValues(io.confluent.ksql.parser.tree.AssertValues) KsqlTestFolder(io.confluent.ksql.test.util.KsqlTestFolder) GenericRowSerDe(io.confluent.ksql.serde.GenericRowSerDe) Before(org.junit.Before) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) TestStatement(io.confluent.ksql.test.parser.TestStatement) TestFunctionRegistry(io.confluent.ksql.test.tools.TestFunctionRegistry) Properties(java.util.Properties) Logger(org.slf4j.Logger) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) GenericRecordFactory(io.confluent.ksql.engine.generic.GenericRecordFactory) ServiceInfo(io.confluent.ksql.ServiceInfo) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) SqlTestLoader(io.confluent.ksql.test.parser.SqlTestLoader) AssertTombstone(io.confluent.ksql.parser.tree.AssertTombstone) File(java.io.File) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) GenericKeySerDe(io.confluent.ksql.serde.GenericKeySerDe) Rule(org.junit.Rule) Paths(java.nio.file.Paths) GenericRow(io.confluent.ksql.GenericRow) QueryEventListener(io.confluent.ksql.engine.QueryEventListener) GenericKey(io.confluent.ksql.GenericKey) PersistenceSchema(io.confluent.ksql.schema.ksql.PersistenceSchema) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) TemporaryFolder(org.junit.rules.TemporaryFolder) UnsetProperty(io.confluent.ksql.parser.tree.UnsetProperty) TopologyTestDriver(org.apache.kafka.streams.TopologyTestDriver) Topology(org.apache.kafka.streams.Topology) Properties(java.util.Properties) TopicInfo(io.confluent.ksql.test.driver.TestDriverPipeline.TopicInfo) DataSource(io.confluent.ksql.metastore.model.DataSource) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) InsertValues(io.confluent.ksql.parser.tree.InsertValues) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) SetProperty(io.confluent.ksql.parser.tree.SetProperty) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Aggregations

DataSource (io.confluent.ksql.metastore.model.DataSource)70 Test (org.junit.Test)25 KsqlException (io.confluent.ksql.util.KsqlException)24 SourceName (io.confluent.ksql.name.SourceName)21 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)12 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)12 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)10 MetricCollectors (io.confluent.ksql.metrics.MetricCollectors)9 Collectors (java.util.stream.Collectors)9 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)8 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)7 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)7 Optional (java.util.Optional)7 ImmutableList (com.google.common.collect.ImmutableList)6 GenericKey (io.confluent.ksql.GenericKey)6 QueryId (io.confluent.ksql.query.QueryId)6 ServiceContext (io.confluent.ksql.services.ServiceContext)6 KsqlConfig (io.confluent.ksql.util.KsqlConfig)6 Collections (java.util.Collections)6 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)6