Search in sources :

Example 1 with Schemas

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

the class SchemasController method addNewSchema.

public Schemas addNewSchema(Project project, String schemaString) throws SchemaException {
    Schema schema = validateSchema(project, schemaString);
    Optional<Schemas> current = schemasFacade.findBySchema(project, schema.toString());
    if (current.isPresent()) {
        return current.get();
    } else {
        Schemas s = new Schemas(schema.toString(), project);
        schemasFacade.save(s);
        return s;
    }
}
Also used : Schema(org.apache.avro.Schema) Schemas(io.hops.hopsworks.persistence.entity.kafka.schemas.Schemas)

Example 2 with Schemas

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

Aggregations

Schemas (io.hops.hopsworks.persistence.entity.kafka.schemas.Schemas)2 Schema (org.apache.avro.Schema)2 SubjectDTO (io.hops.hopsworks.common.dao.kafka.schemas.SubjectDTO)1 SchemaException (io.hops.hopsworks.exceptions.SchemaException)1 Subjects (io.hops.hopsworks.persistence.entity.kafka.schemas.Subjects)1 BigInteger (java.math.BigInteger)1 SchemaParseException (org.apache.avro.SchemaParseException)1