use of io.confluent.ksql.api.client.BatchedQueryResult in project ksql by confluentinc.
the class ValidateMigrationsCommandTest method givenAppliedMigrations.
/**
* @param versions versions, in the order they were applied
* @param checksums corresponding checksums (ordered according to {@code versions})
* @param states corresponding migration states (ordered according to {@code versions})
*/
private void givenAppliedMigrations(final List<String> versions, final List<String> checksums, final List<MigrationState> states) throws Exception {
String version = versions.size() > 0 ? versions.get(versions.size() - 1) : MetadataUtil.NONE_VERSION;
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);
for (int i = versions.size() - 1; i >= 0; i--) {
version = versions.get(i);
String prevVersion = i > 0 ? versions.get(i - 1) : MetadataUtil.NONE_VERSION;
row = mock(Row.class);
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(row));
when(row.getString(1)).thenReturn(version);
when(row.getString(2)).thenReturn(checksums.get(i));
when(row.getString(3)).thenReturn(prevVersion);
when(row.getString(4)).thenReturn(states.get(i).toString());
when(row.getString(5)).thenReturn("name");
when(row.getString(6)).thenReturn("N/A");
when(row.getString(7)).thenReturn("N/A");
when(row.getString(8)).thenReturn("no_error");
}
}
use of io.confluent.ksql.api.client.BatchedQueryResult 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.BatchedQueryResult 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.BatchedQueryResult in project ksql by confluentinc.
the class MetadataUtil method getOptionalInfoForVersions.
public static Map<Integer, Optional<MigrationVersionInfo>> getOptionalInfoForVersions(final List<Integer> versions, final MigrationConfig config, final Client ksqlClient) {
if (serverSupportsMultiKeyPullQuery(ksqlClient, config)) {
// issue a single, multi-key pull query
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 IN ('" + versions.stream().map(String::valueOf).collect(Collectors.joining("', '")) + "');");
final Map<Integer, MigrationVersionInfo> resultSet;
try {
resultSet = result.get().stream().map(MigrationVersionInfo::fromResultRow).collect(Collectors.toMap(MigrationVersionInfo::getVersion, vInfo -> vInfo));
} catch (InterruptedException | ExecutionException e) {
throw new MigrationException(String.format("Failed to query state for migration with versions %s: %s", versions, e.getMessage()));
}
return versions.stream().collect(Collectors.toMap(v -> v, v -> Optional.ofNullable(resultSet.get(v))));
} else {
// issue multiple, single-key pull queries
return versions.stream().collect(Collectors.toMap(v -> v, v -> getOptionalInfoForVersion(String.valueOf(v), config, ksqlClient)));
}
}
use of io.confluent.ksql.api.client.BatchedQueryResult 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));
}
Aggregations