use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class ClientIntegrationTest method shouldStreamPushQuerySync.
@Test
public void shouldStreamPushQuerySync() throws Exception {
// When
final StreamedQueryResult streamedQueryResult = client.streamQuery(PUSH_QUERY).get();
// Then
assertThat(streamedQueryResult.columnNames(), is(TEST_COLUMN_NAMES));
assertThat(streamedQueryResult.columnTypes(), is(TEST_COLUMN_TYPES));
assertThat(streamedQueryResult.queryID(), is(notNullValue()));
for (int i = 0; i < TEST_NUM_ROWS; i++) {
final Row row = streamedQueryResult.poll();
verifyStreamRowWithIndex(row, i);
}
assertThat(streamedQueryResult.isComplete(), is(false));
}
use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class ClientIntegrationTest method shouldStreamPushQueryWithLimitSync.
@Test
public void shouldStreamPushQueryWithLimitSync() throws Exception {
// When
final StreamedQueryResult streamedQueryResult = client.streamQuery(PUSH_QUERY_WITH_LIMIT).get();
// Then
assertThat(streamedQueryResult.columnNames(), is(TEST_COLUMN_NAMES));
assertThat(streamedQueryResult.columnTypes(), is(TEST_COLUMN_TYPES));
assertThat(streamedQueryResult.queryID(), is(notNullValue()));
for (int i = 0; i < PUSH_QUERY_LIMIT_NUM_ROWS; i++) {
final Row row = streamedQueryResult.poll();
verifyStreamRowWithIndex(row, i);
}
assertThat(streamedQueryResult.poll(), is(nullValue()));
assertThat(streamedQueryResult.isComplete(), is(true));
}
use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class StreamQueryResponseHandler method handleRow.
@Override
protected void handleRow(final Buffer buff) {
if (queryResult == null) {
throw new IllegalStateException("handleRow called before metadata processed");
}
final Object json = buff.toJson();
final Row row;
if (json instanceof JsonArray) {
row = new RowImpl(queryResult.columnNames(), queryResult.columnTypes(), (JsonArray) json, columnNameToIndex);
final boolean full = queryResult.accept(row);
if (full && !paused) {
recordParser.pause();
queryResult.drainHandler(this::publisherReceptive);
paused = true;
}
} else if (json instanceof JsonObject) {
final JsonObject jsonObject = (JsonObject) json;
// Don't add it to the publisher's buffer since the user should not see it
if (jsonObject.getMap() != null && jsonObject.getMap().containsKey("consistencyToken")) {
LOG.info("Response contains consistency vector " + jsonObject);
serializedConsistencyVector.set((String) ((JsonObject) json).getMap().get("consistencyToken"));
} else {
queryResult.handleError(new KsqlException(jsonObject.getString("message")));
}
} else {
throw new RuntimeException("Could not decode JSON: " + json);
}
}
use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class ExecuteQueryResponseHandler method handleRow.
@Override
protected void handleRow(final Buffer buff) {
final Row row;
final Object json = buff.toJson();
if (json instanceof JsonObject) {
final JsonObject jsonObject = (JsonObject) json;
// Don't add it to the result list since the user should not see it
if (jsonObject.getMap() != null && jsonObject.getMap().containsKey("consistencyToken")) {
log.info("Response contains consistency vector " + jsonObject);
serializedConsistencyVector.set((String) ((JsonObject) json).getMap().get("consistencyToken"));
} else {
throw new RuntimeException("Could not decode JSON, expected consistency toke: " + json);
}
} else if (json instanceof JsonArray) {
final JsonArray values = new JsonArray(buff);
if (rows.size() < maxRows) {
rows.add(new RowImpl(columnNames, columnTypes, values, columnNameToIndex));
} else {
throw new KsqlClientException("Reached max number of rows that may be returned by executeQuery(). " + "Increase the limit via ClientOptions#setExecuteQueryMaxResultRows(). " + "Current limit: " + maxRows);
}
} else {
throw new RuntimeException("Could not decode JSON: " + json);
}
}
use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class PollableSubscriber method poll.
public synchronized Row poll(final Duration timeout) {
if (closed || failed) {
return null;
}
final long timeoutNs = timeout.toNanos();
final long end;
long remainingTime;
if (timeoutNs > 0) {
end = System.nanoTime() + timeoutNs;
remainingTime = timeoutNs;
} else {
end = Long.MAX_VALUE;
remainingTime = Long.MAX_VALUE;
}
do {
// Poll in smaller units so we can exit on close
final long pollTime = Math.min(remainingTime, MAX_POLL_NANOS);
try {
final Row row = queue.poll(pollTime, TimeUnit.NANOSECONDS);
if (row != null) {
tokens.decrementAndGet();
context.runOnContext(v -> checkRequestTokens());
return row;
} else if (complete) {
// If complete, close once the queue has been emptied
close();
}
} catch (InterruptedException e) {
return null;
}
remainingTime = end - System.nanoTime();
} while (!closed && !failed && remainingTime > 0);
return null;
}
Aggregations