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");
}
}
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());
}
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);
}
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);
}
}
}
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);
}
Aggregations