Search in sources :

Example 1 with SchemaVersionKey

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

the class StreamlineEventSerializer method serialize.

@Override
public byte[] serialize(String topic, StreamlineEvent streamlineEvent) {
    SchemaMetadata schemaMetadata = getSchemaKey(topic, false);
    SchemaVersionInfo schemaVersionInfo;
    try {
        schemaMetadata = schemaRegistryClient.getSchemaMetadataInfo(schemaMetadata.getName()).getSchemaMetadata();
        if (writerSchemaVersion != null) {
            schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaMetadata.getName(), writerSchemaVersion));
        } else {
            schemaVersionInfo = schemaRegistryClient.getLatestSchemaVersionInfo(schemaMetadata.getName());
        }
    } catch (SchemaNotFoundException e) {
        LOG.error("Exception occured while getting SchemaVersionInfo for " + schemaMetadata, e);
        throw new RuntimeException(e);
    }
    if (streamlineEvent == null || streamlineEvent.isEmpty()) {
        return null;
    } else {
        return avroSnapshotSerializer.serialize(getAvroRecord(streamlineEvent, new Schema.Parser().parse(schemaVersionInfo.getSchemaText())), schemaMetadata);
    }
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) Schema(org.apache.avro.Schema) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey)

Example 2 with SchemaVersionKey

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

the class SchemaResource method getSchemaForVersion.

@GET
@Path("/{schemaName}/versions/{version}")
@Timed
@Produces(MediaType.APPLICATION_JSON)
public Response getSchemaForVersion(@PathParam("schemaName") String schemaName, @PathParam("version") String version, @Context SecurityContext securityContext) {
    try {
        LOG.info("Get schema:version [{}:{}]", schemaName, version);
        SchemaVersionInfo schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, Integer.parseInt(version)));
        if (schemaVersionInfo != null) {
            LOG.debug("Received schema version from schema registry: [{}]", schemaVersionInfo);
            String schema = schemaVersionInfo.getSchemaText();
            if (schema != null && !schema.isEmpty()) {
                schema = AvroStreamlineSchemaConverter.convertAvroSchemaToStreamlineSchema(schema);
            }
            LOG.debug("Converted schema: [{}]", schema);
            return WSUtils.respondEntity(schema, OK);
        } else {
            throw new SchemaNotFoundException("Version " + version + " of schema " + schemaName + " not found ");
        }
    } catch (SchemaNotFoundException e) {
        LOG.error("Schema not found: [{}]", schemaName, e);
        throw EntityNotFoundException.byName(schemaName);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) 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) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET)

Example 3 with SchemaVersionKey

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

the class SampleSchemaRegistryClientApp method runSchemaApis.

public void runSchemaApis() throws Exception {
    String schemaFileName = "/device.avsc";
    String schema1 = getSchema(schemaFileName);
    SchemaMetadata schemaMetadata = createSchemaMetadata("com.hwx.schemas.sample-" + System.currentTimeMillis());
    // registering a new schema
    SchemaIdVersion v1 = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "Initial version of the schema"));
    LOG.info("Registered schema [{}] and returned version [{}]", schema1, v1);
    // adding a new version of the schema
    String schema2 = getSchema("/device-next.avsc");
    SchemaVersion schemaInfo2 = new SchemaVersion(schema2, "second version");
    SchemaIdVersion v2 = schemaRegistryClient.addSchemaVersion(schemaMetadata, schemaInfo2);
    LOG.info("Registered schema [{}] and returned version [{}]", schema2, v2);
    // adding same schema returns the earlier registered version
    SchemaIdVersion version = schemaRegistryClient.addSchemaVersion(schemaMetadata, schemaInfo2);
    LOG.info("Received version [{}] for schema metadata [{}]", version, schemaMetadata);
    // get a specific version of the schema
    String schemaName = schemaMetadata.getName();
    SchemaVersionInfo schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v2.getVersion()));
    LOG.info("Received schema version info [{}] for schema metadata [{}]", schemaVersionInfo, schemaMetadata);
    // get latest version of the schema
    SchemaVersionInfo latest = schemaRegistryClient.getLatestSchemaVersionInfo(schemaName);
    LOG.info("Latest schema with schema key [{}] is : [{}]", schemaMetadata, latest);
    // get all versions of the schema
    Collection<SchemaVersionInfo> allVersions = schemaRegistryClient.getAllVersions(schemaName);
    LOG.info("All versions of schema key [{}] is : [{}]", schemaMetadata, allVersions);
    // finding schemas containing a specific field
    SchemaFieldQuery md5FieldQuery = new SchemaFieldQuery.Builder().name("md5").build();
    Collection<SchemaVersionKey> md5SchemaVersionKeys = schemaRegistryClient.findSchemasByFields(md5FieldQuery);
    LOG.info("Schemas containing field query [{}] : [{}]", md5FieldQuery, md5SchemaVersionKeys);
    SchemaFieldQuery txidFieldQuery = new SchemaFieldQuery.Builder().name("txid").build();
    Collection<SchemaVersionKey> txidSchemaVersionKeys = schemaRegistryClient.findSchemasByFields(txidFieldQuery);
    LOG.info("Schemas containing field query [{}] : [{}]", txidFieldQuery, txidSchemaVersionKeys);
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaFieldQuery(com.hortonworks.registries.schemaregistry.SchemaFieldQuery) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey)

Example 4 with SchemaVersionKey

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

the class SchemaRegistryClient method handleSchemaIdVersionResponse.

private SchemaIdVersion handleSchemaIdVersionResponse(SchemaMetadataInfo schemaMetadataInfo, Response response) throws IncompatibleSchemaException, InvalidSchemaException {
    int status = response.getStatus();
    String msg = response.readEntity(String.class);
    if (status == Response.Status.BAD_REQUEST.getStatusCode() || status == Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()) {
        CatalogResponse catalogResponse = readCatalogResponse(msg);
        if (CatalogResponse.ResponseMessage.INCOMPATIBLE_SCHEMA.getCode() == catalogResponse.getResponseCode()) {
            throw new IncompatibleSchemaException(catalogResponse.getResponseMessage());
        } else if (CatalogResponse.ResponseMessage.INVALID_SCHEMA.getCode() == catalogResponse.getResponseCode()) {
            throw new InvalidSchemaException(catalogResponse.getResponseMessage());
        } else {
            throw new RuntimeException(catalogResponse.getResponseMessage());
        }
    }
    Integer version = readEntity(msg, Integer.class);
    SchemaVersionInfo schemaVersionInfo = doGetSchemaVersionInfo(new SchemaVersionKey(schemaMetadataInfo.getSchemaMetadata().getName(), version));
    return new SchemaIdVersion(schemaMetadataInfo.getId(), version, schemaVersionInfo.getId());
}
Also used : IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey)

Example 5 with SchemaVersionKey

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

the class AvroSchemaRegistryClientTest method testSchemaVersionDeletion.

@Test
public void testSchemaVersionDeletion() throws Exception {
    SchemaVersionKey schemaVersionKey = addAndDeleteSchemaVersion(TEST_NAME_RULE.getMethodName());
    Assert.assertTrue(SCHEMA_REGISTRY_CLIENT.getAllVersions(schemaVersionKey.getSchemaName()).isEmpty());
}
Also used : SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) IntegrationTest(com.hortonworks.registries.common.test.IntegrationTest) Test(org.junit.Test)

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