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);
}
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);
}
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);
}
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());
}
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);
}
}
Aggregations