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)));
}
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)));
}
}
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));
}
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());
}
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)));
}
Aggregations