Search in sources :

Example 11 with SchemaException

use of io.hops.hopsworks.exceptions.SchemaException in project hopsworks by logicalclocks.

the class KafkaController method updateTopicSubjectVersion.

public void updateTopicSubjectVersion(Project project, String topic, String subject, Integer version) throws KafkaException, SchemaException {
    ProjectTopics pt = projectTopicsFacade.findTopicByNameAndProject(project, topic).orElseThrow(() -> new KafkaException(RESTCodes.KafkaErrorCode.TOPIC_NOT_FOUND, Level.FINE, "topic: " + topic));
    String topicSubject = pt.getSubjects().getSubject();
    Subjects st = subjectsFacade.findSubjectByNameAndVersion(project, subject, version).orElseThrow(() -> new SchemaException(RESTCodes.SchemaRegistryErrorCode.VERSION_NOT_FOUND, Level.FINE, "schema: " + topicSubject + ", version: " + version));
    projectTopicsFacade.updateTopicSchemaVersion(pt, st);
}
Also used : SchemaException(io.hops.hopsworks.exceptions.SchemaException) ProjectTopics(io.hops.hopsworks.persistence.entity.kafka.ProjectTopics) KafkaException(io.hops.hopsworks.exceptions.KafkaException) Subjects(io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)

Example 12 with SchemaException

use of io.hops.hopsworks.exceptions.SchemaException in project hopsworks by logicalclocks.

the class SubjectsController method registerNewSubject.

public SubjectDTO registerNewSubject(Project project, String subject, String schemaContent, boolean isEnablingKafkaService) throws KafkaException, SchemaException {
    validateSubject(subject, isEnablingKafkaService);
    Schema schema;
    try {
        schema = new Schema.Parser().parse(schemaContent);
    } catch (SchemaParseException e) {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.INVALID_AVRO_SCHEMA, Level.FINE, "schema=" + schemaContent);
    }
    // check if schema exists - return current id
    Optional<Subjects> optionalSubject = subjectsFacade.findSubjectByNameAndSchema(project, subject, schema.toString());
    if (optionalSubject.isPresent()) {
        Subjects subjects = optionalSubject.get();
        return new SubjectDTO(subjects.getSchema().getId(), subjects.getSubject(), subjects.getVersion());
    }
    // check if schema compatible - return 409 of not
    if (!isCompatible(project, subject, schema)) {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.INCOMPATIBLE_AVRO_SCHEMA, Level.FINE, "Subject=" + subject + ", project=" + project.getName());
    }
    Integer latestVersion = subjectsFacade.findSubjectByName(project, subject).stream().map(Subjects::getVersion).max(Integer::compareTo).orElse(0);
    Schemas schemas = schemasController.addNewSchema(project, schema.toString());
    Integer id = subjectsFacade.insertNewSubject(project, subject, schemas, latestVersion + 1);
    return new SubjectDTO(id, subject, latestVersion + 1);
}
Also used : BigInteger(java.math.BigInteger) SchemaException(io.hops.hopsworks.exceptions.SchemaException) SubjectDTO(io.hops.hopsworks.common.dao.kafka.schemas.SubjectDTO) SchemaParseException(org.apache.avro.SchemaParseException) Schema(org.apache.avro.Schema) Schemas(io.hops.hopsworks.persistence.entity.kafka.schemas.Schemas) Subjects(io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)

Example 13 with SchemaException

use of io.hops.hopsworks.exceptions.SchemaException in project hopsworks by logicalclocks.

the class SubjectsController method checkIfSchemaCompatible.

public CompatibilityCheck checkIfSchemaCompatible(Project project, String subject, String version, String schemaToTest) throws SchemaException {
    validateVersion(version);
    Schema schema;
    try {
        schema = new Schema.Parser().parse(schemaToTest);
    } catch (SchemaParseException e) {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.INVALID_AVRO_SCHEMA, Level.FINE, "schema=" + schemaToTest);
    }
    if (!subjectsFacade.getListOfSubjects(project).contains(subject)) {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.SUBJECT_NOT_FOUND, Level.FINE, "subject=" + subject);
    }
    SchemaCompatibility sc = getSubjectOrProjectCompatibility(project, subject);
    Optional<Subjects> optional;
    if (version.equals("latest")) {
        optional = subjectsFacade.findSubjectLatestVersion(project, subject);
    } else {
        optional = subjectsFacade.findSubjectByNameAndVersion(project, subject, Integer.valueOf(version));
    }
    if (!optional.isPresent()) {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.VERSION_NOT_FOUND, Level.FINE, "project=" + project.getName() + ", subject=" + subject + ", version=" + version);
    }
    boolean isCompatible = isCompatible(new Schema.Parser().parse(optional.get().getSchema().getSchema()), schema, sc);
    return new CompatibilityCheck(isCompatible);
}
Also used : CompatibilityCheck(io.hops.hopsworks.common.dao.kafka.schemas.CompatibilityCheck) SchemaException(io.hops.hopsworks.exceptions.SchemaException) SchemaParseException(org.apache.avro.SchemaParseException) SchemaCompatibility(io.hops.hopsworks.persistence.entity.kafka.schemas.SchemaCompatibility) Schema(org.apache.avro.Schema) Subjects(io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)

Example 14 with SchemaException

use of io.hops.hopsworks.exceptions.SchemaException in project hopsworks by logicalclocks.

the class SubjectsController method checkIfSchemaRegistered.

public SubjectDTO checkIfSchemaRegistered(Project project, String subject, String schemaContent) throws SchemaException {
    if (!subjectsFacade.getListOfSubjects(project).contains(subject)) {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.SUBJECT_NOT_FOUND, Level.FINE, "subject=" + subject);
    }
    Schema schema;
    try {
        schema = new Schema.Parser().parse(schemaContent);
    } catch (SchemaParseException e) {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.INVALID_AVRO_SCHEMA, Level.FINE, "schema=" + schemaContent);
    }
    Optional<Subjects> optional = subjectsFacade.findSubjectByNameAndSchema(project, subject, schema.toString());
    if (!optional.isPresent()) {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.SCHEMA_NOT_FOUND, Level.FINE, "schema=" + schema.toString());
    }
    return new SubjectDTO(optional.get());
}
Also used : SchemaException(io.hops.hopsworks.exceptions.SchemaException) SubjectDTO(io.hops.hopsworks.common.dao.kafka.schemas.SubjectDTO) SchemaParseException(org.apache.avro.SchemaParseException) Schema(org.apache.avro.Schema) Subjects(io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)

Example 15 with SchemaException

use of io.hops.hopsworks.exceptions.SchemaException in project hopsworks by logicalclocks.

the class SubjectsController method getSubjectDetails.

public SubjectDTO getSubjectDetails(Project project, String subject, String version) throws SchemaException {
    validateVersion(version);
    if (subjectsFacade.findSubjectByName(project, subject).isEmpty()) {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.SUBJECT_NOT_FOUND, Level.FINE, "subject=" + subject);
    }
    Optional<Subjects> optional;
    if (version.equals("latest")) {
        optional = subjectsFacade.findSubjectLatestVersion(project, subject);
    } else {
        optional = subjectsFacade.findSubjectByNameAndVersion(project, subject, Integer.valueOf(version));
    }
    if (optional.isPresent()) {
        Subjects res = optional.get();
        return new SubjectDTO(res.getSchema().getId(), res.getSubject(), res.getVersion(), res.getSchema().getSchema());
    } else {
        throw new SchemaException(RESTCodes.SchemaRegistryErrorCode.VERSION_NOT_FOUND, Level.FINE, "subject=" + subject + ", version=" + version);
    }
}
Also used : SchemaException(io.hops.hopsworks.exceptions.SchemaException) SubjectDTO(io.hops.hopsworks.common.dao.kafka.schemas.SubjectDTO) Subjects(io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)

Aggregations

SchemaException (io.hops.hopsworks.exceptions.SchemaException)15 Subjects (io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)7 KafkaException (io.hops.hopsworks.exceptions.KafkaException)6 Path (javax.ws.rs.Path)5 Produces (javax.ws.rs.Produces)5 AllowedProjectRoles (io.hops.hopsworks.api.filter.AllowedProjectRoles)4 ApiKeyRequired (io.hops.hopsworks.api.filter.apiKey.ApiKeyRequired)4 SchemaRegistryError (io.hops.hopsworks.common.dao.kafka.schemas.SchemaRegistryError)4 SubjectDTO (io.hops.hopsworks.common.dao.kafka.schemas.SubjectDTO)4 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)4 ApiOperation (io.swagger.annotations.ApiOperation)4 BigInteger (java.math.BigInteger)4 GenericEntity (javax.ws.rs.core.GenericEntity)4 SchemaCompatibility (io.hops.hopsworks.persistence.entity.kafka.schemas.SchemaCompatibility)3 Project (io.hops.hopsworks.persistence.entity.project.Project)3 Schema (org.apache.avro.Schema)3 SchemaParseException (org.apache.avro.SchemaParseException)3 DistributedFileSystemOps (io.hops.hopsworks.common.hdfs.DistributedFileSystemOps)2 ProvTypeDTO (io.hops.hopsworks.common.provenance.core.dto.ProvTypeDTO)2 DatasetException (io.hops.hopsworks.exceptions.DatasetException)2