use of com.hortonworks.registries.schemaregistry.SchemaIdVersion 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.SchemaIdVersion in project registry by hortonworks.
the class SchemaRegistryClient method addSchemaVersion.
@Override
public SchemaIdVersion addSchemaVersion(String schemaBranchName, SchemaMetadata schemaMetadata, SchemaVersion schemaVersion) throws InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
// get it, if it exists in cache
SchemaDigestEntry schemaDigestEntry = buildSchemaTextEntry(schemaVersion, schemaMetadata.getName());
SchemaIdVersion schemaIdVersion = schemaTextCache.getIfPresent(schemaDigestEntry);
if (schemaIdVersion == null) {
// register schema metadata if it does not exist
Long metadataId = registerSchemaMetadata(schemaMetadata);
if (metadataId == null) {
LOG.error("Schema Metadata [{}] is not registered successfully", schemaMetadata);
throw new RuntimeException("Given SchemaMetadata could not be registered: " + schemaMetadata);
}
// add schemaIdVersion
schemaIdVersion = addSchemaVersion(schemaBranchName, schemaMetadata.getName(), schemaVersion);
}
return schemaIdVersion;
}
use of com.hortonworks.registries.schemaregistry.SchemaIdVersion 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.SchemaIdVersion 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);
}
use of com.hortonworks.registries.schemaregistry.SchemaIdVersion 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());
}
Aggregations