Search in sources :

Example 1 with CommandStatus

use of io.confluent.ksql.rest.entity.CommandStatus in project ksql by confluentinc.

the class Console method printAsTable.

private void printAsTable(KsqlEntity ksqlEntity) {
    List<String> header = new ArrayList<>();
    List<String> footer = new ArrayList<>();
    List<String> columnHeaders = new ArrayList<>();
    List<List<String>> rowValues = new ArrayList<>();
    if (ksqlEntity instanceof CommandStatusEntity) {
        CommandStatusEntity commandStatusEntity = (CommandStatusEntity) ksqlEntity;
        columnHeaders = Arrays.asList("Message");
        CommandStatus commandStatus = commandStatusEntity.getCommandStatus();
        rowValues = Collections.singletonList(Arrays.asList(commandStatus.getMessage().split("\n", 2)[0]));
    } else if (ksqlEntity instanceof ErrorMessageEntity) {
        ErrorMessage errorMessage = ((ErrorMessageEntity) ksqlEntity).getErrorMessage();
        printErrorMessage(errorMessage);
        return;
    } else if (ksqlEntity instanceof PropertiesList) {
        PropertiesList propertiesList = CliUtils.propertiesListWithOverrides((PropertiesList) ksqlEntity, restClient.getLocalProperties());
        Map<String, Object> properties = propertiesList.getProperties();
        columnHeaders = Arrays.asList("Property", "Value");
        rowValues = properties.entrySet().stream().map(propertyEntry -> Arrays.asList(propertyEntry.getKey(), Objects.toString(propertyEntry.getValue()))).collect(Collectors.toList());
    } else if (ksqlEntity instanceof Queries) {
        List<Queries.RunningQuery> runningQueries = ((Queries) ksqlEntity).getQueries();
        columnHeaders = Arrays.asList("Query ID", "Kafka Topic", "Query String");
        rowValues = runningQueries.stream().map(runningQuery -> Arrays.asList(runningQuery.getId().toString(), runningQuery.getKafkaTopic(), runningQuery.getQueryString())).collect(Collectors.toList());
        footer.add("For detailed information on a Query run: EXPLAIN <Query ID>;");
    } else if (ksqlEntity instanceof SourceDescription) {
        SourceDescription sourceDescription = (SourceDescription) ksqlEntity;
        List<SourceDescription.FieldSchemaInfo> fields = sourceDescription.getSchema();
        if (!fields.isEmpty()) {
            columnHeaders = Arrays.asList("Field", "Type");
            rowValues = fields.stream().map(field -> Arrays.asList(field.getName(), formatFieldType(field, sourceDescription.getKey()))).collect(Collectors.toList());
        }
        printExtendedInformation(header, footer, sourceDescription);
    } else if (ksqlEntity instanceof TopicDescription) {
        columnHeaders = new ArrayList<>();
        columnHeaders.add("Topic Name");
        columnHeaders.add("Kafka Topic");
        columnHeaders.add("Type");
        List<String> topicInfo = new ArrayList<>();
        TopicDescription topicDescription = (TopicDescription) ksqlEntity;
        topicInfo.add(topicDescription.getName());
        topicInfo.add(topicDescription.getKafkaTopic());
        topicInfo.add(topicDescription.getFormat());
        if (topicDescription.getFormat().equalsIgnoreCase("AVRO")) {
            columnHeaders.add("AvroSchema");
            topicInfo.add(topicDescription.getSchemaString());
        }
        rowValues = Arrays.asList(topicInfo);
    } else if (ksqlEntity instanceof StreamsList) {
        List<SourceInfo.Stream> streamInfos = ((StreamsList) ksqlEntity).getStreams();
        columnHeaders = Arrays.asList("Stream Name", "Kafka Topic", "Format");
        rowValues = streamInfos.stream().map(streamInfo -> Arrays.asList(streamInfo.getName(), streamInfo.getTopic(), streamInfo.getFormat())).collect(Collectors.toList());
    } else if (ksqlEntity instanceof TablesList) {
        List<SourceInfo.Table> tableInfos = ((TablesList) ksqlEntity).getTables();
        columnHeaders = Arrays.asList("Table Name", "Kafka Topic", "Format", "Windowed");
        rowValues = tableInfos.stream().map(tableInfo -> Arrays.asList(tableInfo.getName(), tableInfo.getTopic(), tableInfo.getFormat(), Boolean.toString(tableInfo.getIsWindowed()))).collect(Collectors.toList());
    } else if (ksqlEntity instanceof KsqlTopicsList) {
        List<KsqlTopicInfo> topicInfos = ((KsqlTopicsList) ksqlEntity).getTopics();
        columnHeaders = Arrays.asList("Ksql Topic", "Kafka Topic", "Format");
        rowValues = topicInfos.stream().map(topicInfo -> Arrays.asList(topicInfo.getName(), topicInfo.getKafkaTopic(), topicInfo.getFormat().name())).collect(Collectors.toList());
    } else if (ksqlEntity instanceof KafkaTopicsList) {
        List<KafkaTopicInfo> topicInfos = ((KafkaTopicsList) ksqlEntity).getTopics();
        columnHeaders = Arrays.asList("Kafka Topic", "Registered", "Partitions", "Partition Replicas", "Consumers", "Consumer Groups");
        rowValues = topicInfos.stream().map(topicInfo -> Arrays.asList(topicInfo.getName(), Boolean.toString(topicInfo.getRegistered()), Integer.toString(topicInfo.getReplicaInfo().size()), getTopicReplicaInfo(topicInfo.getReplicaInfo()), Integer.toString(topicInfo.getConsumerCount()), Integer.toString(topicInfo.getConsumerGroupCount()))).collect(Collectors.toList());
    } else if (ksqlEntity instanceof ExecutionPlan) {
        ExecutionPlan executionPlan = (ExecutionPlan) ksqlEntity;
        columnHeaders = Arrays.asList("Execution Plan");
        rowValues = Collections.singletonList(Arrays.asList(executionPlan.getExecutionPlan()));
    } else {
        throw new RuntimeException(String.format("Unexpected KsqlEntity class: '%s'", ksqlEntity.getClass().getCanonicalName()));
    }
    printTable(columnHeaders, rowValues, header, footer);
}
Also used : Arrays(java.util.Arrays) StreamsList(io.confluent.ksql.rest.entity.StreamsList) StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) LoggerFactory(org.slf4j.LoggerFactory) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) KafkaTopicsList(io.confluent.ksql.rest.entity.KafkaTopicsList) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) ErrorMessage(io.confluent.ksql.rest.entity.ErrorMessage) KsqlTopicInfo(io.confluent.ksql.rest.entity.KsqlTopicInfo) ServerInfo(io.confluent.ksql.rest.entity.ServerInfo) KsqlRestClient(io.confluent.ksql.rest.client.KsqlRestClient) InfoCmp(org.jline.utils.InfoCmp) CliUtils(io.confluent.ksql.util.CliUtils) Map(java.util.Map) ExecutionPlan(io.confluent.ksql.rest.entity.ExecutionPlan) Queries(io.confluent.ksql.rest.entity.Queries) TablesList(io.confluent.ksql.rest.entity.TablesList) SchemaMapper(io.confluent.ksql.rest.entity.SchemaMapper) Terminal(org.jline.terminal.Terminal) PrintWriter(java.io.PrintWriter) Logger(org.slf4j.Logger) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) PropertiesList(io.confluent.ksql.rest.entity.PropertiesList) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Objects(java.util.Objects) KsqlTopicsList(io.confluent.ksql.rest.entity.KsqlTopicsList) SourceInfo(io.confluent.ksql.rest.entity.SourceInfo) ErrorMessageEntity(io.confluent.ksql.rest.entity.ErrorMessageEntity) List(java.util.List) TopicDescription(io.confluent.ksql.rest.entity.TopicDescription) StringUtil(io.confluent.ksql.util.StringUtil) EndOfFileException(org.jline.reader.EndOfFileException) GenericRow(io.confluent.ksql.GenericRow) Closeable(java.io.Closeable) KafkaTopicInfo(io.confluent.ksql.rest.entity.KafkaTopicInfo) Comparator(java.util.Comparator) Collections(java.util.Collections) Queries(io.confluent.ksql.rest.entity.Queries) SourceInfo(io.confluent.ksql.rest.entity.SourceInfo) KafkaTopicsList(io.confluent.ksql.rest.entity.KafkaTopicsList) ArrayList(java.util.ArrayList) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) ExecutionPlan(io.confluent.ksql.rest.entity.ExecutionPlan) StreamsList(io.confluent.ksql.rest.entity.StreamsList) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) KafkaTopicsList(io.confluent.ksql.rest.entity.KafkaTopicsList) ArrayList(java.util.ArrayList) TablesList(io.confluent.ksql.rest.entity.TablesList) PropertiesList(io.confluent.ksql.rest.entity.PropertiesList) KsqlTopicsList(io.confluent.ksql.rest.entity.KsqlTopicsList) List(java.util.List) StreamsList(io.confluent.ksql.rest.entity.StreamsList) KsqlTopicInfo(io.confluent.ksql.rest.entity.KsqlTopicInfo) ErrorMessageEntity(io.confluent.ksql.rest.entity.ErrorMessageEntity) PropertiesList(io.confluent.ksql.rest.entity.PropertiesList) TablesList(io.confluent.ksql.rest.entity.TablesList) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) TopicDescription(io.confluent.ksql.rest.entity.TopicDescription) ErrorMessage(io.confluent.ksql.rest.entity.ErrorMessage) KsqlTopicsList(io.confluent.ksql.rest.entity.KsqlTopicsList) SourceDescription(io.confluent.ksql.rest.entity.SourceDescription)

Example 2 with CommandStatus

use of io.confluent.ksql.rest.entity.CommandStatus in project ksql by confluentinc.

the class StatementExecutor method executeStatement.

private void executeStatement(Statement statement, Command command, CommandId commandId, Map<QueryId, CommandId> terminatedQueries, boolean wasDropped) throws Exception {
    String statementStr = command.getStatement();
    DdlCommandResult result = null;
    String successMessage = "";
    if (statement instanceof DdlStatement) {
        result = ksqlEngine.executeDdlStatement(statementStr, (DdlStatement) statement, command.getKsqlProperties());
    } else if (statement instanceof CreateAsSelect) {
        successMessage = handleCreateAsSelect((CreateAsSelect) statement, command, commandId, terminatedQueries, statementStr, wasDropped);
        if (successMessage == null) {
            return;
        }
    } else if (statement instanceof TerminateQuery) {
        terminateQuery((TerminateQuery) statement);
        successMessage = "Query terminated.";
    } else if (statement instanceof RunScript) {
        handleRunScript(command);
    } else {
        throw new Exception(String.format("Unexpected statement type: %s", statement.getClass().getName()));
    }
    // TODO: change to unified return message
    CommandStatus successStatus = new CommandStatus(CommandStatus.Status.SUCCESS, result != null ? result.getMessage() : successMessage);
    statusStore.put(commandId, successStatus);
    completeStatusFuture(commandId, successStatus);
}
Also used : DdlCommandResult(io.confluent.ksql.ddl.commands.DdlCommandResult) TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) RunScript(io.confluent.ksql.parser.tree.RunScript) CreateAsSelect(io.confluent.ksql.parser.tree.CreateAsSelect) WakeupException(org.apache.kafka.common.errors.WakeupException) KsqlException(io.confluent.ksql.util.KsqlException)

Example 3 with CommandStatus

use of io.confluent.ksql.rest.entity.CommandStatus in project ksql by confluentinc.

the class StatementExecutor method startQuery.

private boolean startQuery(String queryString, Query query, CommandId commandId, Map<QueryId, CommandId> terminatedQueries, Command command, boolean wasDropped) throws Exception {
    if (query.getQueryBody() instanceof QuerySpecification) {
        QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
        Relation into = querySpecification.getInto();
        if (into instanceof Table) {
            Table table = (Table) into;
            if (ksqlEngine.getMetaStore().getSource(table.getName().getSuffix()) != null) {
                throw new Exception(String.format("Sink specified in INTO clause already exists: %s", table.getName().getSuffix().toUpperCase()));
            }
        }
    }
    QueryMetadata queryMetadata = ksqlEngine.buildMultipleQueries(queryString, command.getKsqlProperties()).get(0);
    if (queryMetadata instanceof PersistentQueryMetadata) {
        PersistentQueryMetadata persistentQueryMetadata = (PersistentQueryMetadata) queryMetadata;
        final QueryId queryId = persistentQueryMetadata.getId();
        if (terminatedQueries != null && terminatedQueries.containsKey(queryId)) {
            CommandId terminateId = terminatedQueries.get(queryId);
            statusStore.put(terminateId, new CommandStatus(CommandStatus.Status.SUCCESS, "Termination request granted"));
            statusStore.put(commandId, new CommandStatus(CommandStatus.Status.TERMINATED, "Query terminated"));
            ksqlEngine.terminateQuery(queryId, false);
            return false;
        } else if (wasDropped) {
            ksqlEngine.terminateQuery(queryId, false);
            return false;
        } else {
            persistentQueryMetadata.getKafkaStreams().start();
            return true;
        }
    } else {
        throw new Exception(String.format("Unexpected query metadata type: %s; was expecting %s", queryMetadata.getClass().getCanonicalName(), PersistentQueryMetadata.class.getCanonicalName()));
    }
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Relation(io.confluent.ksql.parser.tree.Relation) Table(io.confluent.ksql.parser.tree.Table) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) QueryId(io.confluent.ksql.query.QueryId) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) WakeupException(org.apache.kafka.common.errors.WakeupException) KsqlException(io.confluent.ksql.util.KsqlException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 4 with CommandStatus

use of io.confluent.ksql.rest.entity.CommandStatus in project ksql by confluentinc.

the class KsqlResource method distributeStatement.

private CommandStatusEntity distributeStatement(String statementText, Statement statement, Map<String, Object> streamsProperties) throws KsqlException {
    CommandId commandId = commandStore.distributeStatement(statementText, statement, streamsProperties);
    CommandStatus commandStatus;
    try {
        commandStatus = statementExecutor.registerQueuedStatement(commandId).get(distributedCommandResponseTimeout, TimeUnit.MILLISECONDS);
    } catch (TimeoutException exception) {
        log.warn("Timeout to get commandStatus, waited {} milliseconds:, statementText:" + statementText, distributedCommandResponseTimeout, exception);
        commandStatus = statementExecutor.getStatus(commandId).get();
    } catch (Exception e) {
        throw new KsqlException(String.format("Could not write the statement '%s' into the command " + "topic.", statementText), e);
    }
    return new CommandStatusEntity(statementText, commandId, commandStatus);
}
Also used : CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) CommandId(io.confluent.ksql.rest.server.computation.CommandId) KsqlException(io.confluent.ksql.util.KsqlException) TimeoutException(java.util.concurrent.TimeoutException) KsqlException(io.confluent.ksql.util.KsqlException) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with CommandStatus

use of io.confluent.ksql.rest.entity.CommandStatus in project ksql by confluentinc.

the class StatementExecutorTest method shouldHandlePriorStatement.

@Test
public void shouldHandlePriorStatement() throws Exception {
    TestUtils testUtils = new TestUtils();
    List<Pair<CommandId, Command>> priorCommands = testUtils.getAllPriorCommandRecords();
    final RestoreCommands restoreCommands = new RestoreCommands();
    priorCommands.forEach(pair -> restoreCommands.addCommand(pair.left, pair.right));
    CommandId topicCommandId = new CommandId(CommandId.Type.TOPIC, "_CSASTopicGen", CommandId.Action.CREATE);
    CommandId csCommandId = new CommandId(CommandId.Type.STREAM, "_CSASStreamGen", CommandId.Action.CREATE);
    CommandId csasCommandId = new CommandId(CommandId.Type.STREAM, "_CSASGen", CommandId.Action.CREATE);
    CommandId ctasCommandId = new CommandId(CommandId.Type.TABLE, "_CTASGen", CommandId.Action.CREATE);
    statementExecutor.handleRestoration(restoreCommands);
    Map<CommandId, CommandStatus> statusStore = statementExecutor.getStatuses();
    Assert.assertNotNull(statusStore);
    Assert.assertEquals(4, statusStore.size());
    Assert.assertEquals(CommandStatus.Status.SUCCESS, statusStore.get(topicCommandId).getStatus());
    Assert.assertEquals(CommandStatus.Status.SUCCESS, statusStore.get(csCommandId).getStatus());
    Assert.assertEquals(CommandStatus.Status.SUCCESS, statusStore.get(csasCommandId).getStatus());
    Assert.assertEquals(CommandStatus.Status.ERROR, statusStore.get(ctasCommandId).getStatus());
}
Also used : TestUtils(io.confluent.ksql.rest.server.utils.TestUtils) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) Pair(io.confluent.ksql.util.Pair) Test(org.junit.Test)

Aggregations

CommandStatus (io.confluent.ksql.rest.entity.CommandStatus)13 CommandId (io.confluent.ksql.rest.server.computation.CommandId)5 KsqlException (io.confluent.ksql.util.KsqlException)5 WakeupException (org.apache.kafka.common.errors.WakeupException)4 CommandStatusEntity (io.confluent.ksql.rest.entity.CommandStatusEntity)3 Map (java.util.Map)3 Test (org.junit.Test)3 DdlStatement (io.confluent.ksql.parser.tree.DdlStatement)2 QueryId (io.confluent.ksql.query.QueryId)2 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)2 HashMap (java.util.HashMap)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 GenericRow (io.confluent.ksql.GenericRow)1 DdlCommandResult (io.confluent.ksql.ddl.commands.DdlCommandResult)1 CreateAsSelect (io.confluent.ksql.parser.tree.CreateAsSelect)1 Expression (io.confluent.ksql.parser.tree.Expression)1 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)1 RegisterTopic (io.confluent.ksql.parser.tree.RegisterTopic)1 Relation (io.confluent.ksql.parser.tree.Relation)1