Search in sources :

Example 11 with SchemaVersion

use of com.hortonworks.registries.schemaregistry.SchemaVersion in project streamline by hortonworks.

the class SchemaResource method postStreamsSchema.

@POST
@Timed
@Produces(MediaType.APPLICATION_JSON)
public Response postStreamsSchema(StreamsSchemaInfo streamsSchemaInfo, @Context SecurityContext securityContext) throws IOException {
    Preconditions.checkNotNull(streamsSchemaInfo, "streamsSchemaInfo can not be null");
    SchemaIdVersion schemaIdVersion = null;
    SchemaMetadata schemaMetadata = streamsSchemaInfo.getSchemaMetadata();
    String schemaName = schemaMetadata.getName();
    Long schemaMetadataId = schemaRegistryClient.registerSchemaMetadata(schemaMetadata);
    LOG.info("Registered schemaMetadataId [{}] for schema with name:[{}]", schemaMetadataId, schemaName);
    String streamsSchemaText = streamsSchemaInfo.getSchemaVersion().getSchemaText();
    try {
        // convert streams schema to avro schema.
        String avroSchemaText = AvroStreamlineSchemaConverter.convertStreamlineSchemaToAvroSchema(streamsSchemaText);
        SchemaVersion avroSchemaVersion = new SchemaVersion(avroSchemaText, streamsSchemaInfo.getSchemaVersion().getDescription());
        schemaIdVersion = schemaRegistryClient.addSchemaVersion(schemaName, avroSchemaVersion);
    } catch (InvalidSchemaException e) {
        String errMsg = String.format("Invalid schema received for schema with name [%s] : [%s]", schemaName, streamsSchemaText);
        LOG.error(errMsg, e);
        throw BadRequestException.message(errMsg, e);
    } catch (SchemaNotFoundException e) {
        String errMsg = String.format("Schema not found for topic: [%s]", schemaName);
        LOG.error(errMsg, e);
        throw EntityNotFoundException.byId(schemaName);
    } catch (IncompatibleSchemaException e) {
        String errMsg = String.format("Incompatible schema received for schema with name [%s] : [%s]", schemaName, streamsSchemaText);
        LOG.error(errMsg, e);
        throw BadRequestException.message(errMsg, e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return WSUtils.respondEntity(schemaIdVersion, OK);
}
Also used : IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) EntityNotFoundException(com.hortonworks.streamline.common.exception.service.exception.request.EntityNotFoundException) BadRequestException(com.hortonworks.streamline.common.exception.service.exception.request.BadRequestException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 12 with SchemaVersion

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

the class SchemaRegistryClientTest method testClientWithCache.

@Test
public void testClientWithCache() throws Exception {
    final String schemaName = "foo";
    final SchemaMetadata schemaMetaData = new SchemaMetadata.Builder(schemaName).schemaGroup("group").type("type").build();
    final SchemaVersion schemaVersion = new SchemaVersion("schema-text", "desc");
    final SchemaIdVersion schemaIdVersion = new SchemaIdVersion(1L, 1);
    new Expectations(schemaRegistryClient) {

        {
            invoke(schemaRegistryClient, "registerSchemaMetadata", schemaMetaData);
            result = 1L;
            invoke(schemaRegistryClient, "doAddSchemaVersion", SchemaBranch.MASTER_BRANCH, schemaName, schemaVersion);
            result = schemaIdVersion;
            // this should be invoked only once as this should have been cached
            times = 1;
        }
    };
    Long metadataId = schemaRegistryClient.registerSchemaMetadata(schemaMetaData);
    schemaRegistryClient.addSchemaVersion(schemaMetaData, schemaVersion);
    schemaRegistryClient.addSchemaVersion(schemaMetaData, schemaVersion);
    schemaRegistryClient.addSchemaVersion(schemaName, schemaVersion);
}
Also used : Expectations(mockit.Expectations) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) Test(org.junit.Test)

Example 13 with SchemaVersion

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

the class AvroSchemaRegistryClientTest method doTestSchemaVersionLifeCycleStates.

private void doTestSchemaVersionLifeCycleStates(SchemaValidationLevel validationLevel) throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, IOException, SchemaLifecycleException, SchemaBranchNotFoundException {
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(TEST_NAME_RULE.getMethodName() + "-schema").type(AvroSchemaProvider.TYPE).schemaGroup("group").compatibility(SchemaCompatibility.BOTH).validationLevel(validationLevel).build();
    String schemaName = schemaMetadata.getName();
    Long id = SCHEMA_REGISTRY_CLIENT.registerSchemaMetadata(schemaMetadata);
    SchemaIdVersion schemaIdVersion_1 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-1.avsc"), "Initial version of the schema"));
    SchemaIdVersion schemaIdVersion_2 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-2.avsc"), "Second version of the schema"));
    // disable version 2
    SCHEMA_REGISTRY_CLIENT.disableSchemaVersion(schemaIdVersion_2.getSchemaVersionId());
    Assert.assertEquals(SchemaVersionLifecycleStates.DISABLED.getId(), SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_2).getStateId());
    // add version 3
    SchemaIdVersion schemaIdVersion_3 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-3.avsc"), "Third version of the schema, removes name field"));
    // enable version 2
    SCHEMA_REGISTRY_CLIENT.enableSchemaVersion(schemaIdVersion_2.getSchemaVersionId());
    Assert.assertEquals(SchemaVersionLifecycleStates.ENABLED.getId(), SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_2).getStateId());
    // disable version 3
    SCHEMA_REGISTRY_CLIENT.disableSchemaVersion(schemaIdVersion_3.getSchemaVersionId());
    Assert.assertEquals(SchemaVersionLifecycleStates.DISABLED.getId(), SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_3).getStateId());
    // enable version 3
    SCHEMA_REGISTRY_CLIENT.enableSchemaVersion(schemaIdVersion_3.getSchemaVersionId());
    Assert.assertEquals(SchemaVersionLifecycleStates.ENABLED.getId(), SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_3).getStateId());
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion)

Example 14 with SchemaVersion

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

the class AvroSchemaRegistryClientTest method doTestSchemaVersionEnableState.

public void doTestSchemaVersionEnableState(String schemaName) throws Exception {
    String schema = AvroSchemaRegistryClientUtil.getSchema("/device.avsc");
    String incompatSchema = AvroSchemaRegistryClientUtil.getSchema("/device-compat.avsc");
    SchemaMetadata schemaMetadata = createSchemaMetadata(schemaName, SchemaCompatibility.BACKWARD);
    // registering a new schema
    SchemaIdVersion schemaIdVersion_1 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaMetadata, new SchemaVersion(schema, "Initial version of the schema"));
    // adding a new version of the schema
    SchemaVersion incompatSchemaInfo = new SchemaVersion(incompatSchema, "second version");
    SchemaIdVersion schemaIdVersion_2 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaMetadata, incompatSchemaInfo);
    Long schemaVersionId_2 = schemaIdVersion_2.getSchemaVersionId();
    SCHEMA_REGISTRY_CLIENT.disableSchemaVersion(schemaVersionId_2);
    SchemaVersionInfo schemaVersionInfo = SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_2);
    System.out.println("disable :: schemaVersionInfo.getStateId() = " + schemaVersionInfo.getStateId());
    Assert.assertEquals(SchemaVersionLifecycleStates.DISABLED.getId(), schemaVersionInfo.getStateId());
    SCHEMA_REGISTRY_CLIENT.enableSchemaVersion(schemaVersionId_2);
    schemaVersionInfo = SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_2);
    System.out.println("enable :: schemaVersionInfo.getStateId() = " + schemaVersionInfo.getStateId());
    Assert.assertEquals(SchemaVersionLifecycleStates.ENABLED.getId(), schemaVersionInfo.getStateId());
    SCHEMA_REGISTRY_CLIENT.disableSchemaVersion(schemaVersionId_2);
    schemaVersionInfo = SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_2);
    System.out.println("disable :: schemaVersionInfo.getStateId() = " + schemaVersionInfo.getStateId());
    Assert.assertEquals(SchemaVersionLifecycleStates.DISABLED.getId(), schemaVersionInfo.getStateId());
    SCHEMA_REGISTRY_CLIENT.archiveSchemaVersion(schemaVersionId_2);
    schemaVersionInfo = SCHEMA_REGISTRY_CLIENT.getSchemaVersionInfo(schemaIdVersion_2);
    System.out.println("archive :: schemaVersionInfo.getStateId() = " + schemaVersionInfo.getStateId());
    Assert.assertEquals(SchemaVersionLifecycleStates.ARCHIVED.getId(), schemaVersionInfo.getStateId());
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion)

Example 15 with SchemaVersion

use of com.hortonworks.registries.schemaregistry.SchemaVersion 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)

Aggregations

SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)27 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)25 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)18 Test (org.junit.Test)16 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)7 IntegrationTest (com.hortonworks.registries.common.test.IntegrationTest)6 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)6 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)6 SchemaBranch (com.hortonworks.registries.schemaregistry.SchemaBranch)4 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)4 IOException (java.io.IOException)4 Timed (com.codahale.metrics.annotation.Timed)3 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)3 SchemaRegistryTestServerClientWrapper (com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper)3 SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)3 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)3 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Response (javax.ws.rs.core.Response)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2