Search in sources :

Example 1 with SchemaValidationException

use of org.apache.avro.SchemaValidationException in project hopsworks by logicalclocks.

the class SubjectsController method isCompatible.

private boolean isCompatible(Project project, String subject, Schema schema) throws SchemaException {
    SchemaCompatibility sc = getSubjectOrProjectCompatibility(project, subject);
    if (sc.equals(SchemaCompatibility.NONE)) {
        return true;
    }
    SchemaValidator validator = getSchemaValidator(sc);
    List<Schema> previousSchemas = subjectsFacade.findSubjectByName(project, subject).stream().sorted(Comparator.comparing(Subjects::getVersion).reversed()).map(s -> new Schema.Parser().parse(s.getSchema().getSchema())).collect(Collectors.toList());
    try {
        validator.validate(schema, previousSchemas);
    } catch (SchemaValidationException e) {
        return false;
    }
    return true;
}
Also used : Arrays(java.util.Arrays) SubjectDTO(io.hops.hopsworks.common.dao.kafka.schemas.SubjectDTO) Schemas(io.hops.hopsworks.persistence.entity.kafka.schemas.Schemas) CompatibilityCheck(io.hops.hopsworks.common.dao.kafka.schemas.CompatibilityCheck) Project(io.hops.hopsworks.persistence.entity.project.Project) Level(java.util.logging.Level) ProjectTopicsFacade(io.hops.hopsworks.common.dao.kafka.ProjectTopicsFacade) SubjectsCompatibility(io.hops.hopsworks.persistence.entity.kafka.schemas.SubjectsCompatibility) Settings(io.hops.hopsworks.common.util.Settings) TransactionAttributeType(javax.ejb.TransactionAttributeType) TransactionAttribute(javax.ejb.TransactionAttribute) SubjectsFacade(io.hops.hopsworks.common.dao.kafka.schemas.SubjectsFacade) SubjectsCompatibilityFacade(io.hops.hopsworks.common.dao.kafka.schemas.SubjectsCompatibilityFacade) BigInteger(java.math.BigInteger) EJB(javax.ejb.EJB) SchemaCompatibility(io.hops.hopsworks.persistence.entity.kafka.schemas.SchemaCompatibility) Stateless(javax.ejb.Stateless) Schema(org.apache.avro.Schema) SchemaValidator(org.apache.avro.SchemaValidator) SchemaValidationException(org.apache.avro.SchemaValidationException) SchemaException(io.hops.hopsworks.exceptions.SchemaException) RESTCodes(io.hops.hopsworks.restutils.RESTCodes) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) List(java.util.List) Subjects(io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects) SchemaValidatorBuilder(org.apache.avro.SchemaValidatorBuilder) Optional(java.util.Optional) SchemaParseException(org.apache.avro.SchemaParseException) Comparator(java.util.Comparator) KafkaException(io.hops.hopsworks.exceptions.KafkaException) Collections(java.util.Collections) SchemaValidationException(org.apache.avro.SchemaValidationException) SchemaCompatibility(io.hops.hopsworks.persistence.entity.kafka.schemas.SchemaCompatibility) Schema(org.apache.avro.Schema) SchemaValidator(org.apache.avro.SchemaValidator) Subjects(io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)

Example 2 with SchemaValidationException

use of org.apache.avro.SchemaValidationException in project apicurio-registry by Apicurio.

the class AvroCompatibilityChecker method testCompatibility.

/**
 * @see CompatibilityChecker#testCompatibility(io.apicurio.registry.rules.compatibility.CompatibilityLevel, java.util.List, java.lang.String)
 */
@Override
public CompatibilityExecutionResult testCompatibility(CompatibilityLevel compatibilityLevel, List<String> existingSchemaStrings, String proposedSchemaString) {
    requireNonNull(compatibilityLevel, "compatibilityLevel MUST NOT be null");
    requireNonNull(existingSchemaStrings, "existingSchemaStrings MUST NOT be null");
    requireNonNull(proposedSchemaString, "proposedSchemaString MUST NOT be null");
    SchemaValidator schemaValidator = validatorFor(compatibilityLevel);
    if (schemaValidator == null) {
        return CompatibilityExecutionResult.compatible();
    }
    try {
        List<Schema> existingSchemas = existingSchemaStrings.stream().map(s -> new Schema.Parser().parse(s)).collect(Collectors.toList());
        // the most recent must come first, i.e. reverse-chronological.
        Collections.reverse(existingSchemas);
        Schema toValidate = new Schema.Parser().parse(proposedSchemaString);
        schemaValidator.validate(toValidate, existingSchemas);
        return CompatibilityExecutionResult.compatible();
    } catch (SchemaValidationException e) {
        return CompatibilityExecutionResult.incompatible(e);
    } catch (SchemaParseException | AvroTypeException e) {
        throw new UnprocessableSchemaException(e.getMessage());
    }
}
Also used : List(java.util.List) Schema(org.apache.avro.Schema) SchemaValidator(org.apache.avro.SchemaValidator) AvroTypeException(org.apache.avro.AvroTypeException) SchemaValidationException(org.apache.avro.SchemaValidationException) Objects.requireNonNull(java.util.Objects.requireNonNull) SchemaValidatorBuilder(org.apache.avro.SchemaValidatorBuilder) UnprocessableSchemaException(io.apicurio.registry.rules.UnprocessableSchemaException) SchemaParseException(org.apache.avro.SchemaParseException) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) SchemaValidationException(org.apache.avro.SchemaValidationException) SchemaParseException(org.apache.avro.SchemaParseException) Schema(org.apache.avro.Schema) SchemaValidator(org.apache.avro.SchemaValidator) UnprocessableSchemaException(io.apicurio.registry.rules.UnprocessableSchemaException) AvroTypeException(org.apache.avro.AvroTypeException)

Aggregations

Collections (java.util.Collections)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Schema (org.apache.avro.Schema)2 SchemaParseException (org.apache.avro.SchemaParseException)2 SchemaValidationException (org.apache.avro.SchemaValidationException)2 SchemaValidator (org.apache.avro.SchemaValidator)2 SchemaValidatorBuilder (org.apache.avro.SchemaValidatorBuilder)2 UnprocessableSchemaException (io.apicurio.registry.rules.UnprocessableSchemaException)1 ProjectTopicsFacade (io.hops.hopsworks.common.dao.kafka.ProjectTopicsFacade)1 CompatibilityCheck (io.hops.hopsworks.common.dao.kafka.schemas.CompatibilityCheck)1 SubjectDTO (io.hops.hopsworks.common.dao.kafka.schemas.SubjectDTO)1 SubjectsCompatibilityFacade (io.hops.hopsworks.common.dao.kafka.schemas.SubjectsCompatibilityFacade)1 SubjectsFacade (io.hops.hopsworks.common.dao.kafka.schemas.SubjectsFacade)1 Settings (io.hops.hopsworks.common.util.Settings)1 KafkaException (io.hops.hopsworks.exceptions.KafkaException)1 SchemaException (io.hops.hopsworks.exceptions.SchemaException)1 SchemaCompatibility (io.hops.hopsworks.persistence.entity.kafka.schemas.SchemaCompatibility)1 Schemas (io.hops.hopsworks.persistence.entity.kafka.schemas.Schemas)1 Subjects (io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)1