Search in sources :

Example 1 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext in project ksql by confluentinc.

the class ListConnectorsExecutor method execute.

@SuppressWarnings("OptionalGetWithoutIsPresent")
public StatementExecutorResponse execute(final ConfiguredStatement<ListConnectors> configuredStatement, final SessionProperties sessionProperties, final KsqlExecutionContext ksqlExecutionContext, final ServiceContext serviceContext) {
    final ConnectClient connectClient = serviceContext.getConnectClient();
    final ConnectResponse<List<String>> connectors = serviceContext.getConnectClient().connectors();
    if (connectors.error().isPresent()) {
        return StatementExecutorResponse.handled(connectErrorHandler.handle(configuredStatement, connectors));
    }
    final List<SimpleConnectorInfo> infos = new ArrayList<>();
    final List<KsqlWarning> warnings = new ArrayList<>();
    final Scope scope = configuredStatement.getStatement().getScope();
    for (final String name : connectors.datum().get()) {
        final ConnectResponse<ConnectorInfo> response = connectClient.describe(name);
        if (response.datum().filter(i -> inScope(i.type(), scope)).isPresent()) {
            final ConnectResponse<ConnectorStateInfo> status = connectClient.status(name);
            infos.add(fromConnectorInfoResponse(name, response, status));
        } else if (response.error().isPresent()) {
            if (scope == Scope.ALL) {
                infos.add(new SimpleConnectorInfo(name, ConnectorType.UNKNOWN, null, null));
            }
            warnings.add(new KsqlWarning(String.format("Could not describe connector %s: %s", name, response.error().get())));
        }
    }
    return StatementExecutorResponse.handled(Optional.of(new ConnectorList(configuredStatement.getStatementText(), warnings, infos)));
}
Also used : SessionProperties(io.confluent.ksql.rest.SessionProperties) Scope(io.confluent.ksql.parser.tree.ListConnectors.Scope) ConnectClient(io.confluent.ksql.services.ConnectClient) ServiceContext(io.confluent.ksql.services.ServiceContext) SimpleConnectorInfo(io.confluent.ksql.rest.entity.SimpleConnectorInfo) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) ArrayList(java.util.ArrayList) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) List(java.util.List) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ConnectorList(io.confluent.ksql.rest.entity.ConnectorList) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) State(org.apache.kafka.connect.runtime.AbstractStatus.State) ConnectResponse(io.confluent.ksql.services.ConnectClient.ConnectResponse) Optional(java.util.Optional) ListConnectors(io.confluent.ksql.parser.tree.ListConnectors) AbstractState(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo.AbstractState) ConnectorType(org.apache.kafka.connect.runtime.rest.entities.ConnectorType) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) SimpleConnectorInfo(io.confluent.ksql.rest.entity.SimpleConnectorInfo) SimpleConnectorInfo(io.confluent.ksql.rest.entity.SimpleConnectorInfo) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ArrayList(java.util.ArrayList) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) ConnectorList(io.confluent.ksql.rest.entity.ConnectorList) Scope(io.confluent.ksql.parser.tree.ListConnectors.Scope) ConnectClient(io.confluent.ksql.services.ConnectClient) ArrayList(java.util.ArrayList) List(java.util.List) ConnectorList(io.confluent.ksql.rest.entity.ConnectorList) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo)

Example 2 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext in project ksql by confluentinc.

the class ListTopicsExecutor method execute.

public static StatementExecutorResponse execute(final ConfiguredStatement<ListTopics> statement, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
    final KafkaTopicClient client = serviceContext.getTopicClient();
    final Map<String, TopicDescription> topicDescriptions = listTopics(client, statement);
    if (statement.getStatement().getShowExtended()) {
        final KafkaConsumerGroupClient consumerGroupClient = new KafkaConsumerGroupClientImpl(serviceContext::getAdminClient);
        final Map<String, List<Integer>> topicConsumersAndGroupCount = getTopicConsumerAndGroupCounts(consumerGroupClient);
        final List<KafkaTopicInfoExtended> topicInfoExtendedList = topicDescriptions.values().stream().map(desc -> topicDescriptionToTopicInfoExtended(desc, topicConsumersAndGroupCount)).collect(Collectors.toList());
        return StatementExecutorResponse.handled(Optional.of(new KafkaTopicsListExtended(statement.getStatementText(), topicInfoExtendedList)));
    } else {
        final List<KafkaTopicInfo> topicInfoList = topicDescriptions.values().stream().map(ListTopicsExecutor::topicDescriptionToTopicInfo).collect(Collectors.toList());
        return StatementExecutorResponse.handled(Optional.of(new KafkaTopicsList(statement.getStatementText(), topicInfoList)));
    }
}
Also used : Arrays(java.util.Arrays) SessionProperties(io.confluent.ksql.rest.SessionProperties) ListTopics(io.confluent.ksql.parser.tree.ListTopics) ServiceContext(io.confluent.ksql.services.ServiceContext) ConsumerSummary(io.confluent.ksql.services.KafkaConsumerGroupClient.ConsumerSummary) HashMap(java.util.HashMap) KafkaTopicsList(io.confluent.ksql.rest.entity.KafkaTopicsList) KafkaTopicsListExtended(io.confluent.ksql.rest.entity.KafkaTopicsListExtended) ReservedInternalTopics(io.confluent.ksql.util.ReservedInternalTopics) KafkaTopicClient(io.confluent.ksql.services.KafkaTopicClient) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) TopicPartition(org.apache.kafka.common.TopicPartition) KafkaConsumerGroupClientImpl(io.confluent.ksql.services.KafkaConsumerGroupClientImpl) Collection(java.util.Collection) Set(java.util.Set) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Collectors(java.util.stream.Collectors) List(java.util.List) TreeMap(java.util.TreeMap) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) KafkaConsumerGroupClient(io.confluent.ksql.services.KafkaConsumerGroupClient) KafkaTopicInfoExtended(io.confluent.ksql.rest.entity.KafkaTopicInfoExtended) KafkaTopicInfo(io.confluent.ksql.rest.entity.KafkaTopicInfo) Optional(java.util.Optional) KafkaTopicsList(io.confluent.ksql.rest.entity.KafkaTopicsList) KafkaTopicsListExtended(io.confluent.ksql.rest.entity.KafkaTopicsListExtended) KafkaTopicInfo(io.confluent.ksql.rest.entity.KafkaTopicInfo) KafkaTopicClient(io.confluent.ksql.services.KafkaTopicClient) KafkaConsumerGroupClientImpl(io.confluent.ksql.services.KafkaConsumerGroupClientImpl) KafkaTopicInfoExtended(io.confluent.ksql.rest.entity.KafkaTopicInfoExtended) TopicDescription(org.apache.kafka.clients.admin.TopicDescription) KafkaTopicsList(io.confluent.ksql.rest.entity.KafkaTopicsList) ArrayList(java.util.ArrayList) List(java.util.List) KafkaConsumerGroupClient(io.confluent.ksql.services.KafkaConsumerGroupClient)

Example 3 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext in project ksql by confluentinc.

the class DescribeConnectorExecutor method execute.

@SuppressWarnings("OptionalGetWithoutIsPresent")
public StatementExecutorResponse execute(final ConfiguredStatement<DescribeConnector> configuredStatement, final SessionProperties sessionProperties, final KsqlExecutionContext ksqlExecutionContext, final ServiceContext serviceContext) {
    final String connectorName = configuredStatement.getStatement().getConnectorName();
    final ConnectResponse<ConnectorStateInfo> statusResponse = serviceContext.getConnectClient().status(connectorName);
    if (statusResponse.error().isPresent()) {
        return StatementExecutorResponse.handled(connectErrorHandler.handle(configuredStatement, statusResponse));
    }
    final ConnectResponse<ConnectorInfo> infoResponse = serviceContext.getConnectClient().describe(connectorName);
    if (infoResponse.error().isPresent()) {
        return StatementExecutorResponse.handled(connectErrorHandler.handle(configuredStatement, infoResponse));
    }
    final ConnectorStateInfo status = statusResponse.datum().get();
    final ConnectorInfo info = infoResponse.datum().get();
    final Optional<Connector> connector = connectorFactory.apply(info);
    final List<KsqlWarning> warnings;
    final List<String> topics;
    if (connector.isPresent()) {
        // Small optimization. If a connector's info is not found in the response, don't query for
        // active topics with the given connectorName
        final ConnectResponse<Map<String, Map<String, List<String>>>> topicsResponse = serviceContext.getConnectClient().topics(connectorName);
        // server logs.
        if (topicsResponse.error().isPresent() && topicsResponse.httpCode() == HttpStatus.SC_NOT_FOUND) {
            topics = ImmutableList.of();
            warnings = ImmutableList.of();
            LOG.warn("Could not list related topics due to error: " + topicsResponse.error().get());
        } else if (topicsResponse.error().isPresent()) {
            topics = ImmutableList.of();
            warnings = ImmutableList.of(new KsqlWarning("Could not list related topics due to error: " + topicsResponse.error().get()));
        } else {
            topics = topicsResponse.datum().get().get(connectorName).getOrDefault(TOPICS_KEY, ImmutableList.of());
            warnings = ImmutableList.of();
        }
    } else {
        topics = ImmutableList.of();
        warnings = ImmutableList.of();
    }
    final List<SourceDescription> sources;
    if (connector.isPresent()) {
        sources = ksqlExecutionContext.getMetaStore().getAllDataSources().values().stream().filter(source -> topics.contains(source.getKafkaTopicName())).map(source -> SourceDescriptionFactory.create(source, false, ImmutableList.of(), ImmutableList.of(), Optional.empty(), ImmutableList.of(), ImmutableList.of(), ksqlExecutionContext.metricCollectors())).collect(Collectors.toList());
    } else {
        sources = ImmutableList.of();
    }
    final ConnectorDescription description = new ConnectorDescription(configuredStatement.getStatementText(), info.config().get(ConnectorConfig.CONNECTOR_CLASS_CONFIG), status, sources, topics, warnings);
    return StatementExecutorResponse.handled(Optional.of(description));
}
Also used : Connectors(io.confluent.ksql.connect.supported.Connectors) SessionProperties(io.confluent.ksql.rest.SessionProperties) ServiceContext(io.confluent.ksql.services.ServiceContext) LoggerFactory(org.slf4j.LoggerFactory) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription) Function(java.util.function.Function) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) DescribeConnector(io.confluent.ksql.parser.tree.DescribeConnector) Logger(org.slf4j.Logger) Connector(io.confluent.ksql.connect.Connector) SourceDescriptionFactory(io.confluent.ksql.rest.entity.SourceDescriptionFactory) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Collectors(java.util.stream.Collectors) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) List(java.util.List) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) ConnectorDescription(io.confluent.ksql.rest.entity.ConnectorDescription) ConnectResponse(io.confluent.ksql.services.ConnectClient.ConnectResponse) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) HttpStatus(org.apache.hc.core5.http.HttpStatus) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) ConnectorConfig(org.apache.kafka.connect.runtime.ConnectorConfig) DescribeConnector(io.confluent.ksql.parser.tree.DescribeConnector) Connector(io.confluent.ksql.connect.Connector) ConnectorInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo) KsqlWarning(io.confluent.ksql.rest.entity.KsqlWarning) ConnectorDescription(io.confluent.ksql.rest.entity.ConnectorDescription) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Map(java.util.Map) ConnectorStateInfo(org.apache.kafka.connect.runtime.rest.entities.ConnectorStateInfo) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription)

Example 4 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext in project ksql by confluentinc.

the class ExplainExecutor method explainStatement.

private static QueryDescription explainStatement(final ConfiguredStatement<Explain> explain, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
    final Statement statement = explain.getStatement().getStatement().orElseThrow(() -> new KsqlStatementException("must have either queryID or statement", explain.getStatementText()));
    if (!(statement instanceof Query || statement instanceof QueryContainer)) {
        throw new KsqlException("The provided statement does not run a ksql query");
    }
    final PreparedStatement<?> preparedStatement = PreparedStatement.of(explain.getStatementText().substring("EXPLAIN ".length()), statement);
    final QueryMetadata metadata;
    final KsqlExecutionContext sandbox = executionContext.createSandbox(serviceContext);
    if (preparedStatement.getStatement() instanceof Query) {
        metadata = sandbox.executeTransientQuery(serviceContext, ConfiguredStatement.of(preparedStatement, explain.getSessionConfig()).cast(), false);
    } else {
        metadata = sandbox.execute(serviceContext, ConfiguredStatement.of(preparedStatement, explain.getSessionConfig())).getQuery().orElseThrow(() -> new IllegalStateException("The provided statement did not run a ksql query"));
    }
    return QueryDescriptionFactory.forQueryMetadata(metadata, Collections.emptyMap());
}
Also used : QueryMetadata(io.confluent.ksql.util.QueryMetadata) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Query(io.confluent.ksql.parser.tree.Query) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) Statement(io.confluent.ksql.parser.tree.Statement) PreparedStatement(io.confluent.ksql.parser.KsqlParser.PreparedStatement) KsqlStatementException(io.confluent.ksql.util.KsqlStatementException) KsqlException(io.confluent.ksql.util.KsqlException) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) QueryContainer(io.confluent.ksql.parser.tree.QueryContainer)

Example 5 with KsqlExecutionContext

use of io.confluent.ksql.KsqlExecutionContext in project ksql by confluentinc.

the class ListFunctionsExecutor method execute.

public static StatementExecutorResponse execute(final ConfiguredStatement<ListFunctions> statement, final SessionProperties sessionProperties, final KsqlExecutionContext executionContext, final ServiceContext serviceContext) {
    final FunctionRegistry functionRegistry = executionContext.getMetaStore();
    final List<SimpleFunctionInfo> all = functionRegistry.listFunctions().stream().map(factory -> new SimpleFunctionInfo(factory.getName().toUpperCase(), FunctionType.SCALAR, factory.getMetadata().getCategory())).collect(Collectors.toList());
    functionRegistry.listTableFunctions().stream().map(factory -> new SimpleFunctionInfo(factory.getName().toUpperCase(), FunctionType.TABLE, factory.getMetadata().getCategory())).forEach(all::add);
    functionRegistry.listAggregateFunctions().stream().map(factory -> new SimpleFunctionInfo(factory.getName().toUpperCase(), FunctionType.AGGREGATE, factory.getMetadata().getCategory())).forEach(all::add);
    return StatementExecutorResponse.handled(Optional.of(new FunctionNameList(statement.getStatementText(), all)));
}
Also used : FunctionNameList(io.confluent.ksql.rest.entity.FunctionNameList) FunctionType(io.confluent.ksql.rest.entity.FunctionType) List(java.util.List) SessionProperties(io.confluent.ksql.rest.SessionProperties) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) ServiceContext(io.confluent.ksql.services.ServiceContext) ListFunctions(io.confluent.ksql.parser.tree.ListFunctions) Optional(java.util.Optional) ConfiguredStatement(io.confluent.ksql.statement.ConfiguredStatement) SimpleFunctionInfo(io.confluent.ksql.rest.entity.SimpleFunctionInfo) Collectors(java.util.stream.Collectors) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) FunctionNameList(io.confluent.ksql.rest.entity.FunctionNameList) SimpleFunctionInfo(io.confluent.ksql.rest.entity.SimpleFunctionInfo)

Aggregations

KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)13 ConfiguredStatement (io.confluent.ksql.statement.ConfiguredStatement)7 ServiceContext (io.confluent.ksql.services.ServiceContext)6 SessionProperties (io.confluent.ksql.rest.SessionProperties)5 List (java.util.List)5 Optional (java.util.Optional)5 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)4 QueryMetadata (io.confluent.ksql.util.QueryMetadata)4 Collectors (java.util.stream.Collectors)4 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)3 KsqlWarning (io.confluent.ksql.rest.entity.KsqlWarning)3 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 ImmutableList (com.google.common.collect.ImmutableList)2 PreparedStatement (io.confluent.ksql.parser.KsqlParser.PreparedStatement)2 Query (io.confluent.ksql.parser.tree.Query)2 ConnectResponse (io.confluent.ksql.services.ConnectClient.ConnectResponse)2 Injector (io.confluent.ksql.statement.Injector)2 KsqlConfig (io.confluent.ksql.util.KsqlConfig)2 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)2