Search in sources :

Example 21 with SchemaIdentifier

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

the class HortonworksEncodedSchemaReferenceWriter 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 Encoded Schema Reference because the Schema Identifier is not known");
    }
    final OptionalInt versionOption = identifier.getVersion();
    if (!versionOption.isPresent()) {
        throw new SchemaNotFoundException("Cannot write Encoded Schema 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 22 with SchemaIdentifier

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

the class HortonworksEncodedSchemaReferenceWriter 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();
    final Integer version = identifier.getVersion().getAsInt();
    // This decoding follows the pattern that is provided for serializing data by the Hortonworks Schema Registry serializer
    // as it is provided at:
    // https://github.com/hortonworks/registry/blob/master/schema-registry/serdes/src/main/java/com/hortonworks/registries/schemaregistry/serdes/avro/AvroSnapshotSerializer.java
    final ByteBuffer bb = ByteBuffer.allocate(13);
    bb.put((byte) LATEST_PROTOCOL_VERSION);
    bb.putLong(id);
    bb.putInt(version);
    out.write(bb.array());
}
Also used : OptionalLong(java.util.OptionalLong) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) ByteBuffer(java.nio.ByteBuffer)

Example 23 with SchemaIdentifier

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

the class AbstractSchemaAccessStrategyTest method setup.

@Before
public void setup() {
    this.schemaRegistry = Mockito.mock(SchemaRegistry.class);
    final List<RecordField> fields = new ArrayList<>();
    fields.add(new RecordField("firstName", RecordFieldType.STRING.getDataType()));
    fields.add(new RecordField("lastName", RecordFieldType.STRING.getDataType()));
    final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("person").branch("master").version(1).id(1L).build();
    this.recordSchema = new SimpleRecordSchema(fields, schemaIdentifier);
}
Also used : SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordField(org.apache.nifi.serialization.record.RecordField) ArrayList(java.util.ArrayList) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) SchemaRegistry(org.apache.nifi.schemaregistry.services.SchemaRegistry) Before(org.junit.Before)

Example 24 with SchemaIdentifier

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

the class TestConfluentSchemaRegistryStrategy method testGetSchemaWithValidEncoding.

@Test
public void testGetSchemaWithValidEncoding() throws IOException, SchemaNotFoundException {
    final SchemaAccessStrategy schemaAccessStrategy = new ConfluentSchemaRegistryStrategy(schemaRegistry);
    final int schemaId = 123456;
    try (final ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(bytesOut)) {
        out.write(0);
        out.writeInt(schemaId);
        out.flush();
        try (final ByteArrayInputStream in = new ByteArrayInputStream(bytesOut.toByteArray())) {
            // the confluent strategy will read the id from the input stream and use '1' as the version
            final SchemaIdentifier expectedSchemaIdentifier = SchemaIdentifier.builder().id((long) schemaId).version(1).build();
            when(schemaRegistry.retrieveSchema(argThat(new SchemaIdentifierMatcher(expectedSchemaIdentifier)))).thenReturn(recordSchema);
            final RecordSchema retrievedSchema = schemaAccessStrategy.getSchema(Collections.emptyMap(), in, recordSchema);
            assertNotNull(retrievedSchema);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 25 with SchemaIdentifier

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

the class TestConfluentSchemaRegistryWriter method testValidateInvalidSchema.

@Test(expected = SchemaNotFoundException.class)
public void testValidateInvalidSchema() throws SchemaNotFoundException {
    final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().name("test").build();
    final RecordSchema recordSchema = createRecordSchema(schemaIdentifier);
    final SchemaAccessWriter schemaAccessWriter = new ConfluentSchemaRegistryWriter();
    schemaAccessWriter.validateSchema(recordSchema);
}
Also used : SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

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