use of io.confluent.ksql.api.client.KsqlArray 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.KsqlArray in project ksql by confluentinc.
the class RowImplTest method shouldGetAsObject.
@Test
public void shouldGetAsObject() {
// When
final KsqlObject obj = row.asObject();
// Then
assertThat(obj.getString("f_str"), is("foo"));
assertThat(obj.getInteger("f_int"), is(2));
assertThat(obj.getLong("f_long"), is(1234L));
assertThat(obj.getDouble("f_double"), is(34.43));
assertThat(obj.getBoolean("f_bool"), is(false));
assertThat(obj.getDecimal("f_decimal"), is(new BigDecimal("12.21")));
assertThat(obj.getBytes("f_bytes"), is(new byte[] { 0, 1, 2 }));
assertThat(obj.getKsqlArray("f_array"), is(new KsqlArray(ImmutableList.of("e1", "e2"))));
assertThat(obj.getKsqlObject("f_map"), is(new KsqlObject(ImmutableMap.of("k1", "v1", "k2", "v2"))));
assertThat(obj.getKsqlObject("f_struct"), is(new KsqlObject(ImmutableMap.of("f1", "baz", "f2", 12))));
assertThat(obj.getString("f_timestamp"), is("2020-01-01T04:40:34.789"));
assertThat(obj.getString("f_date"), is("2020-01-01"));
assertThat(obj.getString("f_time"), is("04:40:34.789"));
}
use of io.confluent.ksql.api.client.KsqlArray in project ksql by confluentinc.
the class ClientMutationIntegrationTest method shouldExecuteQueryWithProperties.
@Test
public void shouldExecuteQueryWithProperties() {
// 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_shouldExecuteQueryWithProperties"))).put("STR", "Value_shouldExecuteQueryWithProperties").put("LONG", 2000L).put("DEC", new BigDecimal("12.34")).put("BYTES_", new byte[] { 0, 1, 2 }).put("ARRAY", new KsqlArray().add("v1_shouldExecuteQueryWithProperties").add("v2_shouldExecuteQueryWithProperties")).put("MAP", new KsqlObject().put("test_name", "shouldExecuteQueryWithProperties")).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 BatchedQueryResult queryResult = client.executeQuery(sql, properties);
// Then: a newly inserted row arrives
// Wait for row to arrive
final AtomicReference<Row> rowRef = new AtomicReference<>();
new Thread(() -> {
try {
final List<Row> rows = queryResult.get();
assertThat(rows, hasSize(1));
rowRef.set(rows.get(0));
} catch (final Exception e) {
throw new RuntimeException(e);
}
}).start();
// Insert a new row
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 rowRef.get();
}, is(notNullValue()));
// Verify received row
assertThat(row.getKsqlObject("K"), is(new KsqlObject().put("F1", new KsqlArray().add("my_key_shouldExecuteQueryWithProperties"))));
assertThat(row.getString("STR"), is("Value_shouldExecuteQueryWithProperties"));
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_shouldExecuteQueryWithProperties").add("v2_shouldExecuteQueryWithProperties")));
assertThat(row.getKsqlObject("MAP"), is(new KsqlObject().put("test_name", "shouldExecuteQueryWithProperties")));
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"));
}
use of io.confluent.ksql.api.client.KsqlArray in project ksql by confluentinc.
the class ClientIntegrationTest method convertToClientRows.
private static List<KsqlArray> convertToClientRows(final Multimap<GenericKey, GenericRow> data) {
final List<KsqlArray> expectedRows = new ArrayList<>();
for (final Map.Entry<GenericKey, GenericRow> entry : data.entries()) {
final KsqlArray expectedRow = new KsqlArray();
addObjectToKsqlArray(expectedRow, entry.getKey().get(0));
for (final Object value : entry.getValue().values()) {
addObjectToKsqlArray(expectedRow, value);
}
// Add header column
expectedRow.add(new byte[] { 23 });
expectedRows.add(expectedRow);
}
return expectedRows;
}
use of io.confluent.ksql.api.client.KsqlArray in project ksql by confluentinc.
the class ApplyMigrationCommandTest method createKsqlObject.
private KsqlObject createKsqlObject(final String versionKey, final int version, final String name, final MigrationState state, final String startOn, final String completedOn, final String previous, final Optional<String> errorReason) {
final List<String> KEYS = ImmutableList.of("VERSION_KEY", "VERSION", "NAME", "STATE", "CHECKSUM", "STARTED_ON", "COMPLETED_ON", "PREVIOUS", "ERROR_REASON");
final List<String> values = ImmutableList.of(versionKey, Integer.toString(version), name, state.toString(), MigrationsDirectoryUtil.computeHashForFile(getMigrationFilePath(version, name, migrationsDir)), startOn, completedOn, previous, errorReason.orElse("N/A"));
return KsqlObject.fromArray(KEYS, new KsqlArray(values));
}
Aggregations