Search in sources :

Example 1 with SchemaRegistry

use of org.apache.nifi.schemaregistry.services.SchemaRegistry in project nifi by apache.

the class ValidateRecord method getValidationSchema.

protected RecordSchema getValidationSchema(final ProcessContext context, final FlowFile flowFile, final RecordReader reader) throws MalformedRecordException, IOException, SchemaNotFoundException {
    final String schemaAccessStrategy = context.getProperty(SCHEMA_ACCESS_STRATEGY).getValue();
    if (schemaAccessStrategy.equals(READER_SCHEMA.getValue())) {
        return reader.getSchema();
    } else if (schemaAccessStrategy.equals(SCHEMA_NAME_PROPERTY.getValue())) {
        final SchemaRegistry schemaRegistry = context.getProperty(SCHEMA_REGISTRY).asControllerService(SchemaRegistry.class);
        final String schemaName = context.getProperty(SCHEMA_NAME).evaluateAttributeExpressions(flowFile).getValue();
        final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name(schemaName).build();
        return schemaRegistry.retrieveSchema(schemaIdentifier);
    } else if (schemaAccessStrategy.equals(SCHEMA_TEXT_PROPERTY.getValue())) {
        final String schemaText = context.getProperty(SCHEMA_TEXT).evaluateAttributeExpressions(flowFile).getValue();
        final Parser parser = new Schema.Parser();
        final Schema avroSchema = parser.parse(schemaText);
        return AvroTypeUtil.createSchema(avroSchema);
    } else {
        throw new ProcessException("Invalid Schema Access Strategy: " + schemaAccessStrategy);
    }
}
Also used : ProcessException(org.apache.nifi.processor.exception.ProcessException) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Schema(org.apache.avro.Schema) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) SchemaRegistry(org.apache.nifi.schemaregistry.services.SchemaRegistry) Parser(org.apache.avro.Schema.Parser)

Example 2 with SchemaRegistry

use of org.apache.nifi.schemaregistry.services.SchemaRegistry in project nifi by apache.

the class SchemaRegistryService method storeSchemaAccessStrategy.

@OnEnabled
public void storeSchemaAccessStrategy(final ConfigurationContext context) {
    this.configurationContext = context;
    final SchemaRegistry schemaRegistry = context.getProperty(SCHEMA_REGISTRY).asControllerService(SchemaRegistry.class);
    final PropertyDescriptor descriptor = getSchemaAcessStrategyDescriptor();
    final String schemaAccess = context.getProperty(descriptor).getValue();
    this.schemaAccessStrategy = getSchemaAccessStrategy(schemaAccess, schemaRegistry, context);
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) SchemaRegistry(org.apache.nifi.schemaregistry.services.SchemaRegistry) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Example 3 with SchemaRegistry

use of org.apache.nifi.schemaregistry.services.SchemaRegistry in project nifi by apache.

the class SchemaRegistryService method getSuppliedSchemaFields.

protected Set<SchemaField> getSuppliedSchemaFields(final ValidationContext validationContext) {
    final String accessStrategyValue = validationContext.getProperty(getSchemaAcessStrategyDescriptor()).getValue();
    final SchemaRegistry schemaRegistry = validationContext.getProperty(SCHEMA_REGISTRY).asControllerService(SchemaRegistry.class);
    final SchemaAccessStrategy accessStrategy = getSchemaAccessStrategy(accessStrategyValue, schemaRegistry, validationContext);
    if (accessStrategy == null) {
        return EnumSet.noneOf(SchemaField.class);
    }
    final Set<SchemaField> suppliedFields = accessStrategy.getSuppliedSchemaFields();
    return suppliedFields;
}
Also used : SchemaField(org.apache.nifi.schema.access.SchemaField) SchemaAccessStrategy(org.apache.nifi.schema.access.SchemaAccessStrategy) SchemaRegistry(org.apache.nifi.schemaregistry.services.SchemaRegistry)

Aggregations

SchemaRegistry (org.apache.nifi.schemaregistry.services.SchemaRegistry)3 Schema (org.apache.avro.Schema)1 Parser (org.apache.avro.Schema.Parser)1 OnEnabled (org.apache.nifi.annotation.lifecycle.OnEnabled)1 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 SchemaAccessStrategy (org.apache.nifi.schema.access.SchemaAccessStrategy)1 SchemaField (org.apache.nifi.schema.access.SchemaField)1 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)1 SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)1