Search in sources :

Example 16 with Row

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);
}
Also used : Row(io.confluent.ksql.api.client.Row) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Example 17 with Row

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()));
    }
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Row(io.confluent.ksql.api.client.Row) ExecutionException(java.util.concurrent.ExecutionException) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Example 18 with Row

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));
}
Also used : MigrationException(io.confluent.ksql.tools.migrations.MigrationException) Row(io.confluent.ksql.api.client.Row) ExecutionException(java.util.concurrent.ExecutionException) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Example 19 with Row

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());
        }
    }
}
Also used : ArrayList(java.util.ArrayList) Row(io.confluent.ksql.api.client.Row) Matchers.containsString(org.hamcrest.Matchers.containsString) BatchedQueryResult(io.confluent.ksql.api.client.BatchedQueryResult)

Example 20 with Row

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

Aggregations

Row (io.confluent.ksql.api.client.Row)27 Test (org.junit.Test)16 IntegrationTest (io.confluent.common.utils.IntegrationTest)13 StreamedQueryResult (io.confluent.ksql.api.client.StreamedQueryResult)10 GenericRow (io.confluent.ksql.GenericRow)8 BatchedQueryResult (io.confluent.ksql.api.client.BatchedQueryResult)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6 KsqlObject (io.confluent.ksql.api.client.KsqlObject)5 ExecutionException (java.util.concurrent.ExecutionException)5 KsqlArray (io.confluent.ksql.api.client.KsqlArray)4 KsqlClientException (io.confluent.ksql.api.client.exception.KsqlClientException)4 BigDecimal (java.math.BigDecimal)4 HashMap (java.util.HashMap)3 LinkedList (java.util.LinkedList)3 ZooKeeperClientException (kafka.zookeeper.ZooKeeperClientException)3 MigrationException (io.confluent.ksql.tools.migrations.MigrationException)2 JsonArray (io.vertx.core.json.JsonArray)2 JsonObject (io.vertx.core.json.JsonObject)2 ArrayList (java.util.ArrayList)2 AcksPublisher (io.confluent.ksql.api.client.AcksPublisher)1