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