Search in sources :

Example 16 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo 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)

Example 17 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project nifi by apache.

the class TestHortonworksSchemaRegistry method testCacheUsed.

@Test
public void testCacheUsed() throws Exception {
    final String text = new String(Files.readAllBytes(Paths.get("src/test/resources/empty-schema.avsc")));
    final SchemaVersionInfo info = new SchemaVersionInfo(1L, "unit-test", 2, text, System.currentTimeMillis(), "description");
    schemaVersionInfoMap.put("unit-test", info);
    final SchemaMetadata metadata = new SchemaMetadata.Builder("unit-test").compatibility(SchemaCompatibility.NONE).evolve(true).schemaGroup("group").type("AVRO").build();
    final Constructor<SchemaMetadataInfo> ctr = SchemaMetadataInfo.class.getDeclaredConstructor(SchemaMetadata.class, Long.class, Long.class);
    ctr.setAccessible(true);
    final SchemaMetadataInfo schemaMetadataInfo = ctr.newInstance(metadata, 1L, System.currentTimeMillis());
    schemaMetadataInfoMap.put("unit-test", schemaMetadataInfo);
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(HortonworksSchemaRegistry.URL, "http://localhost:44444");
    properties.put(HortonworksSchemaRegistry.CACHE_EXPIRATION, "5 mins");
    properties.put(HortonworksSchemaRegistry.CACHE_SIZE, "1000");
    final ConfigurationContext configurationContext = new MockConfigurationContext(properties, null);
    registry.enable(configurationContext);
    for (int i = 0; i < 10000; i++) {
        final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("unit-test").build();
        final RecordSchema schema = registry.retrieveSchema(schemaIdentifier);
        assertNotNull(schema);
    }
    Mockito.verify(client, Mockito.times(1)).getLatestSchemaVersionInfo(any(String.class));
}
Also used : ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Test(org.junit.Test)

Example 18 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project nifi by apache.

the class TestHortonworksSchemaRegistry method testCacheExpires.

@Test
@Ignore("This can be useful for manual testing/debugging, but will keep ignored for now because we don't want automated builds to run this, since it depends on timing")
public void testCacheExpires() throws Exception {
    final String text = new String(Files.readAllBytes(Paths.get("src/test/resources/empty-schema.avsc")));
    final SchemaVersionInfo info = new SchemaVersionInfo(1L, "unit-test", 2, text, System.currentTimeMillis(), "description");
    schemaVersionInfoMap.put("unit-test", info);
    final SchemaMetadata metadata = new SchemaMetadata.Builder("unit-test").compatibility(SchemaCompatibility.NONE).evolve(true).schemaGroup("group").type("AVRO").build();
    final Constructor<SchemaMetadataInfo> ctr = SchemaMetadataInfo.class.getDeclaredConstructor(SchemaMetadata.class, Long.class, Long.class);
    ctr.setAccessible(true);
    final SchemaMetadataInfo schemaMetadataInfo = ctr.newInstance(metadata, 1L, System.currentTimeMillis());
    schemaMetadataInfoMap.put("unit-test", schemaMetadataInfo);
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(HortonworksSchemaRegistry.URL, "http://localhost:44444");
    properties.put(HortonworksSchemaRegistry.CACHE_EXPIRATION, "1 sec");
    properties.put(HortonworksSchemaRegistry.CACHE_SIZE, "1000");
    final ConfigurationContext configurationContext = new MockConfigurationContext(properties, null);
    registry.enable(configurationContext);
    for (int i = 0; i < 2; i++) {
        final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("unit-test").build();
        final RecordSchema schema = registry.retrieveSchema(schemaIdentifier);
        assertNotNull(schema);
    }
    Mockito.verify(client, Mockito.times(1)).getLatestSchemaVersionInfo(any(String.class));
    Thread.sleep(2000L);
    for (int i = 0; i < 2; i++) {
        final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("unit-test").build();
        final RecordSchema schema = registry.retrieveSchema(schemaIdentifier);
        assertNotNull(schema);
    }
    Mockito.verify(client, Mockito.times(2)).getLatestSchemaVersionInfo(any(String.class));
}
Also used : ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) HashMap(java.util.HashMap) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 19 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project streamline by hortonworks.

the class AvroStreamsSnapshotDeserializerTest method testAvroPayloadConversions.

@Test
public void testAvroPayloadConversions() throws Exception {
    try (InputStream schemaStream = AvroStreamsSnapshotDeserializerTest.class.getResourceAsStream("/avro/complex.avsc")) {
        CustomAvroSerializer customAvroSerializer = new CustomAvroSerializer();
        customAvroSerializer.init(Collections.singletonMap("serdes.protocol.version", (byte) 1));
        final Schema schema = new Schema.Parser().parse(schemaStream);
        GenericRecord inputRecord = generateGenericRecord(schema);
        LOG.info("Generated record [{}]", inputRecord);
        byte[] serializedBytes = customAvroSerializer.customSerialize(inputRecord);
        SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("topic-1").type("avro").schemaGroup("kafka").build();
        new Expectations() {

            {
                mockSchemaRegistryClient.getSchemaVersionInfo(withEqual(SCHEMA_ID_VERSION));
                // only versions of schema matters for this mock, as getVersion is only used during de-serialization
                result = new SchemaVersionInfo(1l, schemaMetadata.getName(), SCHEMA_ID_VERSION.getVersion(), "doesNotMatter", 1l, "doesNotMatter");
                mockSchemaRegistryClient.getSchemaMetadataInfo(withEqual(schemaMetadata.getName()));
                result = new SchemaMetadataInfo(schemaMetadata);
            }
        };
        AvroStreamsSnapshotDeserializer avroStreamsSnapshotDeserializer = new AvroStreamsSnapshotDeserializer() {

            @Override
            protected Schema getSchema(SchemaVersionKey schemaVersionKey) {
                return schema;
            }
        };
        Map<String, String> config = Collections.singletonMap(SchemaRegistryClient.Configuration.SCHEMA_REGISTRY_URL.name(), "http://localhost:8080/api/v1");
        avroStreamsSnapshotDeserializer.init(config);
        Object deserializedObject = avroStreamsSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedBytes), 1);
        Map<Object, Object> map = (Map<Object, Object>) deserializedObject;
        String deserializedJson = new ObjectMapper().writeValueAsString(map);
        String inputJson = GenericData.get().toString(inputRecord);
        LOG.info("inputJson #{}# " + inputJson);
        LOG.info("deserializedJson = #{}#" + deserializedJson);
        ObjectMapper objectMapper = new ObjectMapper();
        Assert.assertEquals(objectMapper.readTree(inputJson), objectMapper.readTree(deserializedJson));
    }
}
Also used : Expectations(mockit.Expectations) AvroStreamsSnapshotDeserializer(com.hortonworks.streamline.streams.runtime.storm.spout.AvroStreamsSnapshotDeserializer) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Schema(org.apache.avro.Schema) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) ByteArrayInputStream(java.io.ByteArrayInputStream) GenericRecord(org.apache.avro.generic.GenericRecord) Map(java.util.Map) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Test(org.junit.Test)

Example 20 with SchemaVersionInfo

use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo 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);
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) RegistryException(com.hortonworks.registries.schemaregistry.exceptions.RegistryException) RegistryException(com.hortonworks.registries.schemaregistry.exceptions.RegistryException) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) ExecutionException(java.util.concurrent.ExecutionException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)

Aggregations

SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)31 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)19 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)17 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)14 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)13 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)13 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)11 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)11 IOException (java.io.IOException)11 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)10 Test (org.junit.Test)10 Timed (com.codahale.metrics.annotation.Timed)7 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)7 SchemaBranchNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException)7 GET (javax.ws.rs.GET)7 Path (javax.ws.rs.Path)7 UnitOfWork (com.hortonworks.registries.common.transaction.UnitOfWork)6 InvalidSchemaBranchDeletionException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaBranchDeletionException)6 SchemaBranchAlreadyExistsException (com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException)6 UnsupportedSchemaTypeException (com.hortonworks.registries.schemaregistry.errors.UnsupportedSchemaTypeException)6