Search in sources :

Example 26 with SchemaMetadata

use of com.hortonworks.registries.schemaregistry.SchemaMetadata 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 27 with SchemaMetadata

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

the class SampleSchemaRegistryClientApp method runAvroSerDesApis.

public void runAvroSerDesApis() throws IOException {
    // using builtin avro serializer/deserializer
    AvroSnapshotSerializer avroSnapshotSerializer = new AvroSnapshotSerializer();
    avroSnapshotSerializer.init(config);
    AvroSnapshotDeserializer avroSnapshotDeserializer = new AvroSnapshotDeserializer();
    avroSnapshotDeserializer.init(config);
    Object deviceObject = createGenericRecordForDevice("/device.avsc");
    SchemaMetadata schemaMetadata = createSchemaMetadata("avro-serializer-schema-" + System.currentTimeMillis());
    byte[] serializedData = avroSnapshotSerializer.serialize(deviceObject, schemaMetadata);
    Object deserializedObj = avroSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedData), null);
    LOG.info("Serialized and deserialized objects are equal: [{}] ", deviceObject.equals(deserializedObj));
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) AvroSnapshotSerializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer)

Example 28 with SchemaMetadata

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

the class SampleSchemaRegistryClientApp method runDefaultSerDesApi.

public void runDefaultSerDesApi() throws Exception {
    String type = AvroSchemaProvider.TYPE;
    AvroSnapshotSerializer serializer = schemaRegistryClient.getDefaultSerializer(type);
    serializer.init(config);
    AvroSnapshotDeserializer deserializer = schemaRegistryClient.getDefaultDeserializer(type);
    deserializer.init(config);
    Object deviceObject = createGenericRecordForDevice("/device.avsc");
    SchemaMetadata schemaMetadata = createSchemaMetadata("avro-serializer-schema-" + System.currentTimeMillis());
    byte[] serializedData = serializer.serialize(deviceObject, schemaMetadata);
    Object deserializedObj = deserializer.deserialize(new ByteArrayInputStream(serializedData), null);
    LOG.info("Serialized and deserialized objects are equal: [{}] ", deviceObject.equals(deserializedObj));
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) AvroSnapshotSerializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer)

Example 29 with SchemaMetadata

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

the class AbstractSnapshotDeserializer method deserialize.

@Override
public O deserialize(I input, Integer readerSchemaVersion) throws SerDesException {
    if (!initialized) {
        throw new IllegalStateException("init should be invoked before invoking deserialize operation");
    }
    if (closed) {
        throw new IllegalStateException("This deserializer is already closed");
    }
    // it can be enhanced to have respective protocol handlers for different versions
    byte protocolId = retrieveProtocolId(input);
    SchemaIdVersion schemaIdVersion = retrieveSchemaIdVersion(protocolId, input);
    SchemaVersionInfo schemaVersionInfo = null;
    SchemaMetadata schemaMetadata = null;
    try {
        schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(schemaIdVersion);
        schemaMetadata = schemaRegistryClient.getSchemaMetadataInfo(schemaVersionInfo.getName()).getSchemaMetadata();
    } catch (Exception e) {
        throw new RegistryException(e);
    }
    return doDeserialize(input, protocolId, schemaMetadata, schemaVersionInfo.getVersion(), readerSchemaVersion);
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) RegistryException(com.hortonworks.registries.schemaregistry.exceptions.RegistryException) RegistryException(com.hortonworks.registries.schemaregistry.exceptions.RegistryException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) ExecutionException(java.util.concurrent.ExecutionException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)

Example 30 with SchemaMetadata

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

Aggregations

SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)57 Test (org.junit.Test)35 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)33 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)31 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)21 SchemaBranch (com.hortonworks.registries.schemaregistry.SchemaBranch)17 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)15 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)11 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)11 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)9 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)8 IOException (java.io.IOException)8 SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)7 IntegrationTest (com.hortonworks.registries.common.test.IntegrationTest)6 SchemaCompatibility (com.hortonworks.registries.schemaregistry.SchemaCompatibility)6 SchemaRegistryTestServerClientWrapper (com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper)6 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)6 Collection (java.util.Collection)6 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)5