Search in sources :

Example 6 with Row

use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.

the class ClientMutationIntegrationTest method shouldStreamQueryWithProperties.

@Test
public void shouldStreamQueryWithProperties() throws Exception {
    // Given
    final Map<String, Object> properties = new HashMap<>();
    properties.put("auto.offset.reset", "latest");
    final String sql = "SELECT * FROM " + TEST_STREAM + " EMIT CHANGES LIMIT 1;";
    final KsqlObject insertRow = new KsqlObject().put("K", new KsqlObject().put("F1", new KsqlArray().add("my_key_shouldStreamQueryWithProperties"))).put("STR", "Value_shouldStreamQueryWithProperties").put("LONG", 2000L).put("DEC", new BigDecimal("12.34")).put("BYTES_", new byte[] { 0, 1, 2 }).put("ARRAY", new KsqlArray().add("v1_shouldStreamQueryWithProperties").add("v2_shouldStreamQueryWithProperties")).put("MAP", new KsqlObject().put("test_name", "shouldStreamQueryWithProperties")).put("STRUCT", new KsqlObject().put("F1", 4)).put("COMPLEX", COMPLEX_FIELD_VALUE).put("TIMESTAMP", "1970-01-01T00:00:00.001").put("DATE", "1970-01-01").put("TIME", "00:00:00");
    // When
    final StreamedQueryResult queryResult = client.streamQuery(sql, properties).get();
    // Then: a newly inserted row arrives
    final Row row = assertThatEventually(() -> {
        // Potentially try inserting multiple times, in case the query wasn't started by the first time
        try {
            client.insertInto(TEST_STREAM, insertRow).get();
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
        return queryResult.poll(Duration.ofMillis(10));
    }, is(notNullValue()));
    assertThat(row.getKsqlObject("K"), is(new KsqlObject().put("F1", new KsqlArray().add("my_key_shouldStreamQueryWithProperties"))));
    assertThat(row.getString("STR"), is("Value_shouldStreamQueryWithProperties"));
    assertThat(row.getLong("LONG"), is(2000L));
    assertThat(row.getDecimal("DEC"), is(new BigDecimal("12.34")));
    assertThat(row.getBytes("BYTES_"), is(new byte[] { 0, 1, 2 }));
    assertThat(row.getKsqlArray("ARRAY"), is(new KsqlArray().add("v1_shouldStreamQueryWithProperties").add("v2_shouldStreamQueryWithProperties")));
    assertThat(row.getKsqlObject("MAP"), is(new KsqlObject().put("test_name", "shouldStreamQueryWithProperties")));
    assertThat(row.getKsqlObject("STRUCT"), is(new KsqlObject().put("F1", 4)));
    assertThat(row.getKsqlObject("COMPLEX"), is(EXPECTED_COMPLEX_FIELD_VALUE));
    assertThat(row.getString("TIMESTAMP"), is("1970-01-01T00:00:00.001"));
    assertThat(row.getString("DATE"), is("1970-01-01"));
    assertThat(row.getString("TIME"), is("00:00"));
}
Also used : HashMap(java.util.HashMap) KsqlObject(io.confluent.ksql.api.client.KsqlObject) Matchers.containsString(org.hamcrest.Matchers.containsString) Row(io.confluent.ksql.api.client.Row) KsqlArray(io.confluent.ksql.api.client.KsqlArray) BigDecimal(java.math.BigDecimal) ZooKeeperClientException(kafka.zookeeper.ZooKeeperClientException) KsqlClientException(io.confluent.ksql.api.client.exception.KsqlClientException) ExecutionException(java.util.concurrent.ExecutionException) KsqlObject(io.confluent.ksql.api.client.KsqlObject) StreamedQueryResult(io.confluent.ksql.api.client.StreamedQueryResult) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 7 with Row

use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.

the class ClientMutationIntegrationTest method shouldStreamInserts.

@Test
public void shouldStreamInserts() throws Exception {
    // Given
    final InsertsPublisher insertsPublisher = new InsertsPublisher();
    final int numRows = 5;
    // When
    final AcksPublisher acksPublisher = client.streamInserts(EMPTY_TEST_STREAM_2, insertsPublisher).get();
    final TestSubscriber<InsertAck> acksSubscriber = subscribeAndWait(acksPublisher);
    assertThat(acksSubscriber.getValues(), hasSize(0));
    acksSubscriber.getSub().request(numRows);
    for (int i = 0; i < numRows; i++) {
        insertsPublisher.accept(new KsqlObject().put("K", new KsqlObject().put("F1", new KsqlArray().add("my_key_" + i))).put("STR", "TEST_" + i).put("LONG", i).put("DEC", new BigDecimal("13.31")).put("BYTES_", new byte[] { 0, 1, 2 }).put("ARRAY", new KsqlArray().add("v_" + i)).put("MAP", new KsqlObject().put("k_" + i, "v_" + i)).put("COMPLEX", COMPLEX_FIELD_VALUE).put("TIMESTAMP", "1970-01-01T00:00:00.001").put("DATE", "1970-01-01").put("TIME", "00:00"));
    }
    // Then
    assertThatEventually(acksSubscriber::getValues, hasSize(numRows));
    for (int i = 0; i < numRows; i++) {
        assertThat(acksSubscriber.getValues().get(i).seqNum(), is(Long.valueOf(i)));
    }
    assertThat(acksSubscriber.getError(), is(nullValue()));
    assertThat(acksSubscriber.isCompleted(), is(false));
    assertThat(acksPublisher.isComplete(), is(false));
    assertThat(acksPublisher.isFailed(), is(false));
    // Then: should receive new rows
    final String query = "SELECT * FROM " + EMPTY_TEST_STREAM_2 + " EMIT CHANGES LIMIT " + numRows + ";";
    final List<Row> rows = client.executeQuery(query).get();
    // Verify inserted rows are as expected
    assertThat(rows, hasSize(numRows));
    for (int i = 0; i < numRows; i++) {
        assertThat(rows.get(i).getKsqlObject("K"), is(new KsqlObject().put("F1", new KsqlArray().add("my_key_" + i))));
        assertThat(rows.get(i).getString("STR"), is("TEST_" + i));
        assertThat(rows.get(i).getLong("LONG"), is(Long.valueOf(i)));
        assertThat(rows.get(i).getDecimal("DEC"), is(new BigDecimal("13.31")));
        assertThat(rows.get(i).getBytes("BYTES_"), is(new byte[] { 0, 1, 2 }));
        assertThat(rows.get(i).getKsqlArray("ARRAY"), is(new KsqlArray().add("v_" + i)));
        assertThat(rows.get(i).getKsqlObject("MAP"), is(new KsqlObject().put("k_" + i, "v_" + i)));
        assertThat(rows.get(i).getKsqlObject("COMPLEX"), is(EXPECTED_COMPLEX_FIELD_VALUE));
        assertThat(rows.get(i).getString("TIMESTAMP"), is("1970-01-01T00:00:00.001"));
        assertThat(rows.get(i).getString("DATE"), is("1970-01-01"));
        assertThat(rows.get(i).getString("TIME"), is("00:00"));
    }
    // When: end connection
    insertsPublisher.complete();
    // Then
    assertThatEventually(acksSubscriber::isCompleted, is(true));
    assertThat(acksSubscriber.getError(), is(nullValue()));
    assertThat(acksPublisher.isComplete(), is(true));
    assertThat(acksPublisher.isFailed(), is(false));
}
Also used : AcksPublisher(io.confluent.ksql.api.client.AcksPublisher) InsertsPublisher(io.confluent.ksql.api.client.InsertsPublisher) InsertAck(io.confluent.ksql.api.client.InsertAck) Matchers.containsString(org.hamcrest.Matchers.containsString) Row(io.confluent.ksql.api.client.Row) KsqlArray(io.confluent.ksql.api.client.KsqlArray) BigDecimal(java.math.BigDecimal) KsqlObject(io.confluent.ksql.api.client.KsqlObject) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 8 with Row

use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.

the class PollableSubscriberTest method shouldSetError.

@Test
public void shouldSetError() {
    publisher = new BufferedPublisher<Row>(context, ImmutableList.of()) {

        @Override
        protected void maybeSend() {
            sendError(new RuntimeException("Error!"));
        }
    };
    publisher.subscribe(pollableSubscriber);
    Row row = pollableSubscriber.poll(POLL_DURATION);
    assertThat(row, is(nullValue()));
    assertThat(throwable, is(notNullValue()));
    assertThat(throwable.getMessage(), is("Error!"));
}
Also used : Row(io.confluent.ksql.api.client.Row) Test(org.junit.Test)

Example 9 with Row

use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.

the class PollableSubscriberTest method shouldPollRows.

private void shouldPollRows(int numRows) {
    final List<Row> rows = new ArrayList<>();
    for (int i = 0; i < numRows; i++) {
        rows.add(createRow(i));
    }
    publisher = new BufferedPublisher<>(context, rows);
    publisher.subscribe(pollableSubscriber);
    Row row = pollableSubscriber.poll(POLL_DURATION);
    int i = 0;
    for (; row != null; i++) {
        Long col1 = row.getLong(COLUMN_NAME);
        assertThat(col1, is((long) i));
        row = pollableSubscriber.poll(POLL_DURATION);
    }
    assertThat(i, is(numRows));
    assertThat(throwable, is(nullValue()));
}
Also used : ArrayList(java.util.ArrayList) Row(io.confluent.ksql.api.client.Row)

Example 10 with Row

use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.

the class StreamedQueryResultImplTest method shouldDeliverBufferedRowsIfComplete.

@Test
public void shouldDeliverBufferedRowsIfComplete() throws Exception {
    // Given
    givenPublisherAcceptsOneRow();
    completeQueryResult();
    // When
    final Row receivedRow = queryResult.poll();
    // Then
    assertThat(receivedRow, is(row));
}
Also used : Row(io.confluent.ksql.api.client.Row) Test(org.junit.Test)

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