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