use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.
the class ClientMutationIntegrationTest method shouldInsertInto.
@Test
public void shouldInsertInto() throws Exception {
// Given
final KsqlObject insertRow = new KsqlObject().put("k", // Column names are case-insensitive
new KsqlObject().put("F1", new KsqlArray().add("my_key"))).put("str", // Column names are case-insensitive
"HELLO").put("`LONG`", // Backticks may be used to preserve case-sensitivity
100L).put("\"DEC\"", // Double quotes may also be used to preserve case-sensitivity
new BigDecimal("13.31")).put("BYTES_", new byte[] { 0, 1, 2 }).put("ARRAY", new KsqlArray().add("v1").add("v2")).put("MAP", new KsqlObject().put("some_key", "a_value").put("another_key", "")).put("STRUCT", // Nested field names are case-insensitive
new KsqlObject().put("f1", 12)).put("COMPLEX", COMPLEX_FIELD_VALUE).put("TIMESTAMP", "1970-01-01T00:00:00.001").put("DATE", "1970-01-01").put("TIME", "00:00:01");
// When
// Stream name is case-insensitive
client.insertInto(EMPTY_TEST_STREAM.toLowerCase(), insertRow).get();
// Then: should receive new row
final String query = "SELECT * FROM " + EMPTY_TEST_STREAM + " EMIT CHANGES LIMIT 1;";
final List<Row> rows = client.executeQuery(query).get();
// Verify inserted row is as expected
assertThat(rows, hasSize(1));
assertThat(rows.get(0).getKsqlObject("K"), is(new KsqlObject().put("F1", new KsqlArray().add("my_key"))));
assertThat(rows.get(0).getString("STR"), is("HELLO"));
assertThat(rows.get(0).getLong("LONG"), is(100L));
assertThat(rows.get(0).getDecimal("DEC"), is(new BigDecimal("13.31")));
assertThat(rows.get(0).getBytes("BYTES_"), is(new byte[] { 0, 1, 2 }));
assertThat(rows.get(0).getKsqlArray("ARRAY"), is(new KsqlArray().add("v1").add("v2")));
assertThat(rows.get(0).getKsqlObject("MAP"), is(new KsqlObject().put("some_key", "a_value").put("another_key", "")));
assertThat(rows.get(0).getKsqlObject("STRUCT"), is(new KsqlObject().put("F1", 12)));
assertThat(rows.get(0).getKsqlObject("COMPLEX"), is(EXPECTED_COMPLEX_FIELD_VALUE));
assertThat(rows.get(0).getString("TIMESTAMP"), is("1970-01-01T00:00:00.001"));
assertThat(rows.get(0).getString("DATE"), is("1970-01-01"));
assertThat(rows.get(0).getString("TIME"), is("00:00:01"));
}
use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.
the class ClientMutationIntegrationTest method shouldHandleErrorResponseFromInsertInto.
@Test
public void shouldHandleErrorResponseFromInsertInto() {
// Given
final KsqlObject insertRow = new KsqlObject().put("K", new KsqlObject().put("F1", new KsqlArray().add("my_key"))).put("LONG", 11L);
// When
final Exception e = assertThrows(// thrown from .get() when the future completes exceptionally
ExecutionException.class, () -> client.insertInto(NON_EXIST_TABLE, insertRow).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"));
}
use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.
the class ClientIntegrationTest method verifyStreamRowWithIndex.
private static void verifyStreamRowWithIndex(final Row row, final int index) {
final KsqlArray expectedRow = TEST_EXPECTED_ROWS.get(index);
// verify metadata
assertThat(row.values(), equalTo(expectedRow));
assertThat(row.columnNames(), equalTo(TEST_COLUMN_NAMES));
assertThat(row.columnTypes(), equalTo(TEST_COLUMN_TYPES));
// verify type-based getters
assertThat(row.getKsqlObject("K"), is(expectedRow.getKsqlObject(0)));
assertThat(row.getString("STR"), is(expectedRow.getString(1)));
assertThat(row.getLong("LONG"), is(expectedRow.getLong(2)));
assertThat(row.getDecimal("DEC"), is(expectedRow.getDecimal(3)));
assertThat(row.getBytes("BYTES_"), is(expectedRow.getBytes(4)));
assertThat(row.getKsqlArray("ARRAY"), is(expectedRow.getKsqlArray(5)));
assertThat(row.getKsqlObject("MAP"), is(expectedRow.getKsqlObject(6)));
assertThat(row.getKsqlObject("STRUCT"), is(expectedRow.getKsqlObject(7)));
assertThat(row.getKsqlObject("COMPLEX"), is(expectedRow.getKsqlObject(8)));
// verify index-based getters are 1-indexed
assertThat(row.getKsqlObject(1), is(row.getKsqlObject("K")));
assertThat(row.getString(2), is(row.getString("STR")));
assertThat(row.getLong(3), is(row.getLong("LONG")));
assertThat(row.getDecimal(4), is(row.getDecimal("DEC")));
assertThat(row.getBytes(5), is(row.getBytes("BYTES_")));
assertThat(row.getKsqlArray(6), is(row.getKsqlArray("ARRAY")));
assertThat(row.getKsqlObject(7), is(row.getKsqlObject("MAP")));
assertThat(row.getKsqlObject(8), is(row.getKsqlObject("STRUCT")));
assertThat(row.getKsqlObject(9), is(row.getKsqlObject("COMPLEX")));
// verify isNull() evaluation
assertThat(row.isNull("STR"), is(false));
// verify exception on invalid cast
assertThrows(ClassCastException.class, () -> row.getInteger("STR"));
// verify KsqlArray methods
final KsqlArray values = row.values();
assertThat(values.size(), is(TEST_COLUMN_NAMES.size()));
assertThat(values.isEmpty(), is(false));
assertThat(values.getKsqlObject(0), is(row.getKsqlObject("K")));
assertThat(values.getString(1), is(row.getString("STR")));
assertThat(values.getLong(2), is(row.getLong("LONG")));
assertThat(values.getDecimal(3), is(row.getDecimal("DEC")));
assertThat(values.getBytes(4), is(row.getBytes("BYTES_")));
assertThat(values.getKsqlArray(5), is(row.getKsqlArray("ARRAY")));
assertThat(values.getKsqlObject(6), is(row.getKsqlObject("MAP")));
assertThat(values.getKsqlObject(7), is(row.getKsqlObject("STRUCT")));
assertThat(values.getKsqlObject(8), is(row.getKsqlObject("COMPLEX")));
assertThat(values.toJsonString(), is((new JsonArray(values.getList())).toString()));
assertThat(values.toString(), is(values.toJsonString()));
// verify KsqlObject methods
final KsqlObject obj = row.asObject();
assertThat(obj.size(), is(TEST_COLUMN_NAMES.size()));
assertThat(obj.isEmpty(), is(false));
assertThat(obj.fieldNames(), contains(TEST_COLUMN_NAMES.toArray()));
assertThat(obj.getKsqlObject("K"), is(row.getKsqlObject("K")));
assertThat(obj.getString("STR"), is(row.getString("STR")));
assertThat(obj.getLong("LONG"), is(row.getLong("LONG")));
assertThat(obj.getDecimal("DEC"), is(row.getDecimal("DEC")));
assertThat(obj.getBytes("BYTES_"), is(row.getBytes("BYTES_")));
assertThat(obj.getKsqlArray("ARRAY"), is(row.getKsqlArray("ARRAY")));
assertThat(obj.getKsqlObject("MAP"), is(row.getKsqlObject("MAP")));
assertThat(obj.getKsqlObject("STRUCT"), is(row.getKsqlObject("STRUCT")));
assertThat(obj.getKsqlObject("COMPLEX"), is(row.getKsqlObject("COMPLEX")));
assertThat(obj.containsKey("DEC"), is(true));
assertThat(obj.containsKey("notafield"), is(false));
assertThat(obj.toJsonString(), is((new JsonObject(obj.getMap())).toString()));
assertThat(obj.toString(), is(obj.toJsonString()));
}
use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.
the class ApplyMigrationCommandTest method shouldApplyInsertStatement.
@Test
public void shouldApplyInsertStatement() throws Exception {
// Given:
command = PARSER.parse("-v", "3");
createMigrationFile(1, NAME, migrationsDir, COMMAND);
createMigrationFile(3, NAME, migrationsDir, INSERTS);
givenCurrentMigrationVersion("1");
givenAppliedMigration(1, NAME, MigrationState.MIGRATED);
// When:
final int result = command.command(config, (cfg, headers) -> ksqlClient, migrationsDir, Clock.fixed(Instant.ofEpochMilli(1000), ZoneId.systemDefault()));
// Then:
assertThat(result, is(0));
final InOrder inOrder = inOrder(ksqlClient);
verifyMigratedVersion(inOrder, 3, "1", MigrationState.MIGRATED, () -> {
inOrder.verify(ksqlClient).insertInto("`FOO`", new KsqlObject(ImmutableMap.of("`A`", "abcd")));
inOrder.verify(ksqlClient).insertInto("`FOO`", new KsqlObject(ImmutableMap.of("`A`", "efgh")));
inOrder.verify(ksqlClient).insertInto("`FOO`", new KsqlObject(ImmutableMap.of("`A`", "ijkl")));
});
inOrder.verify(ksqlClient).close();
inOrder.verifyNoMoreInteractions();
}
use of io.confluent.ksql.api.client.KsqlObject in project ksql by confluentinc.
the class ApplyMigrationCommandTest method defineStatementsShouldTakePrecedenceOverArgumentVariables.
@Test
public void defineStatementsShouldTakePrecedenceOverArgumentVariables() throws Exception {
// Given:
command = PARSER.parse("-a", "-d", "name=tame");
createMigrationFile(1, NAME, migrationsDir, "DEFINE name='flame'; INSERT INTO FOO VALUES ('${name}');");
when(versionQueryResult.get()).thenReturn(ImmutableList.of());
when(ksqlClient.getVariables()).thenReturn(ImmutableMap.of("name", "flame"));
givenAppliedMigration(1, NAME, MigrationState.MIGRATED);
// When:
final int result = command.command(config, (cfg, headers) -> ksqlClient, migrationsDir, Clock.fixed(Instant.ofEpochMilli(1000), ZoneId.systemDefault()));
// Then:
assertThat(result, is(0));
final InOrder inOrder = inOrder(ksqlClient);
inOrder.verify(ksqlClient).define("name", "flame");
inOrder.verify(ksqlClient).insertInto("`FOO`", new KsqlObject(ImmutableMap.of("`A`", "flame")));
inOrder.verify(ksqlClient).close();
inOrder.verifyNoMoreInteractions();
}
Aggregations