Search in sources :

Example 61 with DataSource

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

the class SourceTopicsExtractor method visitAliasedRelation.

@Override
protected AstNode visitAliasedRelation(final AliasedRelation node, final Void context) {
    final SourceName structuredDataSourceName = ((Table) node.getRelation()).getName();
    final DataSource source = metaStore.getSource(structuredDataSourceName);
    if (source == null) {
        throw new KsqlException(structuredDataSourceName.text() + " does not exist.");
    }
    // This method is called first with the primary kafka topic (or the node.getFrom() node)
    if (primarySourceTopic == null) {
        primarySourceTopic = source.getKsqlTopic();
    }
    sourceTopics.add(source.getKsqlTopic());
    return node;
}
Also used : Table(io.confluent.ksql.parser.tree.Table) SourceName(io.confluent.ksql.name.SourceName) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 62 with DataSource

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

the class TopicDeleteInjector method inject.

@SuppressWarnings({ "unchecked", "UnstableApiUsage" })
@Override
public <T extends Statement> ConfiguredStatement<T> inject(final ConfiguredStatement<T> statement) {
    if (!(statement.getStatement() instanceof DropStatement)) {
        return statement;
    }
    final DropStatement dropStatement = (DropStatement) statement.getStatement();
    if (!dropStatement.isDeleteTopic()) {
        return statement;
    }
    final SourceName sourceName = dropStatement.getName();
    final DataSource source = metastore.getSource(sourceName);
    if (source != null) {
        if (source.isSource()) {
            throw new KsqlException("Cannot delete topic for read-only source: " + sourceName.text());
        }
        checkTopicRefs(source);
        deleteTopic(source);
        final Closer closer = Closer.create();
        closer.register(() -> deleteKeySubject(source));
        closer.register(() -> deleteValueSubject(source));
        try {
            closer.close();
        } catch (final KsqlException e) {
            throw e;
        } catch (final Exception e) {
            throw new KsqlException(e);
        }
    } else if (!dropStatement.getIfExists()) {
        throw new KsqlException("Could not find source to delete topic for: " + statement);
    }
    final T withoutDelete = (T) dropStatement.withoutDeleteClause();
    final String withoutDeleteText = SqlFormatter.formatSql(withoutDelete) + ";";
    return statement.withStatement(withoutDeleteText, withoutDelete);
}
Also used : Closer(com.google.common.io.Closer) SourceName(io.confluent.ksql.name.SourceName) KsqlException(io.confluent.ksql.util.KsqlException) DropStatement(io.confluent.ksql.parser.tree.DropStatement) KsqlException(io.confluent.ksql.util.KsqlException) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 63 with DataSource

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

the class KsqlResourceTest method givenSource.

private void givenSource(final DataSourceType type, final String sourceName, final String topicName, final LogicalSchema schema, final Set<SourceName> sourceReferences) {
    final KsqlTopic ksqlTopic = new KsqlTopic(topicName, KeyFormat.nonWindowed(FormatInfo.of(FormatFactory.KAFKA.name()), SerdeFeatures.of()), ValueFormat.of(FormatInfo.of(FormatFactory.JSON.name()), SerdeFeatures.of()));
    givenKafkaTopicExists(topicName);
    final DataSource source;
    switch(type) {
        case KSTREAM:
            source = new KsqlStream<>("statementText", SourceName.of(sourceName), schema, Optional.empty(), false, ksqlTopic, false);
            break;
        case KTABLE:
            source = new KsqlTable<>("statementText", SourceName.of(sourceName), schema, Optional.empty(), false, ksqlTopic, false);
            break;
        default:
            throw new IllegalArgumentException(type.toString());
    }
    metaStore.putSource(source, false);
    metaStore.addSourceReferences(source.getName(), sourceReferences);
}
Also used : KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic) DataSource(io.confluent.ksql.metastore.model.DataSource)

Example 64 with DataSource

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

the class ListSourceExecutorTest method assertSourceListWithWarning.

private static void assertSourceListWithWarning(final KsqlEntity entity, final DataSource... sources) {
    assertThat(entity, instanceOf(SourceDescriptionList.class));
    final SourceDescriptionList listing = (SourceDescriptionList) entity;
    assertThat(listing.getSourceDescriptions(), containsInAnyOrder(Arrays.stream(sources).map(s -> equalTo(SourceDescriptionFactory.create(s, true, ImmutableList.of(), ImmutableList.of(), Optional.empty(), ImmutableList.of(), ImmutableList.of(), new MetricCollectors()))).collect(Collectors.toList())));
    assertThat(listing.getWarnings(), containsInAnyOrder(Arrays.stream(sources).map(s -> equalTo(new KsqlWarning("Error from Kafka: unknown topic: " + s.getKafkaTopicName()))).collect(Collectors.toList())));
}
Also used : DataSource(io.confluent.ksql.metastore.model.DataSource) Arrays(java.util.Arrays) StreamsList(io.confluent.ksql.rest.entity.StreamsList) SessionProperties(io.confluent.ksql.rest.SessionProperties) SourceName(io.confluent.ksql.name.SourceName) RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) DescribeTables(io.confluent.ksql.parser.tree.DescribeTables) ServiceContext(io.confluent.ksql.services.ServiceContext) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) SourceDescriptionList(io.confluent.ksql.rest.entity.SourceDescriptionList) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) KsqlTable(io.confluent.ksql.metastore.model.KsqlTable) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) TemporaryEngine(io.confluent.ksql.rest.server.TemporaryEngine) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Collectors(java.util.stream.Collectors) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TestServiceContext(io.confluent.ksql.services.TestServiceContext) Matchers.contains(org.hamcrest.Matchers.contains) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Node(org.apache.kafka.common.Node) Matchers.containsString(org.hamcrest.Matchers.containsString) KsqlConstants(io.confluent.ksql.util.KsqlConstants) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Mockito.mock(org.mockito.Mockito.mock) FormatOptions(io.confluent.ksql.schema.utils.FormatOptions) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) DataSourceType(io.confluent.ksql.metastore.model.DataSource.DataSourceType) Mockito.spy(org.mockito.Mockito.spy) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) KafkaTopicClient(io.confluent.ksql.services.KafkaTopicClient) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) SessionConfig(io.confluent.ksql.config.SessionConfig) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) QueryStatusCount(io.confluent.ksql.rest.entity.QueryStatusCount) ImmutableList(com.google.common.collect.ImmutableList) SourceDescriptionEntity(io.confluent.ksql.rest.entity.SourceDescriptionEntity) ShowColumns(io.confluent.ksql.parser.tree.ShowColumns) DescribeStreams(io.confluent.ksql.parser.tree.DescribeStreams) TablesList(io.confluent.ksql.rest.entity.TablesList) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) ListTables(io.confluent.ksql.parser.tree.ListTables) Before(org.junit.Before) TopicPartitionInfo(org.apache.kafka.common.TopicPartitionInfo) ListStreams(io.confluent.ksql.parser.tree.ListStreams) EasyRandom(org.jeasy.random.EasyRandom) SourceDescriptionFactory(io.confluent.ksql.rest.entity.SourceDescriptionFactory) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) KsqlStream(io.confluent.ksql.metastore.model.KsqlStream) Mockito.verify(org.mockito.Mockito.verify) SourceInfo(io.confluent.ksql.rest.entity.SourceInfo) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) Mockito.never(org.mockito.Mockito.never) Rule(org.junit.Rule) Collections(java.util.Collections) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SourceDescriptionList(io.confluent.ksql.rest.entity.SourceDescriptionList) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning)

Example 65 with DataSource

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

the class ListSourceExecutorTest method shouldShowColumnsSource.

@Test
public void shouldShowColumnsSource() {
    // Given:
    engine.givenSource(DataSourceType.KSTREAM, "SOURCE");
    final ExecuteResult result = engine.getEngine().execute(engine.getServiceContext(), engine.configure("CREATE STREAM SINK AS SELECT * FROM source;"));
    final PersistentQueryMetadata metadata = (PersistentQueryMetadata) result.getQuery().orElseThrow(IllegalArgumentException::new);
    final DataSource stream = engine.getEngine().getMetaStore().getSource(SourceName.of("SINK"));
    // When:
    final SourceDescriptionEntity sourceDescription = (SourceDescriptionEntity) CUSTOM_EXECUTORS.showColumns().execute(ConfiguredStatement.of(PreparedStatement.of("DESCRIBE SINK;", new ShowColumns(SourceName.of("SINK"), false)), SessionConfig.of(engine.getKsqlConfig(), ImmutableMap.of())), SESSION_PROPERTIES, engine.getEngine(), engine.getServiceContext()).getEntity().orElseThrow(IllegalStateException::new);
    // Then:
    final QueryStatusCount queryStatusCount = QueryStatusCount.fromStreamsStateCounts(Collections.singletonMap(metadata.getState(), 1));
    assertThat(sourceDescription.getSourceDescription(), equalTo(SourceDescriptionFactory.create(stream, false, ImmutableList.of(), ImmutableList.of(new RunningQuery(metadata.getStatementString(), ImmutableSet.of(metadata.getSinkName().get().toString(FormatOptions.noEscape())), ImmutableSet.of(metadata.getResultTopic().get().getKafkaTopicName()), metadata.getQueryId(), queryStatusCount, KsqlConstants.KsqlQueryType.PERSISTENT)), Optional.empty(), ImmutableList.of(), ImmutableList.of(), new MetricCollectors())));
}
Also used : RunningQuery(io.confluent.ksql.rest.entity.RunningQuery) SourceDescriptionEntity(io.confluent.ksql.rest.entity.SourceDescriptionEntity) ShowColumns(io.confluent.ksql.parser.tree.ShowColumns) QueryStatusCount(io.confluent.ksql.rest.entity.QueryStatusCount) MetricCollectors(io.confluent.ksql.metrics.MetricCollectors) ExecuteResult(io.confluent.ksql.KsqlExecutionContext.ExecuteResult) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) DataSource(io.confluent.ksql.metastore.model.DataSource) Test(org.junit.Test)

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