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));
}
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()));
}
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()));
}
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));
}
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")));
}
Aggregations