Search in sources :

Example 31 with SchemaIdentifier

use of org.apache.nifi.serialization.record.SchemaIdentifier in project nifi by apache.

the class TestSchemaNamePropertyStrategy method testNameAndBlankVersion.

@Test
public void testNameAndBlankVersion() throws SchemaNotFoundException, IOException {
    final PropertyValue nameValue = new MockPropertyValue("person");
    final PropertyValue branchValue = new MockPropertyValue(null);
    final PropertyValue versionValue = new MockPropertyValue("   ");
    final SchemaNamePropertyStrategy schemaNamePropertyStrategy = new SchemaNamePropertyStrategy(schemaRegistry, nameValue, branchValue, versionValue);
    final SchemaIdentifier expectedSchemaIdentifier = SchemaIdentifier.builder().name(nameValue.getValue()).build();
    when(schemaRegistry.retrieveSchema(argThat(new SchemaIdentifierMatcher(expectedSchemaIdentifier)))).thenReturn(recordSchema);
    final RecordSchema retrievedSchema = schemaNamePropertyStrategy.getSchema(Collections.emptyMap(), null, recordSchema);
    assertNotNull(retrievedSchema);
}
Also used : MockPropertyValue(org.apache.nifi.util.MockPropertyValue) PropertyValue(org.apache.nifi.components.PropertyValue) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 32 with SchemaIdentifier

use of org.apache.nifi.serialization.record.SchemaIdentifier in project nifi by apache.

the class AvroSchemaRegistry method onPropertyModified.

@Override
public void onPropertyModified(final PropertyDescriptor descriptor, final String oldValue, final String newValue) {
    if (descriptor.isDynamic()) {
        // Dynamic property = schema, validate it
        if (newValue == null) {
            recordSchemas.remove(descriptor.getName());
        } else {
            try {
                // Use a non-strict parser here, a strict parse can be done (if specified) in customValidate().
                final Schema avroSchema = new Schema.Parser().setValidate(false).parse(newValue);
                final SchemaIdentifier schemaId = SchemaIdentifier.builder().name(descriptor.getName()).build();
                final RecordSchema recordSchema = AvroTypeUtil.createSchema(avroSchema, newValue, schemaId);
                recordSchemas.put(descriptor.getName(), recordSchema);
            } catch (final Exception e) {
            // not a problem - the service won't be valid and the validation message will indicate what is wrong.
            }
        }
    }
}
Also used : RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Schema(org.apache.avro.Schema) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) InitializationException(org.apache.nifi.reporting.InitializationException) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) IOException(java.io.IOException)

Example 33 with SchemaIdentifier

use of org.apache.nifi.serialization.record.SchemaIdentifier in project nifi by apache.

the class TestAvroSchemaRegistry method validateSchemaRegistrationFromrDynamicProperties.

@Test
public void validateSchemaRegistrationFromrDynamicProperties() throws Exception {
    String schemaName = "fooSchema";
    PropertyDescriptor fooSchema = new PropertyDescriptor.Builder().name(schemaName).dynamic(true).build();
    String fooSchemaText = "{\"namespace\": \"example.avro\", " + "\"type\": \"record\", " + "\"name\": \"User\", " + "\"fields\": [ " + "{\"name\": \"name\", \"type\": [\"string\", \"null\"]}, " + "{\"name\": \"favorite_number\",  \"type\": [\"int\", \"null\"]}, " + "{\"name\": \"foo\",  \"type\": [\"int\", \"null\"]}, " + "{\"name\": \"favorite_color\", \"type\": [\"string\", \"null\"]} " + "]" + "}";
    PropertyDescriptor barSchema = new PropertyDescriptor.Builder().name("barSchema").dynamic(false).build();
    AvroSchemaRegistry delegate = new AvroSchemaRegistry();
    delegate.onPropertyModified(fooSchema, null, fooSchemaText);
    delegate.onPropertyModified(barSchema, null, "");
    SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name(schemaName).build();
    RecordSchema locatedSchema = delegate.retrieveSchema(schemaIdentifier);
    assertEquals(fooSchemaText, locatedSchema.getSchemaText().get());
    try {
        delegate.retrieveSchema(SchemaIdentifier.builder().name("barSchema").build());
        Assert.fail("Expected a SchemaNotFoundException to be thrown but it was not");
    } catch (final SchemaNotFoundException expected) {
    }
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 34 with SchemaIdentifier

use of org.apache.nifi.serialization.record.SchemaIdentifier 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 35 with SchemaIdentifier

use of org.apache.nifi.serialization.record.SchemaIdentifier 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

SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)37 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)28 Test (org.junit.Test)22 SimpleRecordSchema (org.apache.nifi.serialization.SimpleRecordSchema)12 OptionalLong (java.util.OptionalLong)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 OptionalInt (java.util.OptionalInt)5 Schema (org.apache.avro.Schema)5 PropertyValue (org.apache.nifi.components.PropertyValue)5 MockPropertyValue (org.apache.nifi.util.MockPropertyValue)5 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)4 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)4 ByteBuffer (java.nio.ByteBuffer)4 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)3 InitializationException (org.apache.nifi.reporting.InitializationException)3 SchemaNotFoundException (org.apache.nifi.schema.access.SchemaNotFoundException)3