Search in sources :

Example 1 with Injector

use of io.confluent.ksql.statement.Injector in project ksql by confluentinc.

the class KsqlContext method sql.

public List<QueryMetadata> sql(final String sql, final Map<String, ?> overriddenProperties) {
    final List<ParsedStatement> statements = ksqlEngine.parse(sql);
    final KsqlExecutionContext sandbox = ksqlEngine.createSandbox(ksqlEngine.getServiceContext());
    final Map<String, Object> validationOverrides = new HashMap<>(overriddenProperties);
    for (final ParsedStatement stmt : statements) {
        execute(sandbox, stmt, ksqlConfig, validationOverrides, injectorFactory.apply(sandbox, sandbox.getServiceContext()));
    }
    final List<QueryMetadata> queries = new ArrayList<>();
    final Injector injector = injectorFactory.apply(ksqlEngine, serviceContext);
    final Map<String, Object> executionOverrides = new HashMap<>(overriddenProperties);
    for (final ParsedStatement parsed : statements) {
        execute(ksqlEngine, parsed, ksqlConfig, executionOverrides, injector).getQuery().ifPresent(queries::add);
    }
    for (final QueryMetadata queryMetadata : queries) {
        if (queryMetadata instanceof PersistentQueryMetadata) {
            queryMetadata.start();
        } else {
            LOG.warn("Ignoring statemenst: {}", sql);
            LOG.warn("Only CREATE statements can run in KSQL embedded mode.");
        }
    }
    return queries;
}
Also used : PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) Injector(io.confluent.ksql.statement.Injector) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 2 with Injector

use of io.confluent.ksql.statement.Injector in project ksql by confluentinc.

the class StandaloneExecutor method processesQueryFile.

private void processesQueryFile(final String queries) {
    final List<ParsedStatement> preparedStatements = ksqlEngine.parse(queries);
    validateStatements(preparedStatements);
    final Injector injector = injectorFactory.apply(ksqlEngine, serviceContext);
    executeStatements(preparedStatements, new StatementExecutor(serviceContext, ksqlEngine, injector, ksqlConfig));
    ksqlEngine.getPersistentQueries().forEach(QueryMetadata::start);
}
Also used : PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) Injector(io.confluent.ksql.statement.Injector) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement)

Example 3 with Injector

use of io.confluent.ksql.statement.Injector in project ksql by confluentinc.

the class StandaloneExecutor method validateStatements.

private void validateStatements(final List<ParsedStatement> statements) {
    final KsqlExecutionContext sandboxEngine = ksqlEngine.createSandbox(serviceContext);
    final Injector injector = injectorFactory.apply(sandboxEngine, sandboxEngine.getServiceContext());
    final StatementExecutor sandboxExecutor = new StatementExecutor(sandboxEngine.getServiceContext(), sandboxEngine, injector, ksqlConfig);
    final boolean hasQueries = executeStatements(statements, sandboxExecutor);
    if (failOnNoQueries && !hasQueries) {
        throw new KsqlException("The SQL file does not contain any persistent queries. " + "i.e. it contains no 'INSERT INTO', 'CREATE TABLE x AS SELECT' or " + "'CREATE STREAM x AS SELECT' style statements.");
    }
}
Also used : Injector(io.confluent.ksql.statement.Injector) KsqlException(io.confluent.ksql.util.KsqlException) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext)

Example 4 with Injector

use of io.confluent.ksql.statement.Injector in project ksql by confluentinc.

the class RequestValidator method validate.

/**
 * Validates the messages against a snapshot in time of the KSQL engine.
 *
 * @param statements          the list of statements to validate
 * @param sessionProperties   session properties for this validation
 * @param sql                 the sql that generated the list of statements, used for
 *                            generating more useful debugging information
 *
 * @return the number of new persistent queries that would be created by {@code statements}
 * @throws KsqlException if any of the statements cannot be validated, or the number
 *                       of requested statements would cause the execution context
 *                       to exceed the number of persistent queries that it was configured
 *                       to support
 */
public int validate(final ServiceContext serviceContext, final List<ParsedStatement> statements, final SessionProperties sessionProperties, final String sql) {
    requireSandbox(serviceContext);
    final KsqlExecutionContext ctx = requireSandbox(snapshotSupplier.apply(serviceContext));
    final Injector injector = injectorFactory.apply(ctx, serviceContext);
    final KsqlConfig ksqlConfig = ctx.getKsqlConfig();
    int numPersistentQueries = 0;
    for (final ParsedStatement parsed : statements) {
        final PreparedStatement<?> prepared = ctx.prepare(parsed, (isVariableSubstitutionEnabled(sessionProperties, ksqlConfig) ? sessionProperties.getSessionVariables() : Collections.emptyMap()));
        final ConfiguredStatement<?> configured = ConfiguredStatement.of(prepared, SessionConfig.of(ksqlConfig, sessionProperties.getMutableScopedProperties()));
        numPersistentQueries += validate(serviceContext, configured, sessionProperties, ctx, injector);
        if (QueryCapacityUtil.exceedsPersistentQueryCapacity(ctx, ksqlConfig)) {
            QueryCapacityUtil.throwTooManyActivePersistentQueriesException(ctx, ksqlConfig, sql);
        }
    }
    return numPersistentQueries;
}
Also used : Injector(io.confluent.ksql.statement.Injector) KsqlConfig(io.confluent.ksql.util.KsqlConfig) ParsedStatement(io.confluent.ksql.parser.KsqlParser.ParsedStatement) KsqlExecutionContext(io.confluent.ksql.KsqlExecutionContext)

Aggregations

Injector (io.confluent.ksql.statement.Injector)4 KsqlExecutionContext (io.confluent.ksql.KsqlExecutionContext)3 ParsedStatement (io.confluent.ksql.parser.KsqlParser.ParsedStatement)3 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)2 QueryMetadata (io.confluent.ksql.util.QueryMetadata)2 KsqlConfig (io.confluent.ksql.util.KsqlConfig)1 KsqlException (io.confluent.ksql.util.KsqlException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1