Search in sources :

Example 6 with KsqlObject

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"));
}
Also used : 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 7 with KsqlObject

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"));
}
Also used : KsqlClientException(io.confluent.ksql.api.client.exception.KsqlClientException) KsqlArray(io.confluent.ksql.api.client.KsqlArray) ZooKeeperClientException(kafka.zookeeper.ZooKeeperClientException) KsqlClientException(io.confluent.ksql.api.client.exception.KsqlClientException) ExecutionException(java.util.concurrent.ExecutionException) KsqlObject(io.confluent.ksql.api.client.KsqlObject) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 8 with KsqlObject

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

Example 9 with KsqlObject

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();
}
Also used : InOrder(org.mockito.InOrder) KsqlObject(io.confluent.ksql.api.client.KsqlObject) Test(org.junit.Test)

Example 10 with KsqlObject

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();
}
Also used : InOrder(org.mockito.InOrder) KsqlObject(io.confluent.ksql.api.client.KsqlObject) Test(org.junit.Test)

Aggregations

KsqlObject (io.confluent.ksql.api.client.KsqlObject)16 Test (org.junit.Test)12 KsqlArray (io.confluent.ksql.api.client.KsqlArray)8 IntegrationTest (io.confluent.common.utils.IntegrationTest)6 KsqlClientException (io.confluent.ksql.api.client.exception.KsqlClientException)6 Row (io.confluent.ksql.api.client.Row)5 BigDecimal (java.math.BigDecimal)5 HashMap (java.util.HashMap)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 InOrder (org.mockito.InOrder)5 StreamedQueryResult (io.confluent.ksql.api.client.StreamedQueryResult)4 JsonArray (io.vertx.core.json.JsonArray)4 JsonObject (io.vertx.core.json.JsonObject)4 AcksPublisher (io.confluent.ksql.api.client.AcksPublisher)3 BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Client (io.confluent.ksql.api.client.Client)2 ClientOptions (io.confluent.ksql.api.client.ClientOptions)2 ConnectorDescription (io.confluent.ksql.api.client.ConnectorDescription)2 ConnectorInfo (io.confluent.ksql.api.client.ConnectorInfo)2