Search in sources :

Example 1 with Row

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));
}
Also used : Row(io.confluent.ksql.api.client.Row) GenericRow(io.confluent.ksql.GenericRow) StreamedQueryResult(io.confluent.ksql.api.client.StreamedQueryResult) Test(org.junit.Test) IntegrationTest(io.confluent.common.utils.IntegrationTest)

Example 2 with Row

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));
}
Also used : Row(io.confluent.ksql.api.client.Row) GenericRow(io.confluent.ksql.GenericRow) StreamedQueryResult(io.confluent.ksql.api.client.StreamedQueryResult) Test(org.junit.Test) IntegrationTest(io.confluent.common.utils.IntegrationTest)

Example 3 with Row

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);
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Row(io.confluent.ksql.api.client.Row) KsqlException(io.confluent.ksql.api.client.exception.KsqlException)

Example 4 with Row

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);
    }
}
Also used : JsonArray(io.vertx.core.json.JsonArray) KsqlClientException(io.confluent.ksql.api.client.exception.KsqlClientException) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) Row(io.confluent.ksql.api.client.Row)

Example 5 with Row

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;
}
Also used : Row(io.confluent.ksql.api.client.Row)

Aggregations

Row (io.confluent.ksql.api.client.Row)27 Test (org.junit.Test)16 IntegrationTest (io.confluent.common.utils.IntegrationTest)13 StreamedQueryResult (io.confluent.ksql.api.client.StreamedQueryResult)10 GenericRow (io.confluent.ksql.GenericRow)8 BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 KsqlObject (io.confluent.ksql.api.client.KsqlObject)5 ExecutionException (java.util.concurrent.ExecutionException)5 KsqlArray (io.confluent.ksql.api.client.KsqlArray)4 KsqlClientException (io.confluent.ksql.api.client.exception.KsqlClientException)4 BigDecimal (java.math.BigDecimal)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 ZooKeeperClientException (kafka.zookeeper.ZooKeeperClientException)3 MigrationException (io.confluent.ksql.tools.migrations.MigrationException)2 JsonArray (io.vertx.core.json.JsonArray)2 JsonObject (io.vertx.core.json.JsonObject)2 ArrayList (java.util.ArrayList)2 AcksPublisher (io.confluent.ksql.api.client.AcksPublisher)1