Search in sources :

Example 11 with Format

use of io.confluent.ksql.serde.Format in project ksql by confluentinc.

the class InsertValuesExecutor method ensureKeySchemasMatch.

/**
 * Ensures that the key schema that we generate will be identical
 * to the schema that is registered in schema registry, if it exists.
 * Otherwise, it is possible that we will publish messages with a new
 * schemaID, meaning that logically identical keys might be routed to
 * different partitions.
 */
private static void ensureKeySchemasMatch(final PersistenceSchema keySchema, final DataSource dataSource, final ServiceContext serviceContext) {
    final KeyFormat keyFormat = dataSource.getKsqlTopic().getKeyFormat();
    final Format format = FormatFactory.fromName(keyFormat.getFormat());
    if (!format.supportsFeature(SerdeFeature.SCHEMA_INFERENCE)) {
        return;
    }
    final ParsedSchema schema = format.getSchemaTranslator(keyFormat.getFormatInfo().getProperties()).toParsedSchema(keySchema);
    final Optional<SchemaMetadata> latest;
    try {
        latest = SchemaRegistryUtil.getLatestSchema(serviceContext.getSchemaRegistryClient(), dataSource.getKafkaTopicName(), true);
    } catch (final KsqlException e) {
        maybeThrowSchemaRegistryAuthError(format, dataSource.getKafkaTopicName(), true, AclOperation.READ, e);
        throw new KsqlException("Could not determine that insert values operations is safe; " + "operation potentially overrides existing key schema in schema registry.", e);
    }
    if (latest.isPresent() && !latest.get().getSchema().equals(schema.canonicalString())) {
        throw new KsqlException("Cannot INSERT VALUES into data source " + dataSource.getName() + ". ksqlDB generated schema would overwrite existing key schema." + "\n\tExisting Schema: " + latest.get().getSchema() + "\n\tksqlDB Generated: " + schema.canonicalString());
    }
}
Also used : KeyFormat(io.confluent.ksql.serde.KeyFormat) Format(io.confluent.ksql.serde.Format) SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) KeyFormat(io.confluent.ksql.serde.KeyFormat) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

Format (io.confluent.ksql.serde.Format)11 KsqlException (io.confluent.ksql.util.KsqlException)8 SchemaTranslator (io.confluent.ksql.serde.SchemaTranslator)6 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)5 FormatInfo (io.confluent.ksql.serde.FormatInfo)5 SerdeFeatures (io.confluent.ksql.serde.SerdeFeatures)5 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)4 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)4 SimpleColumn (io.confluent.ksql.schema.ksql.SimpleColumn)4 Optional (java.util.Optional)4 SchemaMetadata (io.confluent.kafka.schemaregistry.client.SchemaMetadata)3 CreateSource (io.confluent.ksql.parser.tree.CreateSource)3 ImmutableList (com.google.common.collect.ImmutableList)2 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)2 ProtobufSchema (io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema)2 LogicalSchema (io.confluent.ksql.schema.ksql.LogicalSchema)2 SchemaResult (io.confluent.ksql.schema.ksql.inference.TopicSchemaSupplier.SchemaResult)2 SerdeFeature (io.confluent.ksql.serde.SerdeFeature)2 KsqlStatementException (io.confluent.ksql.util.KsqlStatementException)2 IOException (java.io.IOException)2