use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project registry by hortonworks.
the class SchemaRegistryResource method findAggregatedSchemas.
@GET
@Path("/search/schemas/aggregated")
@ApiOperation(value = "Search for schemas containing the given name and description", notes = "Search the schemas for given name and description, return a list of schemas that contain the field.", response = AggregatedSchemaMetadataInfo.class, responseContainer = "Collection", tags = OPERATION_GROUP_SCHEMA)
@Timed
@UnitOfWork
public Response findAggregatedSchemas(@Context UriInfo uriInfo) {
MultivaluedMap<String, String> queryParameters = uriInfo.getQueryParameters();
try {
Collection<SchemaMetadataInfo> schemaMetadataInfos = findSchemaMetadataInfos(uriInfo.getQueryParameters());
List<AggregatedSchemaMetadataInfo> aggregatedSchemaMetadataInfos = new ArrayList<>();
for (SchemaMetadataInfo schemaMetadataInfo : schemaMetadataInfos) {
SchemaMetadata schemaMetadata = schemaMetadataInfo.getSchemaMetadata();
List<SerDesInfo> serDesInfos = new ArrayList<>(schemaRegistry.getSerDes(schemaMetadataInfo.getSchemaMetadata().getName()));
aggregatedSchemaMetadataInfos.add(new AggregatedSchemaMetadataInfo(schemaMetadata, schemaMetadataInfo.getId(), schemaMetadataInfo.getTimestamp(), schemaRegistry.getAggregatedSchemaBranch(schemaMetadata.getName()), serDesInfos));
}
return WSUtils.respondEntities(aggregatedSchemaMetadataInfos, Response.Status.OK);
} catch (SchemaBranchNotFoundException e) {
return WSUtils.respond(Response.Status.NOT_FOUND, CatalogResponse.ResponseMessage.ENTITY_NOT_FOUND, e.getMessage());
} catch (Exception ex) {
LOG.error("Encountered error while finding schemas for given fields [{}]", queryParameters, ex);
return WSUtils.respond(Response.Status.INTERNAL_SERVER_ERROR, CatalogResponse.ResponseMessage.EXCEPTION, ex.getMessage());
}
}
use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project registry by hortonworks.
the class SchemaRegistryResource method findSchemaMetadataInfos.
private Collection<SchemaMetadataInfo> findSchemaMetadataInfos(MultivaluedMap<String, String> queryParameters) {
Collection<SchemaMetadataInfo> schemaMetadataInfos;
// query for those complex queries.
if (queryParameters.containsKey(SchemaMetadataStorable.NAME) || queryParameters.containsKey(SchemaMetadataStorable.DESCRIPTION)) {
String name = queryParameters.getFirst(SchemaMetadataStorable.NAME);
String description = queryParameters.getFirst(SchemaMetadataStorable.DESCRIPTION);
WhereClause whereClause = WhereClause.begin().contains(SchemaMetadataStorable.NAME, name).or().contains(SchemaMetadataStorable.DESCRIPTION, description).combine();
// todo refactor orderby field in DefaultSchemaRegistry#search APIs merge with these APIs
String orderByFieldStr = queryParameters.getFirst(ORDER_BY_FIELDS_PARAM_NAME);
schemaMetadataInfos = schemaRegistry.searchSchemas(whereClause, getOrderByFields(orderByFieldStr));
} else {
schemaMetadataInfos = Collections.emptyList();
}
return schemaMetadataInfos;
}
use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project registry by hortonworks.
the class BasicSchemaRegistryClientOpsTest method doTestSchemaOps.
private void doTestSchemaOps(SchemaValidationLevel validationLevel) throws IOException, InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException {
String testName = TEST_NAME_RULE.getMethodName();
SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(testName + "-schema").type(AvroSchemaProvider.TYPE).schemaGroup(testName + "-group").description("Schema for " + testName).validationLevel(validationLevel).compatibility(SchemaCompatibility.BOTH).build();
Long id = schemaRegistryClient.registerSchemaMetadata(schemaMetadata);
Assert.assertNotNull(id);
// registering a new schema
String schemaName = schemaMetadata.getName();
String schema1 = AvroSchemaRegistryClientUtil.getSchema("/schema-1.avsc");
SchemaIdVersion v1 = schemaRegistryClient.addSchemaVersion(schemaName, new SchemaVersion(schema1, "Initial version of the schema"));
Assert.assertNotNull(v1.getSchemaMetadataId());
Assert.assertEquals(1, v1.getVersion().intValue());
SchemaMetadataInfo schemaMetadataInfoForId = schemaRegistryClient.getSchemaMetadataInfo(v1.getSchemaMetadataId());
SchemaMetadataInfo schemaMetadataInfoForName = schemaRegistryClient.getSchemaMetadataInfo(schemaName);
Assert.assertEquals(schemaMetadataInfoForId, schemaMetadataInfoForName);
// adding a new version of the schema using uploadSchemaVersion API
SchemaIdVersion v2 = schemaRegistryClient.uploadSchemaVersion(schemaMetadata.getName(), "second version", AvroSchemaRegistryClientTest.class.getResourceAsStream("/schema-2.avsc"));
Assert.assertEquals(v1.getVersion() + 1, v2.getVersion().intValue());
SchemaVersionInfo schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v2.getVersion()));
SchemaVersionInfo latest = schemaRegistryClient.getLatestSchemaVersionInfo(schemaName);
Assert.assertEquals(latest, schemaVersionInfo);
Collection<SchemaVersionInfo> allVersions = schemaRegistryClient.getAllVersions(schemaName);
Assert.assertEquals(2, allVersions.size());
// receive the same version as earlier without adding a new schema entry as it exists in the same schema group.
SchemaIdVersion version = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "already added schema"));
Assert.assertEquals(version, v1);
Collection<SchemaVersionKey> md5SchemaVersionKeys = schemaRegistryClient.findSchemasByFields(new SchemaFieldQuery.Builder().name("md5").build());
Assert.assertEquals(2, md5SchemaVersionKeys.size());
Collection<SchemaVersionKey> txidSchemaVersionKeys = schemaRegistryClient.findSchemasByFields(new SchemaFieldQuery.Builder().name("txid").build());
Assert.assertEquals(1, txidSchemaVersionKeys.size());
// checks we can update schema meta data.
SchemaMetadata currentSchemaMetadata = schemaRegistryClient.getSchemaMetadataInfo(schemaName).getSchemaMetadata();
SchemaMetadata schemaMetadataToUpdateTo = new SchemaMetadata.Builder(currentSchemaMetadata).validationLevel(SchemaValidationLevel.LATEST).build();
SchemaMetadataInfo updatedSchemaMetadata = schemaRegistryClient.updateSchemaMetadata(schemaName, schemaMetadataToUpdateTo);
Assert.assertEquals(SchemaValidationLevel.LATEST, updatedSchemaMetadata.getSchemaMetadata().getValidationLevel());
}
use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project nifi by apache.
the class HortonworksSchemaRegistry method retrieveSchemaByName.
private RecordSchema retrieveSchemaByName(final SchemaIdentifier schemaIdentifier) throws org.apache.nifi.schema.access.SchemaNotFoundException, IOException {
final SchemaRegistryClient client = getClient();
final SchemaVersionInfo versionInfo;
final Long schemaId;
final Optional<String> schemaName = schemaIdentifier.getName();
if (!schemaName.isPresent()) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Name is not present");
}
final Optional<String> schemaBranchName = schemaIdentifier.getBranch();
final OptionalInt schemaVersion = schemaIdentifier.getVersion();
try {
final SchemaMetadataInfo metadataInfo = client.getSchemaMetadataInfo(schemaName.get());
if (metadataInfo == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with name '" + schemaName + "'");
}
schemaId = metadataInfo.getId();
if (schemaId == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with name '" + schemaName + "'");
}
// possible scenarios are name only, name + branch, or name + version
if (schemaVersion.isPresent()) {
final SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaName.get(), schemaVersion.getAsInt());
versionInfo = getSchemaVersionInfo(client, schemaVersionKey);
} else {
versionInfo = getLatestSchemaVersionInfo(client, schemaName.get(), schemaBranchName.orElse(null));
}
if (versionInfo == null || versionInfo.getVersion() == null) {
final String message = createErrorMessage("Could not find schema", schemaName, schemaBranchName, schemaVersion);
throw new org.apache.nifi.schema.access.SchemaNotFoundException(message);
}
} catch (final Exception e) {
final String message = createErrorMessage("Failed to retrieve schema", schemaName, schemaBranchName, schemaVersion);
handleException(message, e);
return null;
}
final String schemaText = versionInfo.getSchemaText();
final SchemaIdentifier resultSchemaIdentifier = SchemaIdentifier.builder().id(schemaId).name(schemaName.get()).branch(schemaBranchName.orElse(null)).version(versionInfo.getVersion()).build();
final Tuple<SchemaIdentifier, String> tuple = new Tuple<>(resultSchemaIdentifier, schemaText);
return schemaNameToSchemaMap.computeIfAbsent(tuple, t -> {
final Schema schema = new Schema.Parser().parse(schemaText);
return AvroTypeUtil.createSchema(schema, schemaText, resultSchemaIdentifier);
});
}
use of com.hortonworks.registries.schemaregistry.SchemaMetadataInfo in project nifi by apache.
the class HortonworksSchemaRegistry method retrieveSchemaByIdAndVersion.
private RecordSchema retrieveSchemaByIdAndVersion(final SchemaIdentifier schemaIdentifier) throws org.apache.nifi.schema.access.SchemaNotFoundException, IOException {
final SchemaRegistryClient client = getClient();
final String schemaName;
final SchemaVersionInfo versionInfo;
final OptionalLong schemaId = schemaIdentifier.getIdentifier();
if (!schemaId.isPresent()) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Id is not present");
}
final OptionalInt version = schemaIdentifier.getVersion();
if (!version.isPresent()) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Version is not present");
}
try {
final SchemaMetadataInfo info = client.getSchemaMetadataInfo(schemaId.getAsLong());
if (info == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with ID '" + schemaId + "' and version '" + version + "'");
}
final SchemaMetadata metadata = info.getSchemaMetadata();
schemaName = metadata.getName();
final SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaName, version.getAsInt());
versionInfo = getSchemaVersionInfo(client, schemaVersionKey);
if (versionInfo == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with ID '" + schemaId + "' and version '" + version + "'");
}
} catch (final Exception e) {
handleException("Failed to retrieve schema with ID '" + schemaId + "' and version '" + version + "'", e);
return null;
}
final String schemaText = versionInfo.getSchemaText();
final SchemaIdentifier resultSchemaIdentifier = SchemaIdentifier.builder().name(schemaName).id(schemaId.getAsLong()).version(version.getAsInt()).build();
final Tuple<SchemaIdentifier, String> tuple = new Tuple<>(resultSchemaIdentifier, schemaText);
return schemaNameToSchemaMap.computeIfAbsent(tuple, t -> {
final Schema schema = new Schema.Parser().parse(schemaText);
return AvroTypeUtil.createSchema(schema, schemaText, resultSchemaIdentifier);
});
}
Aggregations