@Override public boolean equals(final Object obj) { if (obj == this) { return true; } if (obj == null) { return false; } if (!(obj instanceof SchemaIdentifier)) { return false; } final SchemaIdentifier other = (SchemaIdentifier) obj; return getName().equals(other.getName()) && getIdentifier().equals(other.getIdentifier()) && getVersion().equals(other.getVersion()) && getBranch().equals(other.getBranch()); }
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"); } }
@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; }
@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()); }
@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; }
final Long schemaId; final Optional<String> schemaName = schemaIdentifier.getName(); if (!schemaName.isPresent()) { throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Name is not present"); final Optional<String> schemaBranchName = schemaIdentifier.getBranch(); final OptionalInt schemaVersion = schemaIdentifier.getVersion(); final SchemaIdentifier resultSchemaIdentifier = SchemaIdentifier.builder() .id(schemaId) .name(schemaName.get())
private RecordSchema retrieveSchemaByName(final SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException { final Optional<String> schemaName = schemaIdentifier.getName(); if (!schemaName.isPresent()) { throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Name is not present"); } return schemaNameMap.get(schemaName.get()); }
final SchemaVersionInfo versionInfo; final OptionalLong schemaId = schemaIdentifier.getIdentifier(); if (!schemaId.isPresent()) { throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Id is not present"); final OptionalInt version = schemaIdentifier.getVersion(); if (!version.isPresent()) { throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Version is not present"); final SchemaIdentifier resultSchemaIdentifier = SchemaIdentifier.builder() .name(schemaName) .id(schemaId.getAsLong())
@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()); }
private RecordSchema retrieveSchemaByIdAndVersion(final SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException { final OptionalLong schemaId = schemaIdentifier.getIdentifier(); if (!schemaId.isPresent()) { throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Id is not present"); } final OptionalInt version = schemaIdentifier.getVersion(); if (!version.isPresent()) { throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Version is not present"); } final Tuple<Long, Integer> tuple = new Tuple<>(schemaId.getAsLong(), version.getAsInt()); final RecordSchema schema = schemaIdVersionMap.get(tuple); return schema; }
@Override public RecordSchema retrieveSchema(final SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException { final Optional<String> schemaName = schemaIdentifier.getName(); if (schemaName.isPresent()) { return retrieveSchemaByName(schemaName.get()); } else { throw new SchemaNotFoundException("This Schema Registry only supports retrieving a schema by name."); } }
@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; }
@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; }
private RecordSchema retrieveSchemaById(final SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException { final OptionalLong schemaId = schemaIdentifier.getIdentifier(); if (!schemaId.isPresent()) { throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Id is not present"); } final RecordSchema schema = client.getSchema((int) schemaId.getAsLong()); return schema; }
@Override public boolean equals(final Object obj) { if (obj == this) { return true; } if (obj == null) { return false; } if (!(obj instanceof SchemaIdentifier)) { return false; } final SchemaIdentifier other = (SchemaIdentifier) obj; return getName().equals(other.getName()) && getIdentifier().equals(other.getIdentifier()) && getVersion().equals(other.getVersion()) && getBranch().equals(other.getBranch()); }
/** * @deprecated Use {@link #retrieveSchema(SchemaIdentifier)} instead * * Retrieves and returns the RecordSchema based on the provided name of the schema available in Schema Registry. The RecordSchema * that is returned must have the Schema's name populated in its SchemaIdentifier. I.e., a call to * {@link RecordSchema}.{@link RecordSchema#getIdentifier() getIdentifier()}.{@link SchemaIdentifier#getName() getName()} * will always return an {@link java.util.Optional} that is not empty. * * @return the latest version of the schema with the given name, or <code>null</code> if no schema can be found with the given name. * @throws SchemaNotFoundException if unable to find the schema with the given name */ default RecordSchema retrieveSchema(String schemaName) throws IOException, SchemaNotFoundException { return retrieveSchema(SchemaIdentifier.builder().name(schemaName).build()); }
@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"); } }
private RecordSchema retrieveSchemaByName(final SchemaIdentifier schemaIdentifier) throws IOException, SchemaNotFoundException { final Optional<String> schemaName = schemaIdentifier.getName(); if (!schemaName.isPresent()) { throw new org.apache.nifi.schema.access.SchemaNotFoundException("Cannot retrieve schema because Schema Name is not present"); } final RecordSchema schema = client.getSchema(schemaName.get()); return schema; }
@Override public RecordSchema retrieveSchema(final SchemaIdentifier schemaIdentifier) throws IOException, org.apache.nifi.schema.access.SchemaNotFoundException { if (schemaIdentifier.getIdentifier().isPresent()) { return retrieveSchemaByIdAndVersion(schemaIdentifier); } else { return retrieveSchemaByName(schemaIdentifier); } }
@Override public RecordSchema getSchema(final Map<String, String> variables, final InputStream contentStream, final RecordSchema readSchema) throws SchemaNotFoundException, IOException { final byte[] buffer = new byte[13]; try { StreamUtils.fillBuffer(contentStream, buffer); } catch (final IOException ioe) { throw new SchemaNotFoundException("Could not read first 13 bytes from stream", ioe); } // This encoding 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.wrap(buffer); final int protocolVersion = bb.get(); if (protocolVersion != 1) { throw new SchemaNotFoundException("Schema Encoding appears to be of an incompatible version. The latest known Protocol is Version " + LATEST_PROTOCOL_VERSION + " but the data was encoded with version " + protocolVersion + " or was not encoded with this data format"); } final long schemaId = bb.getLong(); final int schemaVersion = bb.getInt(); final SchemaIdentifier schemaIdentifier = SchemaIdentifier.builder().id(schemaId).version(schemaVersion).build(); return schemaRegistry.retrieveSchema(schemaIdentifier); }