Search in sources :

Example 21 with QueryResponse

use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.

the class ApiIntegrationTest method shouldExecutePushQueryNoLimit.

@Test
public void shouldExecutePushQueryNoLimit() throws Exception {
    KsqlEngine engine = (KsqlEngine) REST_APP.getEngine();
    // One persistent query for the agg table
    assertThatEventually(engine::numberOfLiveQueries, is(1));
    // Given:
    String sql = "SELECT * from " + TEST_STREAM + " EMIT CHANGES;";
    // Create a write stream to capture the incomplete response
    ReceiveStream writeStream = new ReceiveStream(vertx);
    // Make the request to stream a query
    JsonObject properties = new JsonObject();
    JsonObject requestBody = new JsonObject().put("sql", sql).put("properties", properties);
    VertxCompletableFuture<HttpResponse<Void>> responseFuture = new VertxCompletableFuture<>();
    client.post("/query-stream").as(BodyCodec.pipe(writeStream)).sendJsonObject(requestBody, responseFuture);
    assertThatEventually(engine::numberOfLiveQueries, is(2));
    // Wait for all rows in the response to arrive
    assertThatEventually(() -> {
        try {
            Buffer buff = writeStream.getBody();
            QueryResponse queryResponse = new QueryResponse(buff.toString());
            return queryResponse.rows.size();
        } catch (Throwable t) {
            return -1;
        }
    }, greaterThanOrEqualTo(6));
    // The response shouldn't have ended yet
    assertThat(writeStream.isEnded(), is(false));
    QueryResponse queryResponse = new QueryResponse(writeStream.getBody().toString());
    String queryId = queryResponse.responseObject.getString("queryId");
    // Now send another request to close the query
    JsonObject closeQueryRequestBody = new JsonObject().put("queryId", queryId);
    HttpResponse<Buffer> closeQueryResponse = sendRequest(client, "/close-query", closeQueryRequestBody.toBuffer());
    assertThat(closeQueryResponse.statusCode(), is(200));
    // The response should now be ended
    assertThatEventually(writeStream::isEnded, is(true));
    HttpResponse<Void> response = responseFuture.get();
    assertThat(response.statusCode(), is(200));
    // Make sure it's cleaned up on the server
    assertThatEventually(engine::numberOfLiveQueries, is(1));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) ReceiveStream(io.confluent.ksql.api.utils.ReceiveStream) JsonObject(io.vertx.core.json.JsonObject) HttpResponse(io.vertx.ext.web.client.HttpResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) VertxCompletableFuture(io.confluent.ksql.util.VertxCompletableFuture) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 22 with QueryResponse

use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.

the class ApiIntegrationTest method shouldExecutePushQueryWithVariableSubstitution.

@Test
public void shouldExecutePushQueryWithVariableSubstitution() {
    // Given:
    String sql = "SELECT DEC AS ${name} from " + TEST_STREAM + " EMIT CHANGES LIMIT 2;";
    // When:
    QueryResponse response = executeQueryWithVariables(sql, new JsonObject().put("name", "COL"));
    // Then:
    assertThat(response.rows, hasSize(2));
    assertThat(response.responseObject.getJsonArray("columnNames"), is(new JsonArray().add("COL")));
    assertThat(response.responseObject.getJsonArray("columnTypes"), is(new JsonArray().add("DECIMAL(4, 2)")));
    assertThat(response.responseObject.getString("queryId"), is(notNullValue()));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) JsonObject(io.vertx.core.json.JsonObject) Matchers.containsString(org.hamcrest.Matchers.containsString) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 23 with QueryResponse

use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.

the class ApiIntegrationTest method shouldExecutePushQueryWithLimit.

@Test
public void shouldExecutePushQueryWithLimit() {
    // Given:
    String sql = "SELECT * from " + TEST_STREAM + " EMIT CHANGES LIMIT " + 2 + ";";
    // When:
    QueryResponse response = executeQuery(sql);
    // Then:
    assertThat(response.rows, hasSize(2));
    assertThat(response.responseObject.getJsonArray("columnNames"), is(new JsonArray().add("K").add("STR").add("LONG").add("DEC").add("BYTES_").add("ARRAY").add("MAP").add("STRUCT").add("COMPLEX").add("TIMESTAMP").add("DATE").add("TIME").add("HEAD")));
    assertThat(response.responseObject.getJsonArray("columnTypes"), is(new JsonArray().add("STRUCT<`F1` ARRAY<STRING>>").add("STRING").add("BIGINT").add("DECIMAL(4, 2)").add("BYTES").add("ARRAY<STRING>").add("MAP<STRING, STRING>").add("STRUCT<`F1` INTEGER>").add("STRUCT<`DECIMAL` DECIMAL(2, 1), `STRUCT` STRUCT<`F1` STRING, `F2` INTEGER>, " + "`ARRAY_ARRAY` ARRAY<ARRAY<STRING>>, `ARRAY_STRUCT` ARRAY<STRUCT<`F1` STRING>>, " + "`ARRAY_MAP` ARRAY<MAP<STRING, INTEGER>>, `MAP_ARRAY` MAP<STRING, ARRAY<STRING>>, " + "`MAP_MAP` MAP<STRING, MAP<STRING, INTEGER>>, `MAP_STRUCT` MAP<STRING, STRUCT<`F1` STRING>>>").add("TIMESTAMP").add("DATE").add("TIME").add("BYTES")));
    assertThat(response.responseObject.getString("queryId"), is(notNullValue()));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 24 with QueryResponse

use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.

the class ApiIntegrationTest method shouldExecutePullQueryWithVariableSubstitution.

@Test
public void shouldExecutePullQueryWithVariableSubstitution() {
    // Given:
    String sql = "SELECT * from ${AGG_TABLE} WHERE K=" + AN_AGG_KEY + ";";
    final JsonObject variables = new JsonObject().put("AGG_TABLE", AGG_TABLE);
    // When:
    // Maybe need to retry as populating agg table is async
    AtomicReference<QueryResponse> atomicReference = new AtomicReference<>();
    assertThatEventually(() -> {
        QueryResponse queryResponse = executeQueryWithVariables(sql, variables);
        atomicReference.set(queryResponse);
        return queryResponse.rows;
    }, hasSize(1));
    QueryResponse response = atomicReference.get();
    // Then:
    JsonArray expectedColumnNames = new JsonArray().add("K").add("LONG");
    JsonArray expectedColumnTypes = new JsonArray().add("STRUCT<`F1` ARRAY<STRING>>").add("BIGINT");
    assertThat(response.rows, hasSize(1));
    assertThat(response.responseObject.getJsonArray("columnNames"), is(expectedColumnNames));
    assertThat(response.responseObject.getJsonArray("columnTypes"), is(expectedColumnTypes));
    assertThat(response.responseObject.getString("queryId"), startsWith("query_"));
    // rowkey
    assertThat(response.rows.get(0).getJsonObject(0).getJsonArray("F1").getString(0), is("a"));
    // latest_by_offset(long)
    assertThat(response.rows.get(0).getLong(1), is(1L));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) JsonObject(io.vertx.core.json.JsonObject) AtomicReference(java.util.concurrent.atomic.AtomicReference) Matchers.containsString(org.hamcrest.Matchers.containsString) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 25 with QueryResponse

use of io.confluent.ksql.api.utils.QueryResponse in project ksql by confluentinc.

the class RestApiTest method shouldExecutePullQueryOverHttp2QueryStream.

@Test
public void shouldExecutePullQueryOverHttp2QueryStream() {
    QueryStreamArgs queryStreamArgs = new QueryStreamArgs("SELECT COUNT, USERID from " + AGG_TABLE + " WHERE USERID='" + AN_AGG_KEY + "';", Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
    QueryResponse[] queryResponse = new QueryResponse[1];
    assertThatEventually(() -> {
        try {
            HttpResponse<Buffer> resp = RestIntegrationTestUtil.rawRestRequest(REST_APP, HTTP_2, POST, "/query-stream", queryStreamArgs, "application/vnd.ksqlapi.delimited.v1", Optional.empty(), Optional.empty());
            queryResponse[0] = new QueryResponse(resp.body().toString());
            return queryResponse[0].rows.size();
        } catch (Throwable t) {
            return Integer.MAX_VALUE;
        }
    }, is(1));
    assertThat(queryResponse[0].rows.get(0).getList(), is(ImmutableList.of(1, "USER_1")));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) QueryStreamArgs(io.confluent.ksql.rest.entity.QueryStreamArgs) QueryResponse(io.confluent.ksql.api.utils.QueryResponse) Test(org.junit.Test) IntegrationTest(io.confluent.common.utils.IntegrationTest)

Aggregations

QueryResponse (io.confluent.ksql.api.utils.QueryResponse)43 Test (org.junit.Test)30 Buffer (io.vertx.core.buffer.Buffer)29 JsonObject (io.vertx.core.json.JsonObject)21 IntegrationTest (io.confluent.common.utils.IntegrationTest)10 PushQueryId (io.confluent.ksql.rest.entity.PushQueryId)8 VertxCompletableFuture (io.confluent.ksql.util.VertxCompletableFuture)7 HttpResponse (io.vertx.ext.web.client.HttpResponse)7 Matchers.containsString (org.hamcrest.Matchers.containsString)7 JsonArray (io.vertx.core.json.JsonArray)6 ReceiveStream (io.confluent.ksql.api.utils.ReceiveStream)4 ArrayList (java.util.ArrayList)4 WebClient (io.vertx.ext.web.client.WebClient)3 ImmutableList (com.google.common.collect.ImmutableList)2 KsqlEngine (io.confluent.ksql.engine.KsqlEngine)2 QueryStreamArgs (io.confluent.ksql.rest.entity.QueryStreamArgs)2 KsqlException (io.confluent.ksql.util.KsqlException)2 HttpVersion (io.vertx.core.http.HttpVersion)2 List (java.util.List)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2