use of io.confluent.ksql.GenericKey in project ksql by confluentinc.
the class KafkaConsumerFactory method create.
public static KafkaConsumer<Object, GenericRow> create(final KsqlTopic ksqlTopic, final LogicalSchema logicalSchema, final ServiceContext serviceContext, final Map<String, Object> consumerProperties, final KsqlConfig ksqlConfig, final String consumerGroupId) {
final PhysicalSchema physicalSchema = PhysicalSchema.from(logicalSchema, ksqlTopic.getKeyFormat().getFeatures(), ksqlTopic.getValueFormat().getFeatures());
final KeySerdeFactory keySerdeFactory = new GenericKeySerDe();
final Deserializer<Object> keyDeserializer;
if (ksqlTopic.getKeyFormat().getWindowInfo().isPresent()) {
final Serde<Windowed<GenericKey>> keySerde = keySerdeFactory.create(ksqlTopic.getKeyFormat().getFormatInfo(), ksqlTopic.getKeyFormat().getWindowInfo().get(), physicalSchema.keySchema(), ksqlConfig, serviceContext.getSchemaRegistryClientFactory(), "", NoopProcessingLogContext.INSTANCE, Optional.empty());
keyDeserializer = getDeserializer(keySerde.deserializer());
} else {
final Serde<GenericKey> keySerde = keySerdeFactory.create(ksqlTopic.getKeyFormat().getFormatInfo(), physicalSchema.keySchema(), ksqlConfig, serviceContext.getSchemaRegistryClientFactory(), "", NoopProcessingLogContext.INSTANCE, Optional.empty());
keyDeserializer = getDeserializer(keySerde.deserializer());
}
final ValueSerdeFactory valueSerdeFactory = new GenericRowSerDe();
final Serde<GenericRow> valueSerde = valueSerdeFactory.create(ksqlTopic.getValueFormat().getFormatInfo(), physicalSchema.valueSchema(), ksqlConfig, serviceContext.getSchemaRegistryClientFactory(), "", NoopProcessingLogContext.INSTANCE, Optional.empty());
return new KafkaConsumer<>(consumerConfig(consumerProperties, ksqlConfig, consumerGroupId), keyDeserializer, valueSerde.deserializer());
}
use of io.confluent.ksql.GenericKey in project ksql by confluentinc.
the class InsertValuesExecutor method serializeKey.
private byte[] serializeKey(final GenericKey keyValue, final DataSource dataSource, final KsqlConfig config, final ServiceContext serviceContext) {
final PhysicalSchema physicalSchema = PhysicalSchema.from(dataSource.getSchema(), dataSource.getKsqlTopic().getKeyFormat().getFeatures(), dataSource.getKsqlTopic().getValueFormat().getFeatures());
ensureKeySchemasMatch(physicalSchema.keySchema(), dataSource, serviceContext);
final Serde<GenericKey> keySerde = keySerdeFactory.create(dataSource.getKsqlTopic().getKeyFormat().getFormatInfo(), physicalSchema.keySchema(), config, serviceContext.getSchemaRegistryClientFactory(), "", NoopProcessingLogContext.INSTANCE, Optional.empty());
final String topicName = dataSource.getKafkaTopicName();
try {
return keySerde.serializer().serialize(topicName, keyValue);
} catch (final Exception e) {
maybeThrowSchemaRegistryAuthError(FormatFactory.fromName(dataSource.getKsqlTopic().getKeyFormat().getFormat()), topicName, true, AclOperation.WRITE, e);
LOG.error("Could not serialize key.", e);
throw new KsqlException("Could not serialize key: " + keyValue, e);
}
}
use of io.confluent.ksql.GenericKey in project ksql by confluentinc.
the class ForeignKeyTableTableJoinBuilderTest method shouldDoLeftJoinOnSubKey.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldDoLeftJoinOnSubKey() {
// Given:
givenLeftJoin(leftMultiKey, L_KEY_2);
// When:
final KTableHolder<Struct> result = join.build(planBuilder, planInfo);
// Then:
final ArgumentCaptor<KsqlKeyExtractor> ksqlKeyExtractor = ArgumentCaptor.forClass(KsqlKeyExtractor.class);
verify(leftKTableMultiKey).leftJoin(same(rightKTable), ksqlKeyExtractor.capture(), eq(new KsqlValueJoiner(LEFT_SCHEMA_MULTI_KEY.value().size(), RIGHT_SCHEMA.value().size(), 0)), any(Materialized.class));
verifyNoMoreInteractions(leftKTable, rightKTable, resultKTable);
final GenericKey extractedKey = GenericKey.genericKey(LEFT_KEY_2);
assertThat(ksqlKeyExtractor.getValue().apply(LEFT_ROW_MULTI), is(extractedKey));
assertThat(result.getTable(), is(resultKTable));
assertThat(result.getExecutionKeyFactory(), is(executionKeyFactory));
}
use of io.confluent.ksql.GenericKey in project ksql by confluentinc.
the class ForeignKeyTableTableJoinBuilderTest method shouldDoLeftJoinOnKey.
// this is actually a PK-PK join and the logical planner would not compile a FK-join plan
// for this case atm
// however, from a physical plan POV this should still work, so we would like to keep this test
//
// it might be possible to actually change the logical planner to compile a PK-PK join as
// FK-join if input tables are not co-partitioned (instead of throwing an error an rejecting
// the query), ie, if key-format or partition-count do not match -- it's an open question
// if it would be a good idea to do this though
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldDoLeftJoinOnKey() {
// Given:
givenLeftJoin(left, L_KEY);
// When:
final KTableHolder<Struct> result = join.build(planBuilder, planInfo);
// Then:
final ArgumentCaptor<KsqlKeyExtractor> ksqlKeyExtractor = ArgumentCaptor.forClass(KsqlKeyExtractor.class);
verify(leftKTable).leftJoin(same(rightKTable), ksqlKeyExtractor.capture(), eq(new KsqlValueJoiner(LEFT_SCHEMA.value().size(), RIGHT_SCHEMA.value().size(), 0)), any(Materialized.class));
verifyNoMoreInteractions(leftKTable, rightKTable, resultKTable);
final GenericKey extractedKey = GenericKey.genericKey(LEFT_KEY);
assertThat(ksqlKeyExtractor.getValue().apply(LEFT_ROW), is(extractedKey));
assertThat(result.getTable(), is(resultKTable));
assertThat(result.getExecutionKeyFactory(), is(executionKeyFactory));
}
use of io.confluent.ksql.GenericKey in project ksql by confluentinc.
the class ForeignKeyTableTableJoinBuilderTest method shouldDoInnerJoinOnSubKey.
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void shouldDoInnerJoinOnSubKey() {
// Given:
givenInnerJoin(leftMultiKey, L_KEY_2);
// When:
final KTableHolder<Struct> result = join.build(planBuilder, planInfo);
// Then:
final ArgumentCaptor<KsqlKeyExtractor> ksqlKeyExtractor = ArgumentCaptor.forClass(KsqlKeyExtractor.class);
verify(leftKTableMultiKey).join(same(rightKTable), ksqlKeyExtractor.capture(), eq(new KsqlValueJoiner(LEFT_SCHEMA_MULTI_KEY.value().size(), RIGHT_SCHEMA.value().size(), 0)), any(Materialized.class));
verifyNoMoreInteractions(leftKTable, rightKTable, resultKTable);
final GenericKey extractedKey = GenericKey.genericKey(LEFT_KEY_2);
assertThat(ksqlKeyExtractor.getValue().apply(LEFT_ROW_MULTI), is(extractedKey));
assertThat(result.getTable(), is(resultKTable));
assertThat(result.getExecutionKeyFactory(), is(executionKeyFactory));
}
Aggregations