use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class MigrationInfoCommandTest method givenCurrentMigrationVersion.
private void givenCurrentMigrationVersion(final String version) throws Exception {
Row row = mock(Row.class);
BatchedQueryResult queryResult = mock(BatchedQueryResult.class);
when(ksqlClient.executeQuery("SELECT VERSION FROM " + MIGRATIONS_TABLE + " WHERE version_key = '" + CURRENT_VERSION_KEY + "';")).thenReturn(queryResult);
when(queryResult.get()).thenReturn(ImmutableList.of(row));
when(row.getString("VERSION")).thenReturn(version);
}
use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class MetadataUtil method getCurrentVersion.
public static String getCurrentVersion(final MigrationConfig config, final Client client) {
final String migrationTableName = config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_NAME);
final BatchedQueryResult result = client.executeQuery("SELECT VERSION FROM " + migrationTableName + " WHERE version_key = '" + CURRENT_VERSION_KEY + "';");
try {
final List<Row> resultRows = result.get();
if (resultRows.size() == 0) {
return NONE_VERSION;
}
return resultRows.get(0).getString("VERSION");
} catch (InterruptedException | ExecutionException e) {
throw new MigrationException(String.format("Could not query %s: %s", migrationTableName, e.getMessage()));
}
}
use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class MetadataUtil method getOptionalInfoForVersion.
public static Optional<MigrationVersionInfo> getOptionalInfoForVersion(final String version, final MigrationConfig config, final Client ksqlClient) {
final String migrationTableName = config.getString(MigrationConfig.KSQL_MIGRATIONS_TABLE_NAME);
final BatchedQueryResult result = ksqlClient.executeQuery("SELECT version, checksum, previous, state, name, started_on, completed_on, error_reason " + "FROM " + migrationTableName + " WHERE version_key = '" + version + "';");
final Row resultRow;
try {
final List<Row> resultRows = result.get();
if (resultRows.size() == 0) {
return Optional.empty();
}
resultRow = resultRows.get(0);
} catch (InterruptedException | ExecutionException e) {
throw new MigrationException(String.format("Failed to query state for migration with version %s: %s", version, e.getMessage()));
}
return Optional.of(MigrationVersionInfo.fromResultRow(resultRow));
}
use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class MigrationInfoCommandTest method givenMigrations.
/**
* @param appliedVersions applied versions, in the order they were applied
* @param states corresponding migration states (ordered according to {@code versions})
* @param errorReasons corresponding error reasons (ordered according to {@code versions})
* @param unappliedVersions (additional) existing versions, that have not been applied
* @param multiKeyPullQuerySupported whether the server version supports multi-key pull queries
*/
private void givenMigrations(final List<String> appliedVersions, final List<MigrationState> states, final List<String> errorReasons, final List<String> unappliedVersions, final boolean multiKeyPullQuerySupported) throws Exception {
givenExistingMigrationFiles(appliedVersions);
givenExistingMigrationFiles(unappliedVersions);
givenCurrentMigrationVersion(appliedVersions.size() > 0 ? appliedVersions.get(appliedVersions.size() - 1) : MetadataUtil.NONE_VERSION);
final List<Row> appliedRows = new ArrayList<>();
for (int i = 0; i < appliedVersions.size(); i++) {
String version = appliedVersions.get(i);
String prevVersion = i > 0 ? appliedVersions.get(i - 1) : MetadataUtil.NONE_VERSION;
Row row = mock(Row.class);
when(row.getString(1)).thenReturn(version);
when(row.getString(2)).thenReturn("checksum");
when(row.getString(3)).thenReturn(prevVersion);
when(row.getString(4)).thenReturn(states.get(i).toString());
when(row.getString(5)).thenReturn(fileDescriptionForVersion(version));
when(row.getString(6)).thenReturn("N/A");
when(row.getString(7)).thenReturn("N/A");
when(row.getString(8)).thenReturn(errorReasons.get(i));
appliedRows.add(row);
}
if (multiKeyPullQuerySupported) {
BatchedQueryResult queryResult = mock(BatchedQueryResult.class);
when(ksqlClient.executeQuery("SELECT version, checksum, previous, state, name, started_on, completed_on, error_reason " + "FROM " + MIGRATIONS_TABLE + " WHERE version_key IN ('" + Stream.concat(appliedVersions.stream(), unappliedVersions.stream()).collect(Collectors.joining("', '")) + "');")).thenReturn(queryResult);
when(queryResult.get()).thenReturn(appliedRows);
} else {
for (int i = 0; i < appliedVersions.size(); i++) {
BatchedQueryResult queryResult = mock(BatchedQueryResult.class);
when(ksqlClient.executeQuery("SELECT version, checksum, previous, state, name, started_on, completed_on, error_reason FROM " + MIGRATIONS_TABLE + " WHERE version_key = '" + appliedVersions.get(i) + "';")).thenReturn(queryResult);
when(queryResult.get()).thenReturn(ImmutableList.of(appliedRows.get(i)));
}
for (String version : unappliedVersions) {
BatchedQueryResult queryResult = mock(BatchedQueryResult.class);
when(ksqlClient.executeQuery("SELECT version, checksum, previous, state, name, started_on, completed_on, error_reason FROM " + MIGRATIONS_TABLE + " WHERE version_key = '" + version + "';")).thenReturn(queryResult);
when(queryResult.get()).thenReturn(ImmutableList.of());
}
}
}
use of io.confluent.ksql.api.client.Row in project ksql by confluentinc.
the class ClientMutationIntegrationTest method shouldInsertIntoTable.
@Test
public void shouldInsertIntoTable() throws Exception {
// Given
final Map<String, Object> properties = new HashMap<>();
properties.put("auto.offset.reset", "earliest");
final KsqlObject insertRow = new KsqlObject().put("K", "my_key").put("LONG", 11L);
// When
final String query = "SELECT * from " + TEST_TABLE + " WHERE K='my_key' EMIT CHANGES LIMIT 1;";
StreamedQueryResult queryResult = client.streamQuery(query, properties).get();
final Row row = assertThatEventually(() -> {
// Potentially try inserting multiple times, in case the query wasn't started by the first time
try {
client.insertInto(TEST_TABLE, insertRow).get();
} catch (Exception e) {
throw new RuntimeException(e);
}
return queryResult.poll(Duration.ofMillis(10));
}, is(notNullValue()));
// Then: a newly inserted row arrives
assertThat(row.getString("K"), is("my_key"));
assertThat(row.getLong("LONG"), is(11L));
}
Aggregations