Search in sources :

Example 6 with SchemaIdentifier

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

the class HortonworksAttributeSchemaReferenceWriter method getAttributes.

@Override
public Map<String, String> getAttributes(final RecordSchema schema) {
    final Map<String, String> attributes = new HashMap<>(4);
    final SchemaIdentifier id = schema.getIdentifier();
    final Long schemaId = id.getIdentifier().getAsLong();
    final Integer schemaVersion = id.getVersion().getAsInt();
    attributes.put(HortonworksAttributeSchemaReferenceStrategy.SCHEMA_ID_ATTRIBUTE, String.valueOf(schemaId));
    attributes.put(HortonworksAttributeSchemaReferenceStrategy.SCHEMA_VERSION_ATTRIBUTE, String.valueOf(schemaVersion));
    attributes.put(HortonworksAttributeSchemaReferenceStrategy.SCHEMA_PROTOCOL_VERSION_ATTRIBUTE, String.valueOf(LATEST_PROTOCOL_VERSION));
    if (id.getBranch().isPresent()) {
        attributes.put(SCHEMA_BRANCH_ATTRIBUTE, id.getBranch().get());
    }
    return attributes;
}
Also used : HashMap(java.util.HashMap) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier)

Example 7 with SchemaIdentifier

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

the class SchemaNameAsAttribute method getAttributes.

@Override
public Map<String, String> getAttributes(final RecordSchema schema) {
    final Map<String, String> attributes = new HashMap<>(3);
    final SchemaIdentifier identifier = schema.getIdentifier();
    final Optional<String> nameOption = identifier.getName();
    if (nameOption.isPresent()) {
        attributes.put(SCHEMA_NAME_ATTRIBUTE, nameOption.get());
    }
    final OptionalInt versionOption = identifier.getVersion();
    if (versionOption.isPresent()) {
        attributes.put(SCHEMA_VERSION_ATTRIBUTE, String.valueOf(versionOption.getAsInt()));
    }
    final Optional<String> branchOption = identifier.getBranch();
    if (branchOption.isPresent()) {
        attributes.put(SCHEMA_BRANCH_ATTRIBUTE, branchOption.get());
    }
    return attributes;
}
Also used : HashMap(java.util.HashMap) OptionalInt(java.util.OptionalInt) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier)

Example 8 with SchemaIdentifier

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

the class TestConfluentSchemaRegistryWriter method testWriteHeader.

@Test
public void testWriteHeader() throws IOException {
    final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().id(123456L).version(2).build();
    final RecordSchema recordSchema = createRecordSchema(schemaIdentifier);
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    final SchemaAccessWriter schemaAccessWriter = new ConfluentSchemaRegistryWriter();
    schemaAccessWriter.writeHeader(recordSchema, out);
    try (final ByteArrayInputStream bytesIn = new ByteArrayInputStream(out.toByteArray());
        final DataInputStream in = new DataInputStream(bytesIn)) {
        Assert.assertEquals(0, in.readByte());
        Assert.assertEquals((int) schemaIdentifier.getIdentifier().getAsLong(), in.readInt());
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) DataInputStream(java.io.DataInputStream) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 9 with SchemaIdentifier

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

the class TestHortonworksAttributeSchemaReferenceStrategy method testGetSchemaWithValidAttributes.

@Test
public void testGetSchemaWithValidAttributes() throws IOException, SchemaNotFoundException {
    final long schemaId = 123456;
    final int version = 2;
    final int protocol = 1;
    final Map<String, String> attributes = new HashMap<>();
    attributes.put(HortonworksAttributeSchemaReferenceStrategy.SCHEMA_ID_ATTRIBUTE, String.valueOf(schemaId));
    attributes.put(HortonworksAttributeSchemaReferenceStrategy.SCHEMA_VERSION_ATTRIBUTE, String.valueOf(version));
    attributes.put(HortonworksAttributeSchemaReferenceStrategy.SCHEMA_PROTOCOL_VERSION_ATTRIBUTE, String.valueOf(protocol));
    final SchemaAccessStrategy schemaAccessStrategy = new HortonworksAttributeSchemaReferenceStrategy(schemaRegistry);
    final SchemaIdentifier expectedSchemaIdentifier = SchemaIdentifier.builder().id(schemaId).version(version).build();
    when(schemaRegistry.retrieveSchema(argThat(new SchemaIdentifierMatcher(expectedSchemaIdentifier)))).thenReturn(recordSchema);
    final RecordSchema retrievedSchema = schemaAccessStrategy.getSchema(attributes, null, recordSchema);
    assertNotNull(retrievedSchema);
}
Also used : HashMap(java.util.HashMap) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Test(org.junit.Test)

Example 10 with SchemaIdentifier

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

the class TestHortonworksAttributeSchemaReferenceWriter method testValidateWithValidSchema.

@Test
public void testValidateWithValidSchema() throws SchemaNotFoundException {
    final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().id(123456L).version(2).build();
    final RecordSchema recordSchema = createRecordSchema(schemaIdentifier);
    final SchemaAccessWriter schemaAccessWriter = new HortonworksAttributeSchemaReferenceWriter();
    schemaAccessWriter.validateSchema(recordSchema);
}
Also used : SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) SimpleRecordSchema(org.apache.nifi.serialization.SimpleRecordSchema) 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