Search in sources :

Example 6 with Subjects

use of io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects 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 7 with Subjects

use of io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects 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 8 with Subjects

use of io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects 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 9 with Subjects

use of io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects 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)

Example 10 with Subjects

use of io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects in project hopsworks by logicalclocks.

the class KafkaController method updateTopicSchemaVersion.

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

Aggregations

Subjects (io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)11 SchemaException (io.hops.hopsworks.exceptions.SchemaException)7 KafkaException (io.hops.hopsworks.exceptions.KafkaException)5 SubjectDTO (io.hops.hopsworks.common.dao.kafka.schemas.SubjectDTO)4 ProjectTopics (io.hops.hopsworks.persistence.entity.kafka.ProjectTopics)4 Schema (org.apache.avro.Schema)4 SchemaParseException (org.apache.avro.SchemaParseException)4 BigInteger (java.math.BigInteger)3 CompatibilityCheck (io.hops.hopsworks.common.dao.kafka.schemas.CompatibilityCheck)2 SchemaCompatibility (io.hops.hopsworks.persistence.entity.kafka.schemas.SchemaCompatibility)2 Schemas (io.hops.hopsworks.persistence.entity.kafka.schemas.Schemas)2 ProjectTopicsFacade (io.hops.hopsworks.common.dao.kafka.ProjectTopicsFacade)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 ServingException (io.hops.hopsworks.exceptions.ServingException)1 SubjectsCompatibility (io.hops.hopsworks.persistence.entity.kafka.schemas.SubjectsCompatibility)1 Project (io.hops.hopsworks.persistence.entity.project.Project)1 RESTCodes (io.hops.hopsworks.restutils.RESTCodes)1 Arrays (java.util.Arrays)1