use of io.confluent.ksql.api.client.InsertsPublisher 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));
}
use of io.confluent.ksql.api.client.InsertsPublisher in project ksql by confluentinc.
the class ClientMutationIntegrationTest method shouldHandleErrorResponseFromStreamInserts.
@Test
public void shouldHandleErrorResponseFromStreamInserts() {
// When
final Exception e = assertThrows(// thrown from .get() when the future completes exceptionally
ExecutionException.class, () -> client.streamInserts(NON_EXIST_TABLE, new InsertsPublisher()).get());
// Then
assertThat(e.getCause(), instanceOf(KsqlClientException.class));
assertThat(e.getCause().getMessage(), containsString("Received 400 response from server"));
assertThat(e.getCause().getMessage(), containsString("Cannot insert values into an unknown stream/table"));
}
Aggregations