Search in sources :

Example 1 with StreamPublisher

use of io.confluent.ksql.rest.client.StreamPublisher in project ksql by confluentinc.

the class Cli method handlePrintedTopic.

@SuppressWarnings({ "try", "unused" })
private void handlePrintedTopic(final String printTopic, final SqlBaseParser.PrintTopicContext ignored) {
    final RestResponse<StreamPublisher<String>> topicResponse = makeKsqlRequest(printTopic, restClient::makePrintTopicRequest);
    if (topicResponse.isSuccessful()) {
        try (StatusClosable toClose = terminal.setStatusMessage("Press CTRL-C to interrupt")) {
            final CompletableFuture<Void> future = new CompletableFuture<>();
            final StreamPublisher<String> publisher = topicResponse.getResponse();
            final PrintTopicSubscriber subscriber = new PrintTopicSubscriber(publisher.getContext(), future);
            publisher.subscribe(subscriber);
            terminal.handle(Terminal.Signal.INT, signal -> {
                terminal.handle(Terminal.Signal.INT, Terminal.SignalHandler.SIG_IGN);
                subscriber.close();
                future.complete(null);
            });
            try {
                future.get();
            } catch (Exception e) {
                LOGGER.error("Unexpected exception in waiting for print topic completion", e);
            } finally {
                publisher.close();
            }
            terminal.writer().println("Topic printing ceased");
        }
    } else {
        terminal.writer().println(topicResponse.getErrorMessage().getMessage());
    }
    terminal.flush();
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) StreamPublisher(io.confluent.ksql.rest.client.StreamPublisher) StatusClosable(io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable) VertxException(io.vertx.core.VertxException) KsqlRestClientException(io.confluent.ksql.rest.client.exception.KsqlRestClientException) EndOfFileException(org.jline.reader.EndOfFileException) ExecutionException(java.util.concurrent.ExecutionException) KsqlUnsupportedServerException(io.confluent.ksql.rest.client.exception.KsqlUnsupportedServerException) KsqlMissingCredentialsException(io.confluent.ksql.rest.client.exception.KsqlMissingCredentialsException) UserInterruptException(org.jline.reader.UserInterruptException)

Example 2 with StreamPublisher

use of io.confluent.ksql.rest.client.StreamPublisher in project ksql by confluentinc.

the class Cli method handleQuery.

// ignored param is required to compile.
@SuppressWarnings({ "try", "unused" })
private void handleQuery(final String statement, final SqlBaseParser.QueryStatementContext query) {
    final RestResponse<StreamPublisher<StreamedRow>> queryResponse = makeKsqlRequest(statement, restClient::makeQueryRequestStreamed);
    if (!queryResponse.isSuccessful()) {
        terminal.printErrorMessage(queryResponse.getErrorMessage());
        terminal.flush();
    } else {
        try (StatusClosable toClose = terminal.setStatusMessage("Press CTRL-C to interrupt")) {
            final StreamPublisher<StreamedRow> publisher = queryResponse.getResponse();
            final CompletableFuture<Void> future = new CompletableFuture<>();
            final QueryStreamSubscriber subscriber = new QueryStreamSubscriber(publisher.getContext(), future);
            publisher.subscribe(subscriber);
            terminal.handle(Terminal.Signal.INT, signal -> {
                terminal.handle(Terminal.Signal.INT, Terminal.SignalHandler.SIG_IGN);
                subscriber.close();
                future.complete(null);
            });
            try {
                if (streamedQueryTimeoutMs != null) {
                    future.get(streamedQueryTimeoutMs, TimeUnit.MILLISECONDS);
                } else {
                    future.get();
                }
            } catch (Exception e) {
                LOGGER.error("Unexpected exception in waiting for query", e);
            } finally {
                terminal.writer().println("Query terminated");
                terminal.flush();
                publisher.close();
            }
        }
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) StreamPublisher(io.confluent.ksql.rest.client.StreamPublisher) StatusClosable(io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable) VertxException(io.vertx.core.VertxException) KsqlRestClientException(io.confluent.ksql.rest.client.exception.KsqlRestClientException) EndOfFileException(org.jline.reader.EndOfFileException) ExecutionException(java.util.concurrent.ExecutionException) KsqlUnsupportedServerException(io.confluent.ksql.rest.client.exception.KsqlUnsupportedServerException) KsqlMissingCredentialsException(io.confluent.ksql.rest.client.exception.KsqlMissingCredentialsException) UserInterruptException(org.jline.reader.UserInterruptException)

Example 3 with StreamPublisher

use of io.confluent.ksql.rest.client.StreamPublisher in project ksql by confluentinc.

the class ConsistencyOffsetVectorFunctionalTest method shouldRoundTripCVWhenPullQueryHttp1.

@Test(timeout = 120000L)
public void shouldRoundTripCVWhenPullQueryHttp1() throws Exception {
    // Given
    final KsqlRestClient ksqlRestClient = REST_APP.buildKsqlClient(Optional.empty(), ConsistencyLevel.MONOTONIC_SESSION);
    // When
    final RestResponse<StreamPublisher<StreamedRow>> response = ksqlRestClient.makeQueryRequestStreamed(PULL_QUERY_ON_TABLE, 1L, null, null);
    final List<StreamedRow> rows = getElementsFromPublisher(4, response.getResponse());
    // Then
    assertThat(rows, hasSize(3));
    assertThat(rows.get(2).getConsistencyToken().get(), not(Optional.empty()));
    final String serialized = rows.get(2).getConsistencyToken().get().getConsistencyToken();
    verifyConsistencyVector(serialized);
}
Also used : KsqlRestClient(io.confluent.ksql.rest.client.KsqlRestClient) StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) StreamPublisher(io.confluent.ksql.rest.client.StreamPublisher) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Test(org.junit.Test)

Example 4 with StreamPublisher

use of io.confluent.ksql.rest.client.StreamPublisher in project ksql by confluentinc.

the class ConsistencyOffsetVectorFunctionalTest method shouldNotRoundTripCVHttp1.

@Test(timeout = 120000L)
public void shouldNotRoundTripCVHttp1() throws Exception {
    final KsqlRestClient ksqlRestClient = REST_APP.buildKsqlClient(Optional.empty(), ConsistencyLevel.EVENTUAL);
    final RestResponse<StreamPublisher<StreamedRow>> response = ksqlRestClient.makeQueryRequestStreamed(PULL_QUERY_ON_TABLE, 1L);
    final List<StreamedRow> rows = getElementsFromPublisher(4, response.getResponse());
    assertThat(rows, hasSize(2));
    assertThat(rows.get(0).getConsistencyToken(), is(Optional.empty()));
    assertThat(rows.get(1).getConsistencyToken(), is(Optional.empty()));
}
Also used : KsqlRestClient(io.confluent.ksql.rest.client.KsqlRestClient) StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) StreamPublisher(io.confluent.ksql.rest.client.StreamPublisher) Test(org.junit.Test)

Aggregations

StreamPublisher (io.confluent.ksql.rest.client.StreamPublisher)4 StreamedRow (io.confluent.ksql.rest.entity.StreamedRow)3 StatusClosable (io.confluent.ksql.cli.console.KsqlTerminal.StatusClosable)2 KsqlRestClient (io.confluent.ksql.rest.client.KsqlRestClient)2 KsqlMissingCredentialsException (io.confluent.ksql.rest.client.exception.KsqlMissingCredentialsException)2 KsqlRestClientException (io.confluent.ksql.rest.client.exception.KsqlRestClientException)2 KsqlUnsupportedServerException (io.confluent.ksql.rest.client.exception.KsqlUnsupportedServerException)2 VertxException (io.vertx.core.VertxException)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ExecutionException (java.util.concurrent.ExecutionException)2 EndOfFileException (org.jline.reader.EndOfFileException)2 UserInterruptException (org.jline.reader.UserInterruptException)2 Test (org.junit.Test)2 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)1