Search in sources :

Example 6 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project schema-registry by confluentinc.

the class KafkaSchemaRegistry method loadSchema.

private ParsedSchema loadSchema(String schemaType, String schema, List<io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference> references, boolean isNew) throws InvalidSchemaException {
    if (schemaType == null) {
        schemaType = AvroSchema.TYPE;
    }
    SchemaProvider provider = schemaProvider(schemaType);
    if (provider == null) {
        String errMsg = "Invalid schema type " + schemaType;
        log.error(errMsg);
        throw new InvalidSchemaException(errMsg);
    }
    final String type = schemaType;
    try {
        return provider.parseSchemaOrElseThrow(schema, references, isNew);
    } catch (Exception e) {
        throw new InvalidSchemaException("Invalid schema " + schema + " with refs " + references + " of type " + type + ", details: " + e.getMessage());
    }
}
Also used : InvalidSchemaException(io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) SchemaString(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString) IncompatibleSchemaException(io.confluent.kafka.schemaregistry.exceptions.IncompatibleSchemaException) RestException(io.confluent.rest.exceptions.RestException) SchemaRegistryException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryException) TimeoutException(java.util.concurrent.TimeoutException) ReferenceExistsException(io.confluent.kafka.schemaregistry.exceptions.ReferenceExistsException) InvalidSchemaException(io.confluent.kafka.schemaregistry.exceptions.InvalidSchemaException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException) SchemaRegistryStoreException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryStoreException) StoreTimeoutException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreTimeoutException) UnknownLeaderException(io.confluent.kafka.schemaregistry.exceptions.UnknownLeaderException) StoreException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreException) OperationNotPermittedException(io.confluent.kafka.schemaregistry.exceptions.OperationNotPermittedException) SchemaVersionNotSoftDeletedException(io.confluent.kafka.schemaregistry.exceptions.SchemaVersionNotSoftDeletedException) IdGenerationException(io.confluent.kafka.schemaregistry.exceptions.IdGenerationException) SchemaRegistryTimeoutException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryTimeoutException) SubjectNotSoftDeletedException(io.confluent.kafka.schemaregistry.exceptions.SubjectNotSoftDeletedException) IOException(java.io.IOException) IdDoesNotMatchException(io.confluent.kafka.schemaregistry.exceptions.IdDoesNotMatchException) SchemaRegistryInitializationException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryInitializationException) ExecutionException(java.util.concurrent.ExecutionException) StoreInitializationException(io.confluent.kafka.schemaregistry.storage.exceptions.StoreInitializationException) SchemaRegistryRequestForwardingException(io.confluent.kafka.schemaregistry.exceptions.SchemaRegistryRequestForwardingException)

Example 7 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project schema-registry by confluentinc.

the class JsonSchemaTest method testParseSchemaThrowException.

@Test(expected = IllegalArgumentException.class)
public void testParseSchemaThrowException() {
    SchemaProvider jsonSchemaProvider = new JsonSchemaProvider();
    jsonSchemaProvider.parseSchemaOrElseThrow(invalidSchemaString, new ArrayList<>(), false);
}
Also used : SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) Test(org.junit.Test)

Example 8 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project schema-registry by confluentinc.

the class TestLocalCompatibilityMojo method execute.

public void execute() throws MojoExecutionException {
    List<SchemaProvider> providers = MojoUtils.defaultSchemaProviders();
    Map<String, SchemaProvider> schemaProviders = providers.stream().collect(Collectors.toMap(SchemaProvider::schemaType, p -> p));
    getLog().debug(String.format("Loading Schema at %s", schemaPath));
    ParsedSchema schema = loadSchema(schemaPath, schemaProviders);
    getLog().debug("Loading Previous Schemas");
    ArrayList<ParsedSchema> previousSchemas = new ArrayList<>();
    for (File previousSchemaPath : previousSchemaPaths) {
        previousSchemas.add(loadSchema(previousSchemaPath, schemaProviders));
    }
    CompatibilityChecker checker = CompatibilityChecker.checker(compatibilityLevel);
    List<String> errorMessages = checker.isCompatible(schema, previousSchemas);
    if (previousSchemas.size() > 1 && (compatibilityLevel == CompatibilityLevel.BACKWARD || compatibilityLevel == CompatibilityLevel.FORWARD || compatibilityLevel == CompatibilityLevel.FULL)) {
        getLog().info(String.format("Checking only with latest Schema at %s", previousSchemaPaths.get(previousSchemaPaths.size() - 1)));
    }
    success = errorMessages.isEmpty();
    if (success) {
        getLog().info(String.format("Schema is %s compatible with previous schemas", compatibilityLevel.name.toLowerCase()));
    } else {
        String errorLog = String.format("Schema is not %s compatible with previous schemas %n", compatibilityLevel.name.toLowerCase()) + errorMessages.get(0);
        getLog().error(errorLog);
    }
}
Also used : IOException(java.io.IOException) CompatibilityChecker(io.confluent.kafka.schemaregistry.CompatibilityChecker) Parameter(org.apache.maven.plugins.annotations.Parameter) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) Collectors(java.util.stream.Collectors) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ArrayList(java.util.ArrayList) Mojo(org.apache.maven.plugins.annotations.Mojo) List(java.util.List) CompatibilityLevel(io.confluent.kafka.schemaregistry.CompatibilityLevel) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) Map(java.util.Map) Optional(java.util.Optional) SchemaReference(io.confluent.kafka.schemaregistry.client.rest.entities.SchemaReference) AbstractMojo(org.apache.maven.plugin.AbstractMojo) ArrayList(java.util.ArrayList) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) ParsedSchema(io.confluent.kafka.schemaregistry.ParsedSchema) File(java.io.File) CompatibilityChecker(io.confluent.kafka.schemaregistry.CompatibilityChecker)

Example 9 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project schema-registry by confluentinc.

the class SchemaMessageReader method parseSchema.

protected ParsedSchema parseSchema(SchemaRegistryClient schemaRegistry, String schema, List<SchemaReference> references) {
    SchemaProvider provider = getProvider();
    provider.configure(Collections.singletonMap(SchemaProvider.SCHEMA_VERSION_FETCHER_CONFIG, schemaRegistry));
    return provider.parseSchema(schema, references).get();
}
Also used : SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider)

Example 10 with SchemaProvider

use of io.confluent.kafka.schemaregistry.SchemaProvider in project akhq by tchiotludo.

the class KafkaModule method getRegistryClient.

public SchemaRegistryClient getRegistryClient(String clusterId) {
    if (!this.registryClient.containsKey(clusterId)) {
        Connection connection = this.getConnection(clusterId);
        List<SchemaProvider> providers = new ArrayList<>();
        providers.add(new AvroSchemaProvider());
        providers.add(new JsonSchemaProvider());
        providers.add(new ProtobufSchemaProvider());
        SchemaRegistryClient client = new CachedSchemaRegistryClient(this.getRegistryRestClient(clusterId), 1000, providers, connection.getSchemaRegistry() != null ? connection.getSchemaRegistry().getProperties() : null, null);
        this.registryClient.put(clusterId, client);
    }
    return this.registryClient.get(clusterId);
}
Also used : JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) Connection(org.akhq.configs.Connection) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) SchemaProvider(io.confluent.kafka.schemaregistry.SchemaProvider) JsonSchemaProvider(io.confluent.kafka.schemaregistry.json.JsonSchemaProvider) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) ProtobufSchemaProvider(io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider) CachedSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient) AvroSchemaProvider(io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider) SchemaRegistryClient(io.confluent.kafka.schemaregistry.client.SchemaRegistryClient) CachedSchemaRegistryClient(io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient)

Aggregations

SchemaProvider (io.confluent.kafka.schemaregistry.SchemaProvider)22 ParsedSchema (io.confluent.kafka.schemaregistry.ParsedSchema)9 AvroSchemaProvider (io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider)7 JsonSchemaProvider (io.confluent.kafka.schemaregistry.json.JsonSchemaProvider)7 ProtobufSchemaProvider (io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider)7 BadRequestException (io.confluent.kafkarest.exceptions.BadRequestException)5 IOException (java.io.IOException)5 Test (org.junit.Test)5 CachedSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient)4 SchemaRegistryClient (io.confluent.kafka.schemaregistry.client.SchemaRegistryClient)4 RestClientException (io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)3 EmbeddedFormat (io.confluent.kafkarest.entities.EmbeddedFormat)3 Test (org.junit.jupiter.api.Test)3 MockSchemaRegistryClient (io.confluent.kafka.schemaregistry.client.MockSchemaRegistryClient)2 SchemaString (io.confluent.kafka.schemaregistry.client.rest.entities.SchemaString)2 StoreException (io.confluent.kafka.schemaregistry.storage.exceptions.StoreException)2 TopicNameStrategy (io.confluent.kafka.serializers.subject.TopicNameStrategy)2 HashMap (java.util.HashMap)2 SchemaParseException (org.apache.avro.SchemaParseException)2 CompatibilityChecker (io.confluent.kafka.schemaregistry.CompatibilityChecker)1