Search in sources :

Example 26 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class KeyValueExtractor method extractKey.

public static GenericKey extractKey(final JsonObject values, final LogicalSchema logicalSchema, final SqlValueCoercer sqlValueCoercer) {
    final List<Column> keyColumns = logicalSchema.key();
    final GenericKey.Builder builder = GenericKey.builder(logicalSchema);
    for (final Column keyColumn : keyColumns) {
        final Object value = values.getValue(keyColumn.name().text());
        if (value == null) {
            throw new KsqlApiException("Key field must be specified: " + keyColumn.name().text(), Errors.ERROR_CODE_BAD_REQUEST);
        }
        final Object coercedValue = coerceObject(value, keyColumn.type(), sqlValueCoercer);
        builder.append(coercedValue);
    }
    return builder.build();
}
Also used : Column(io.confluent.ksql.schema.ksql.Column) JsonObject(io.vertx.core.json.JsonObject) GenericKey(io.confluent.ksql.GenericKey) KsqlApiException(io.confluent.ksql.api.server.KsqlApiException)

Example 27 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class PullQueryIQv2FunctionalTest method setUpClass.

@BeforeClass
public static void setUpClass() {
    TEST_HARNESS.ensureTopics(2, USER_TOPIC);
    final AtomicLong timestampSupplier = new AtomicLong(BASE_TIME);
    final Multimap<GenericKey, RecordMetadata> producedRows = TEST_HARNESS.produceRows(USER_TOPIC, USER_PROVIDER, KEY_FORMAT, VALUE_FORMAT, timestampSupplier::getAndIncrement);
    LOG.info("Produced rows: " + producedRows.size());
    makeAdminRequest("CREATE STREAM " + USERS_STREAM + " (" + USER_PROVIDER.ksqlSchemaString(false) + ")" + " WITH (" + "   kafka_topic='" + USER_TOPIC + "', " + "   value_format='" + VALUE_FORMAT.name() + "'" + ");");
}
Also used : RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) AtomicLong(java.util.concurrent.atomic.AtomicLong) GenericKey(io.confluent.ksql.GenericKey) BeforeClass(org.junit.BeforeClass)

Example 28 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class KsMaterializedSessionTableIQv2 method get.

@Override
public KsMaterializedQueryResult<WindowedRow> get(final GenericKey key, final int partition, final Range<Instant> windowStart, final Range<Instant> windowEnd, final Optional<Position> position) {
    try {
        final WindowRangeQuery<GenericKey, GenericRow> query = WindowRangeQuery.withKey(key);
        StateQueryRequest<KeyValueIterator<Windowed<GenericKey>, GenericRow>> request = inStore(stateStore.getStateStoreName()).withQuery(query);
        if (position.isPresent()) {
            request = request.withPositionBound(PositionBound.at(position.get()));
        }
        final StateQueryResult<KeyValueIterator<Windowed<GenericKey>, GenericRow>> result = stateStore.getKafkaStreams().query(request);
        final QueryResult<KeyValueIterator<Windowed<GenericKey>, GenericRow>> queryResult = result.getPartitionResults().get(partition);
        if (queryResult.isFailure()) {
            throw failedQueryException(queryResult);
        }
        try (KeyValueIterator<Windowed<GenericKey>, GenericRow> it = queryResult.getResult()) {
            final Builder<WindowedRow> builder = ImmutableList.builder();
            while (it.hasNext()) {
                final KeyValue<Windowed<GenericKey>, GenericRow> next = it.next();
                final Window wnd = next.key.window();
                if (!windowStart.contains(wnd.startTime())) {
                    continue;
                }
                if (!windowEnd.contains(wnd.endTime())) {
                    continue;
                }
                final long rowTime = wnd.end();
                final WindowedRow row = WindowedRow.of(stateStore.schema(), next.key, next.value, rowTime);
                builder.add(row);
            }
            return KsMaterializedQueryResult.rowIteratorWithPosition(builder.build().iterator(), queryResult.getPosition());
        }
    } catch (final NotUpToBoundException | MaterializationException e) {
        throw e;
    } catch (final Exception e) {
        throw new MaterializationException("Failed to get value from materialized table", e);
    }
}
Also used : Window(org.apache.kafka.streams.kstream.Window) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) GenericRow(io.confluent.ksql.GenericRow) Windowed(org.apache.kafka.streams.kstream.Windowed) KeyValueIterator(org.apache.kafka.streams.state.KeyValueIterator) GenericKey(io.confluent.ksql.GenericKey) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow)

Example 29 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class KsMaterializedWindowTableIQv2 method get.

@Override
public KsMaterializedQueryResult<WindowedRow> get(final GenericKey key, final int partition, final Range<Instant> windowStartBounds, final Range<Instant> windowEndBounds, final Optional<Position> position) {
    try {
        final Instant lower = calculateLowerBound(windowStartBounds, windowEndBounds);
        final Instant upper = calculateUpperBound(windowStartBounds, windowEndBounds);
        final WindowKeyQuery<GenericKey, ValueAndTimestamp<GenericRow>> query = WindowKeyQuery.withKeyAndWindowStartRange(key, lower, upper);
        StateQueryRequest<WindowStoreIterator<ValueAndTimestamp<GenericRow>>> request = inStore(stateStore.getStateStoreName()).withQuery(query);
        if (position.isPresent()) {
            request = request.withPositionBound(PositionBound.at(position.get()));
        }
        final KafkaStreams streams = stateStore.getKafkaStreams();
        final StateQueryResult<WindowStoreIterator<ValueAndTimestamp<GenericRow>>> result = streams.query(request);
        final QueryResult<WindowStoreIterator<ValueAndTimestamp<GenericRow>>> queryResult = result.getPartitionResults().get(partition);
        if (queryResult.isFailure()) {
            throw failedQueryException(queryResult);
        }
        if (queryResult.getResult() == null) {
            return KsMaterializedQueryResult.rowIteratorWithPosition(Collections.emptyIterator(), queryResult.getPosition());
        }
        try (WindowStoreIterator<ValueAndTimestamp<GenericRow>> it = queryResult.getResult()) {
            final Builder<WindowedRow> builder = ImmutableList.builder();
            while (it.hasNext()) {
                final KeyValue<Long, ValueAndTimestamp<GenericRow>> next = it.next();
                final Instant windowStart = Instant.ofEpochMilli(next.key);
                if (!windowStartBounds.contains(windowStart)) {
                    continue;
                }
                final Instant windowEnd = windowStart.plus(windowSize);
                if (!windowEndBounds.contains(windowEnd)) {
                    continue;
                }
                final TimeWindow window = new TimeWindow(windowStart.toEpochMilli(), windowEnd.toEpochMilli());
                final WindowedRow row = WindowedRow.of(stateStore.schema(), new Windowed<>(key, window), next.value.value(), next.value.timestamp());
                builder.add(row);
            }
            return KsMaterializedQueryResult.rowIteratorWithPosition(builder.build().iterator(), queryResult.getPosition());
        }
    } catch (final NotUpToBoundException | MaterializationException e) {
        throw e;
    } catch (final Exception e) {
        throw new MaterializationException("Failed to get value from materialized table", e);
    }
}
Also used : WindowStoreIterator(org.apache.kafka.streams.state.WindowStoreIterator) KafkaStreams(org.apache.kafka.streams.KafkaStreams) Instant(java.time.Instant) TimeWindow(org.apache.kafka.streams.kstream.internals.TimeWindow) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) MaterializationException(io.confluent.ksql.execution.streams.materialization.MaterializationException) ValueAndTimestamp(org.apache.kafka.streams.state.ValueAndTimestamp) GenericRow(io.confluent.ksql.GenericRow) GenericKey(io.confluent.ksql.GenericKey) WindowedRow(io.confluent.ksql.execution.streams.materialization.WindowedRow)

Example 30 with GenericKey

use of io.confluent.ksql.GenericKey in project ksql by confluentinc.

the class SourceBuilderTest method shouldHandleMultiKeyFieldEmptyGenericKey.

@Test
public void shouldHandleMultiKeyFieldEmptyGenericKey() {
    // Given:
    givenMultiColumnSourceTable();
    final List<ValueTransformerWithKey<GenericKey, GenericRow, GenericRow>> transformers = getTransformersFromTableSource(tableSource);
    final GenericKey key = GenericKey.genericKey(null, null);
    // When:
    final GenericRow withKeyAndPseudoCols = applyAllTransformers(key, transformers, row);
    // Then:
    assertThat(withKeyAndPseudoCols, equalTo(genericRow("baz", 123, HEADER_A, HEADER_B, null, A_ROWTIME, A_ROWPARTITION, A_ROWOFFSET, null, null)));
}
Also used : GenericRow(io.confluent.ksql.GenericRow) ValueTransformerWithKey(org.apache.kafka.streams.kstream.ValueTransformerWithKey) GenericKey(io.confluent.ksql.GenericKey) Test(org.junit.Test)

Aggregations

GenericKey (io.confluent.ksql.GenericKey)147 GenericRow (io.confluent.ksql.GenericRow)100 Test (org.junit.Test)93 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)24 Windowed (org.apache.kafka.streams.kstream.Windowed)20 WindowedRow (io.confluent.ksql.execution.streams.materialization.WindowedRow)14 PhysicalSchema (io.confluent.ksql.schema.ksql.PhysicalSchema)14 Materialized (org.apache.kafka.streams.kstream.Materialized)13 ValueAndTimestamp (org.apache.kafka.streams.state.ValueAndTimestamp)13 UnqualifiedColumnReferenceExp (io.confluent.ksql.execution.expression.tree.UnqualifiedColumnReferenceExp)12 MaterializationException (io.confluent.ksql.execution.streams.materialization.MaterializationException)9 IntegrationTest (io.confluent.common.utils.IntegrationTest)8 Materialization (io.confluent.ksql.execution.streams.materialization.Materialization)8 Row (io.confluent.ksql.execution.streams.materialization.Row)8 PersistentQueryMetadata (io.confluent.ksql.util.PersistentQueryMetadata)8 TimeWindow (org.apache.kafka.streams.kstream.internals.TimeWindow)8 IntegrationTest (org.apache.kafka.test.IntegrationTest)8 InOrder (org.mockito.InOrder)8 MaterializedTable (io.confluent.ksql.execution.streams.materialization.MaterializedTable)7 Objects (java.util.Objects)7