Search in sources :

Example 6 with SchemaMetadataInfo

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());
    }
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) ArrayList(java.util.ArrayList) AggregatedSchemaMetadataInfo(com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo) SerDesInfo(com.hortonworks.registries.schemaregistry.SerDesInfo) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) InvalidSchemaBranchDeletionException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException) FileNotFoundException(java.io.FileNotFoundException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) UnsupportedSchemaTypeException(com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) IOException(java.io.IOException) AggregatedSchemaMetadataInfo(com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Path(javax.ws.rs.Path) UnitOfWork(com.hortonworks.registries.common.transaction.UnitOfWork) Timed(com.codahale.metrics.annotation.Timed) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation)

Example 7 with SchemaMetadataInfo

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;
}
Also used : WhereClause(com.hortonworks.registries.storage.search.WhereClause) AggregatedSchemaMetadataInfo(com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

Example 8 with SchemaMetadataInfo

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());
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaFieldQuery(com.hortonworks.registries.schemaregistry.SchemaFieldQuery) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

Example 9 with SchemaMetadataInfo

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);
    });
}
Also used : RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Schema(org.apache.avro.Schema) OptionalInt(java.util.OptionalInt) InitializationException(org.apache.nifi.reporting.InitializationException) IOException(java.io.IOException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) OptionalLong(java.util.OptionalLong) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) Tuple(org.apache.nifi.util.Tuple) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

Example 10 with SchemaMetadataInfo

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);
    });
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Schema(org.apache.avro.Schema) OptionalInt(java.util.OptionalInt) InitializationException(org.apache.nifi.reporting.InitializationException) IOException(java.io.IOException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) OptionalLong(java.util.OptionalLong) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) Tuple(org.apache.nifi.util.Tuple) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

Aggregations

SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)22 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)14 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)13 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)10 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)9 Test (org.junit.Test)8 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)7 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)6 IOException (java.io.IOException)6 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)5 AggregatedSchemaMetadataInfo (com.hortonworks.registries.schemaregistry.AggregatedSchemaMetadataInfo)5 Response (javax.ws.rs.core.Response)5 Timed (com.codahale.metrics.annotation.Timed)4 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)4 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)4 SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)4 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)4 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)4 ApiOperation (io.swagger.annotations.ApiOperation)4 Path (javax.ws.rs.Path)4