Search in sources :

Example 1 with ErrorMessageEntity

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

the class Cli method printKsqlResponse.

private void printKsqlResponse(RestResponse<KsqlEntityList> response) throws IOException {
    if (response.isSuccessful()) {
        KsqlEntityList ksqlEntities = response.getResponse();
        boolean noErrorFromServer = true;
        for (KsqlEntity entity : ksqlEntities) {
            if (entity instanceof ErrorMessageEntity) {
                ErrorMessageEntity errorMsg = (ErrorMessageEntity) entity;
                terminal.printErrorMessage(errorMsg.getErrorMessage());
                LOGGER.error(errorMsg.getErrorMessage().getMessage());
                noErrorFromServer = false;
            } else if (entity instanceof CommandStatusEntity && (((CommandStatusEntity) entity).getCommandStatus().getStatus() == CommandStatus.Status.ERROR)) {
                String fullMessage = ((CommandStatusEntity) entity).getCommandStatus().getMessage();
                terminal.printError(fullMessage.split("\n")[0], fullMessage);
                noErrorFromServer = false;
            }
        }
        if (noErrorFromServer) {
            terminal.printKsqlEntityList(response.getResponse());
        }
    } else {
        terminal.printErrorMessage(response.getErrorMessage());
    }
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) ErrorMessageEntity(io.confluent.ksql.rest.entity.ErrorMessageEntity) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) CommandStatusEntity(io.confluent.ksql.rest.entity.CommandStatusEntity)

Example 2 with ErrorMessageEntity

use of io.confluent.ksql.rest.entity.ErrorMessageEntity 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 3 with ErrorMessageEntity

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

the class KsqlResource method handleKsqlStatements.

@POST
public Response handleKsqlStatements(KsqlRequest request) throws Exception {
    KsqlEntityList result = new KsqlEntityList();
    try {
        List<Statement> parsedStatements = ksqlEngine.getStatements(request.getKsql());
        List<String> statementStrings = getStatementStrings(request.getKsql());
        Map<String, Object> streamsProperties = request.getStreamsProperties();
        if (parsedStatements.size() != statementStrings.size()) {
            throw new Exception(String.format("Size of parsed statements and statement strings differ; %d vs. %d, respectively", parsedStatements.size(), statementStrings.size()));
        }
        for (int i = 0; i < parsedStatements.size(); i++) {
            String statementText = statementStrings.get(i);
            result.add(executeStatement(statementText, parsedStatements.get(i), streamsProperties));
        }
    } catch (Exception exception) {
        log.error("Failed to handle POST:" + request, exception);
        result.add(new ErrorMessageEntity(request.getKsql(), exception));
    }
    return Response.ok(result).build();
}
Also used : KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) AbstractStreamCreateStatement(io.confluent.ksql.parser.tree.AbstractStreamCreateStatement) Statement(io.confluent.ksql.parser.tree.Statement) DdlStatement(io.confluent.ksql.parser.tree.DdlStatement) ErrorMessageEntity(io.confluent.ksql.rest.entity.ErrorMessageEntity) TimeoutException(java.util.concurrent.TimeoutException) KsqlException(io.confluent.ksql.util.KsqlException) POST(javax.ws.rs.POST)

Example 4 with ErrorMessageEntity

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

the class KsqlResourceTest method shouldFailForIncorrectCSASStatementResultType.

@Test
public void shouldFailForIncorrectCSASStatementResultType() throws Exception {
    KsqlResource testResource = TestKsqlResourceUtil.get(ksqlEngine, ksqlRestConfig);
    String ksqlString1 = "CREATE STREAM s1 AS SELECT * FROM test_table;";
    Response response1 = testResource.handleKsqlStatements(new KsqlRequest(ksqlString1, Collections.emptyMap()));
    KsqlEntityList result1 = (KsqlEntityList) response1.getEntity();
    assertTrue("Incorrect response size.", result1.size() == 1);
    assertThat(result1.get(0), instanceOf(ErrorMessageEntity.class));
    ErrorMessageEntity errorMessageEntity1 = (ErrorMessageEntity) result1.get(0);
    assertThat("", errorMessageEntity1.getErrorMessage().getMessage(), equalTo("Invalid result type. Your SELECT query produces a TABLE. Please use CREATE TABLE AS SELECT statement instead."));
    String ksqlString2 = "CREATE STREAM s2 AS SELECT S2_F1 , count(S2_F1) FROM test_stream group by " + "s2_f1;";
    Response response2 = testResource.handleKsqlStatements(new KsqlRequest(ksqlString2, Collections.emptyMap()));
    KsqlEntityList result2 = (KsqlEntityList) response2.getEntity();
    assertThat("Incorrect response size.", result2.size(), equalTo(1));
    assertThat(result2.get(0), instanceOf(ErrorMessageEntity.class));
    ErrorMessageEntity errorMessageEntity2 = (ErrorMessageEntity) result2.get(0);
    assertThat("", errorMessageEntity2.getErrorMessage().getMessage(), equalTo("Invalid " + "result type. Your SELECT query produces a TABLE. Please use CREATE TABLE AS SELECT statement instead."));
}
Also used : Response(javax.ws.rs.core.Response) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) ErrorMessageEntity(io.confluent.ksql.rest.entity.ErrorMessageEntity) Test(org.junit.Test)

Example 5 with ErrorMessageEntity

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

the class KsqlResourceTest method shouldFailForIncorrectDropStreamStatement.

@Test
public void shouldFailForIncorrectDropStreamStatement() throws Exception {
    KsqlResource testResource = TestKsqlResourceUtil.get(ksqlEngine, ksqlRestConfig);
    final String ksqlString = "DROP TABLE test_stream;";
    Response response = testResource.handleKsqlStatements(new KsqlRequest(ksqlString, Collections.emptyMap()));
    KsqlEntityList result = (KsqlEntityList) response.getEntity();
    assertThat("Incorrect drop statement.", result.size(), equalTo(1));
    assertThat(result.get(0), instanceOf(ErrorMessageEntity.class));
    ErrorMessageEntity errorMessageEntity = (ErrorMessageEntity) result.get(0);
    assertTrue(errorMessageEntity.getErrorMessage().getMessage().equalsIgnoreCase("Incompatible data source type" + " is STREAM, but statement was DROP TABLE"));
}
Also used : Response(javax.ws.rs.core.Response) KsqlEntityList(io.confluent.ksql.rest.entity.KsqlEntityList) KsqlRequest(io.confluent.ksql.rest.entity.KsqlRequest) ErrorMessageEntity(io.confluent.ksql.rest.entity.ErrorMessageEntity) Test(org.junit.Test)

Aggregations

ErrorMessageEntity (io.confluent.ksql.rest.entity.ErrorMessageEntity)8 KsqlEntityList (io.confluent.ksql.rest.entity.KsqlEntityList)8 Test (org.junit.Test)5 KsqlRequest (io.confluent.ksql.rest.entity.KsqlRequest)4 Response (javax.ws.rs.core.Response)4 CommandStatusEntity (io.confluent.ksql.rest.entity.CommandStatusEntity)3 ExecutionPlan (io.confluent.ksql.rest.entity.ExecutionPlan)2 KafkaTopicInfo (io.confluent.ksql.rest.entity.KafkaTopicInfo)2 KafkaTopicsList (io.confluent.ksql.rest.entity.KafkaTopicsList)2 KsqlEntity (io.confluent.ksql.rest.entity.KsqlEntity)2 KsqlTopicInfo (io.confluent.ksql.rest.entity.KsqlTopicInfo)2 KsqlTopicsList (io.confluent.ksql.rest.entity.KsqlTopicsList)2 PropertiesList (io.confluent.ksql.rest.entity.PropertiesList)2 Queries (io.confluent.ksql.rest.entity.Queries)2 SourceDescription (io.confluent.ksql.rest.entity.SourceDescription)2 StreamsList (io.confluent.ksql.rest.entity.StreamsList)2 TablesList (io.confluent.ksql.rest.entity.TablesList)2 TopicDescription (io.confluent.ksql.rest.entity.TopicDescription)2 ArrayList (java.util.ArrayList)2 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1