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();
}
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() + "'" + ");");
}
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);
}
}
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);
}
}
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)));
}
Aggregations