Search in sources :

Example 16 with SchemaVersionKey

use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project registry by hortonworks.

the class AvroSchemaRegistryClientTest method testDeletingNonExistingSchema.

@Test(expected = SchemaNotFoundException.class)
public void testDeletingNonExistingSchema() throws Exception {
    SchemaVersionKey schemaVersionKey = addAndDeleteSchemaVersion(TEST_NAME_RULE.getMethodName());
    // deleting again should return SchemaNotFoundException
    SCHEMA_REGISTRY_CLIENT.deleteSchemaVersion(schemaVersionKey);
}
Also used : SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) IntegrationTest(com.hortonworks.registries.common.test.IntegrationTest) Test(org.junit.Test)

Example 17 with SchemaVersionKey

use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project registry by hortonworks.

the class AvroSchemaRegistryClientTest method addAndDeleteSchemaVersion.

private SchemaVersionKey addAndDeleteSchemaVersion(String schemaName) throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, IOException, SchemaBranchNotFoundException, SchemaLifecycleException {
    SchemaMetadata schemaMetadata = createSchemaMetadata(schemaName, SchemaCompatibility.BOTH);
    SchemaIdVersion schemaIdVersion = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaMetadata, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/device.avsc"), "Initial version of the schema"));
    SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaMetadata.getName(), schemaIdVersion.getVersion());
    SCHEMA_REGISTRY_CLIENT.deleteSchemaVersion(schemaVersionKey);
    return schemaVersionKey;
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey)

Example 18 with SchemaVersionKey

use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project registry by hortonworks.

the class SchemaRegistryResource method deleteSchemaVersion.

@DELETE
@Path("/schemas/{name}/versions/{version}")
@ApiOperation(value = "Delete a schema version given its schema name and version id", tags = OPERATION_GROUP_SCHEMA)
@UnitOfWork
public Response deleteSchemaVersion(@ApiParam(value = "Schema name", required = true) @PathParam("name") String schemaName, @ApiParam(value = "version of the schema", required = true) @PathParam("version") Integer versionNumber, @Context UriInfo uriInfo) {
    SchemaVersionKey schemaVersionKey = null;
    try {
        schemaVersionKey = new SchemaVersionKey(schemaName, versionNumber);
        schemaRegistry.deleteSchemaVersion(schemaVersionKey);
        return WSUtils.respond(Response.Status.OK);
    } catch (SchemaNotFoundException e) {
        LOG.error("No schemaVersion found with name: [{}], version : [{}]", schemaName, versionNumber);
        return WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, schemaVersionKey.toString());
    } catch (SchemaLifecycleException e) {
        LOG.error("Failed to delete schema name: [{}], version : [{}]", schemaName, versionNumber, e);
        return WSUtils.respond(Response.Status.BAD_REQUEST, CatalogResponse.ResponseMessage.BAD_REQUEST_WITH_MESSAGE, e.getMessage());
    } catch (Exception ex) {
        LOG.error("Encountered error while deleting schemaVersion with name: [{}], version : [{}]", schemaName, versionNumber, ex);
        return WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage());
    }
}
Also used : SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) FileNotFoundException(java.io.FileNotFoundException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) IOException(java.io.IOException) Path(javax.ws.rs.Path) UnitOfWork(com.hortonworks.registries.common.transaction.UnitOfWork) DELETE(javax.ws.rs.DELETE) ApiOperation(io.swagger.annotations.ApiOperation)

Example 19 with SchemaVersionKey

use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project registry by hortonworks.

the class AvroSchemaRegistryTest method testRegistrySchemaOps.

@Test
public void testRegistrySchemaOps() throws Exception {
    SchemaCompatibility compatibility = SchemaCompatibility.BOTH;
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(schemaName).type(AvroSchemaProvider.TYPE).description("devices schema").compatibility(compatibility).schemaGroup(SCHEMA_GROUP).build();
    Long schemaMetadataId = schemaRegistry.registerSchemaMetadata(schemaMetadata);
    SchemaMetadata schemaMetadataReturned = schemaRegistry.getSchemaMetadataInfo(schemaMetadataId).getSchemaMetadata();
    Assert.assertEquals(schemaMetadata, schemaMetadataReturned);
    Integer v1 = schemaRegistry.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "initial version of the schema")).getVersion();
    Integer v2 = schemaRegistry.addSchemaVersion(schemaName, new SchemaVersion(schema2, "second version of the the schema")).getVersion();
    Assert.assertTrue(v2 == v1 + 1);
    Collection<SchemaVersionInfo> allSchemaVersions = schemaRegistry.getAllVersions(schemaName);
    Assert.assertTrue(allSchemaVersions.size() == 2);
    SchemaMetadataInfo schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(schemaName);
    Assert.assertEquals(schemaMetadata, schemaMetadataInfo.getSchemaMetadata());
    Integer schemaVersion = schemaRegistry.getSchemaVersionInfo(schemaName, schema1).getVersion();
    Assert.assertEquals(v1, schemaVersion);
    SchemaVersionInfo schemaVersionInfo1 = schemaRegistry.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v1));
    Assert.assertEquals(schemaVersionInfo1.getSchemaText(), schema1);
    SchemaVersionInfo schemaVersionInfo2 = schemaRegistry.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v2));
    Assert.assertEquals(schemaVersionInfo2.getSchemaText(), schema2);
    // receive the same version as earlier without adding a new schema entry as it exists in the same schema group.
    Integer version = schemaRegistry.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "already added schema")).getVersion();
    Assert.assertEquals(version, v1);
    // aggregate apis
    AggregatedSchemaMetadataInfo aggregatedSchemaMetadata = schemaRegistry.getAggregatedSchemaMetadataInfo(schemaName);
    Assert.assertEquals(allSchemaVersions.size(), aggregatedSchemaMetadata.getSchemaBranches().iterator().next().getSchemaVersionInfos().size());
    Assert.assertTrue(aggregatedSchemaMetadata.getSerDesInfos().isEmpty());
    Collection<AggregatedSchemaMetadataInfo> aggregatedSchemaMetadataCollection = schemaRegistry.findAggregatedSchemaMetadata(Collections.emptyMap());
    Assert.assertEquals(1, aggregatedSchemaMetadataCollection.size());
    // Serializing and deserializing AggregatedSchemaMetadataInfo should not throw any errors
    String aggregateSchemaMetadataStr = new ObjectMapper().writeValueAsString(aggregatedSchemaMetadataCollection);
    Collection<AggregatedSchemaMetadataInfo> returnedResult = new ObjectMapper().readValue(aggregateSchemaMetadataStr, new TypeReference<Collection<AggregatedSchemaMetadataInfo>>() {
    });
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaCompatibility(com.hortonworks.registries.schemaregistry.SchemaCompatibility) Collection(java.util.Collection) AggregatedSchemaMetadataInfo(com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AggregatedSchemaMetadataInfo(com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Test(org.junit.Test)

Example 20 with SchemaVersionKey

use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project registry by hortonworks.

the class ConfluentSchemaRegistryCompatibleResource method getSchemaVersion.

@GET
@Path("/subjects/{subject}/versions/{versionId}")
@ApiOperation(value = "Get the schema information for given subject and versionId", response = Integer.class, responseContainer = "Collection", tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response getSchemaVersion(@ApiParam(value = "subject", required = true) @PathParam("subject") String subject, @ApiParam(value = "versionId", required = true) @PathParam("versionId") String versionId) {
    Response response;
    try {
        SchemaVersionInfo schemaVersionInfo = null;
        if ("latest".equals(versionId)) {
            schemaVersionInfo = schemaRegistry.getLatestSchemaVersionInfo(subject);
        } else {
            SchemaMetadataInfo schemaMetadataInfo = schemaRegistry.getSchemaMetadataInfo(subject);
            if (schemaMetadataInfo == null) {
                throw new SchemaNotFoundException();
            }
            SchemaVersionInfo fetchedSchemaVersionInfo = null;
            try {
                Integer version = Integer.valueOf(versionId);
                if (version > 0 && version <= Integer.MAX_VALUE) {
                    fetchedSchemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaVersionKey(subject, version));
                } else {
                    LOG.error("versionId is not in valid range [{}, {}] ", 1, Integer.MAX_VALUE);
                }
            } catch (NumberFormatException e) {
                LOG.error("Invalid version id string ", versionId, e);
            } catch (SchemaNotFoundException e) {
                LOG.error("Schema version not found with version id [{}]", versionId, e);
            }
            if (fetchedSchemaVersionInfo != null) {
                if (subject.equals(fetchedSchemaVersionInfo.getName())) {
                    schemaVersionInfo = fetchedSchemaVersionInfo;
                } else {
                    LOG.error("Received schema version for id [{}] belongs to subject [{}] which is different from requested subject [{}]", versionId, fetchedSchemaVersionInfo.getName(), subject);
                }
            }
        }
        if (schemaVersionInfo == null) {
            response = versionNotFoundError();
        } else {
            Schema schema = new Schema(schemaVersionInfo.getName(), schemaVersionInfo.getVersion(), schemaVersionInfo.getId(), schemaVersionInfo.getSchemaText());
            response = WSUtils.respondEntity(schema, Response.Status.OK);
        }
    } catch (SchemaNotFoundException ex) {
        LOG.error("No schema found with subject [{}]", subject, ex);
        response = subjectNotFoundError();
    } catch (Exception ex) {
        LOG.error("Encountered error while retrieving all subjects", ex);
        response = serverError();
    }
    return response;
}
Also used : CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) Response(javax.ws.rs.core.Response) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) IOException(java.io.IOException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Path(javax.ws.rs.Path) UnitOfWork(com.hortonworks.registries.common.transaction.UnitOfWork) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)22 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)11 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)8 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)8 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)7 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)7 IOException (java.io.IOException)7 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)6 Schema (org.apache.avro.Schema)6 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)5 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)5 Test (org.junit.Test)5 Path (javax.ws.rs.Path)4 Timed (com.codahale.metrics.annotation.Timed)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)3 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)3 SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)3 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)3 ApiOperation (io.swagger.annotations.ApiOperation)3