Search in sources :

Example 1 with SchemaIdentifier

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

the class RestSchemaRegistryClient method createRecordSchema.

private RecordSchema createRecordSchema(final JsonNode schemaNode) throws SchemaNotFoundException {
    final String subject = schemaNode.get(SUBJECT_FIELD_NAME).asText();
    final int version = schemaNode.get(VERSION_FIELD_NAME).asInt();
    final int id = schemaNode.get(ID_FIELD_NAME).asInt();
    final String schemaText = schemaNode.get(SCHEMA_TEXT_FIELD_NAME).asText();
    try {
        final Schema avroSchema = new Schema.Parser().parse(schemaText);
        final SchemaIdentifier schemaId = SchemaIdentifier.builder().name(subject).id(Long.valueOf(id)).version(version).build();
        final RecordSchema recordSchema = AvroTypeUtil.createSchema(avroSchema, schemaText, schemaId);
        return recordSchema;
    } catch (final SchemaParseException spe) {
        throw new SchemaNotFoundException("Obtained Schema with id " + id + " and name " + subject + " from Confluent Schema Registry but the Schema Text that was returned is not a valid Avro Schema");
    }
}
Also used : SchemaParseException(org.apache.avro.SchemaParseException) Schema(org.apache.avro.Schema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema)

Example 2 with SchemaIdentifier

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

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

the class ConfluentSchemaRegistryStrategy method getSchema.

@Override
public RecordSchema getSchema(final Map<String, String> variables, final InputStream contentStream, final RecordSchema readSchema) throws SchemaNotFoundException, IOException {
    final byte[] buffer = new byte[5];
    try {
        StreamUtils.fillBuffer(contentStream, buffer);
    } catch (final IOException ioe) {
        throw new SchemaNotFoundException("Could not read first 5 bytes from stream", ioe);
    }
    // This encoding follows the pattern that is provided for serializing data by the Confluent Schema Registry serializer
    // as it is provided at:
    // http://docs.confluent.io/current/schema-registry/docs/serializer-formatter.html#wire-format
    // The format consists of the first byte always being 0, to indicate a 'magic byte' followed by 4 bytes
    // representing the schema id.
    final ByteBuffer bb = ByteBuffer.wrap(buffer);
    final int magicByte = bb.get();
    if (magicByte != 0) {
        throw new SchemaNotFoundException("Schema Encoding appears to be of an incompatible version. " + "Expected stream to begin with a Magic Byte of 0 but first byte was " + magicByte);
    }
    final int schemaId = bb.getInt();
    final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().id(Long.valueOf(schemaId)).version(1).build();
    return schemaRegistry.retrieveSchema(schemaIdentifier);
}
Also used : IOException(java.io.IOException) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) ByteBuffer(java.nio.ByteBuffer)

Example 4 with SchemaIdentifier

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

the class ConfluentSchemaRegistryWriter method validateSchema.

@Override
public void validateSchema(RecordSchema schema) throws SchemaNotFoundException {
    final SchemaIdentifier identifier = schema.getIdentifier();
    final OptionalLong identifierOption = identifier.getIdentifier();
    if (!identifierOption.isPresent()) {
        throw new SchemaNotFoundException("Cannot write Confluent Schema Registry Reference because the Schema Identifier is not known");
    }
    final OptionalInt versionOption = identifier.getVersion();
    if (!versionOption.isPresent()) {
        throw new SchemaNotFoundException("Cannot write Confluent Schema Registry Reference because the Schema Version is not known");
    }
}
Also used : OptionalLong(java.util.OptionalLong) OptionalInt(java.util.OptionalInt) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier)

Example 5 with SchemaIdentifier

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

the class ConfluentSchemaRegistryWriter method writeHeader.

@Override
public void writeHeader(final RecordSchema schema, final OutputStream out) throws IOException {
    final SchemaIdentifier identifier = schema.getIdentifier();
    final Long id = identifier.getIdentifier().getAsLong();
    // This encoding follows the pattern that is provided for serializing data by the Confluent Schema Registry serializer
    // as it is provided at:
    // http://docs.confluent.io/current/schema-registry/docs/serializer-formatter.html#wire-format
    // The format consists of the first byte always being 0, to indicate a 'magic byte' followed by 4 bytes
    // representing the schema id.
    final ByteBuffer bb = ByteBuffer.allocate(5);
    bb.put((byte) 0);
    bb.putInt(id.intValue());
    out.write(bb.array());
}
Also used : OptionalLong(java.util.OptionalLong) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) ByteBuffer(java.nio.ByteBuffer)

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