use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo 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);
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project registry by hortonworks.
the class CustomStateTransitionTest method testTransitionToRejectedState.
@Test
public void testTransitionToRejectedState() throws IOException, SchemaNotFoundException, InvalidSchemaException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, SchemaLifecycleException {
SchemaMetadata schemaMetadata = createSchemaMetadata("test", SchemaCompatibility.NONE);
SchemaBranch branch1 = new SchemaBranch("BRANCH1", schemaMetadata.getName());
String schema1 = getSchema("/device.avsc");
schemaRegistryClient.addSchemaMetadata(schemaMetadata);
SchemaIdVersion schemaIdVersion1 = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "initial version"));
schemaRegistryClient.createSchemaBranch(schemaIdVersion1.getSchemaVersionId(), branch1);
String schema2 = AvroSchemaRegistryClientUtil.getSchema("/device1.avsc");
SchemaIdVersion schemaIdVersion2 = schemaRegistryClient.addSchemaVersion(branch1.getName(), schemaMetadata, new SchemaVersion(schema2, "modify version"));
schemaRegistryClient.startSchemaVersionReview(schemaIdVersion2.getSchemaVersionId());
SchemaVersionInfo schemaVersionInfoAtReviewState = schemaRegistryClient.getSchemaVersionInfo(new SchemaIdVersion(schemaIdVersion2.getSchemaVersionId()));
Assert.assertTrue(schemaVersionInfoAtReviewState.getStateId().equals(CustomReviewCycleStates.PEER_REVIEW_STATE.getId()));
schemaRegistryClient.transitionState(schemaIdVersion2.getSchemaVersionId(), CustomReviewCycleStates.REJECTED_REVIEW_STATE.getId(), "Rejected by X".getBytes());
SchemaVersionInfo schemaVersionInfoAtRejectedState = schemaRegistryClient.getSchemaVersionInfo(new SchemaIdVersion(schemaIdVersion2.getSchemaVersionId()));
Assert.assertTrue(schemaVersionInfoAtRejectedState.getStateId().equals(CustomReviewCycleStates.REJECTED_REVIEW_STATE.getId()));
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo 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());
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project registry by hortonworks.
the class MessageContextBasedAvroSerDesTest method testSerDes.
@Test
public void testSerDes() throws Exception {
SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("msgCtx-" + System.currentTimeMillis()).schemaGroup("custom").type(AvroSchemaProvider.TYPE).compatibility(SchemaCompatibility.BACKWARD).build();
SchemaIdVersion schemaIdVersion = new SchemaIdVersion(1L, 1, 1L);
Device input = new Device(1L, "device", 1, System.currentTimeMillis());
SchemaVersionInfo schemaVersionInfo = new SchemaVersionInfo(1l, input.getName().toString(), schemaIdVersion.getVersion(), input.getSchema().toString(), System.currentTimeMillis(), "");
new Expectations() {
{
mockSchemaRegistryClient.addSchemaVersion(withInstanceOf(SchemaMetadata.class), withInstanceOf(SchemaVersion.class));
result = schemaIdVersion;
mockSchemaRegistryClient.getSchemaMetadataInfo(anyString);
result = new SchemaMetadataInfo(schemaMetadata);
mockSchemaRegistryClient.getSchemaVersionInfo(withInstanceOf(SchemaVersionKey.class));
result = schemaVersionInfo;
}
};
MessageContextBasedAvroSerializer serializer = new MessageContextBasedAvroSerializer();
serializer.init(Collections.emptyMap());
MessageContext messageContext = serializer.serialize(input, schemaMetadata);
MessageContextBasedAvroDeserializer deserializer = new MessageContextBasedAvroDeserializer();
deserializer.init(Collections.emptyMap());
Object deserializedObject = deserializer.deserialize(messageContext, null);
Assert.assertTrue(SpecificData.get().compare(input, deserializedObject, input.getSchema()) == 0);
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project registry by hortonworks.
the class ConfluentSchemaRegistryCompatibleResource method getSchemaById.
@GET
@Path("/schemas/ids/{id}")
@ApiOperation(value = "Get schema version by id", response = Schema.class, tags = OPERATION_GROUP_CONFLUENT_SR)
@Timed
@UnitOfWork
public Response getSchemaById(@ApiParam(value = "schema version id", required = true) @PathParam("id") Long id) {
Response response;
try {
SchemaVersionInfo schemaVersionInfo = schemaRegistry.getSchemaVersionInfo(new SchemaIdVersion(id));
SchemaString schema = new SchemaString();
schema.setSchema(schemaVersionInfo.getSchemaText());
response = WSUtils.respondEntity(schema, Response.Status.OK);
} catch (SchemaNotFoundException ex) {
LOG.error("No schema version found with id [{}]", id, ex);
response = schemaNotFoundError();
} catch (Exception ex) {
LOG.error("Encountered error while retrieving Schema with id: [{}]", id, ex);
response = serverError();
}
return response;
}
Aggregations