use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project registry by hortonworks.
the class SchemaVersionInfoCache method updateCacheInvalidationEntries.
private void updateCacheInvalidationEntries(SchemaVersionInfo schemaVersionInfo) {
// need to support this as SchemaIdVersion supports multiple ways to construct for backward compatible APIs
// this would have been simple without that.
SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaVersionInfo.getName(), schemaVersionInfo.getVersion());
SchemaIdVersion key1 = new SchemaIdVersion(schemaVersionInfo.getId());
idWithNameVersion.putIfAbsent(key1, schemaVersionKey);
Long schemaMetadataId = schemaVersionInfo.getSchemaMetadataId();
// schemaMetadataId can be null from earlier registry instances.
if (schemaMetadataId != null) {
SchemaIdVersion key2 = new SchemaIdVersion(schemaMetadataId, schemaVersionInfo.getVersion());
nameVersionWithIds.putIfAbsent(schemaVersionKey, Lists.newArrayList(key1, key2));
idWithNameVersion.putIfAbsent(key2, schemaVersionKey);
} else {
nameVersionWithIds.putIfAbsent(schemaVersionKey, Collections.singletonList(key1));
}
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project nifi by apache.
the class HortonworksSchemaRegistry method retrieveSchemaByName.
private RecordSchema retrieveSchemaByName(final SchemaIdentifier schemaIdentifier) throws org.apache.nifi.schema.access.SchemaNotFoundException, IOException {
final SchemaRegistryClient client = getClient();
final SchemaVersionInfo versionInfo;
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();
try {
final SchemaMetadataInfo metadataInfo = client.getSchemaMetadataInfo(schemaName.get());
if (metadataInfo == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with name '" + schemaName + "'");
}
schemaId = metadataInfo.getId();
if (schemaId == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with name '" + schemaName + "'");
}
// possible scenarios are name only, name + branch, or name + version
if (schemaVersion.isPresent()) {
final SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaName.get(), schemaVersion.getAsInt());
versionInfo = getSchemaVersionInfo(client, schemaVersionKey);
} else {
versionInfo = getLatestSchemaVersionInfo(client, schemaName.get(), schemaBranchName.orElse(null));
}
if (versionInfo == null || versionInfo.getVersion() == null) {
final String message = createErrorMessage("Could not find schema", schemaName, schemaBranchName, schemaVersion);
throw new org.apache.nifi.schema.access.SchemaNotFoundException(message);
}
} catch (final Exception e) {
final String message = createErrorMessage("Failed to retrieve schema", schemaName, schemaBranchName, schemaVersion);
handleException(message, e);
return null;
}
final String schemaText = versionInfo.getSchemaText();
final SchemaIdentifier resultSchemaIdentifier = SchemaIdentifier.builder().id(schemaId).name(schemaName.get()).branch(schemaBranchName.orElse(null)).version(versionInfo.getVersion()).build();
final Tuple<SchemaIdentifier, String> tuple = new Tuple<>(resultSchemaIdentifier, schemaText);
return schemaNameToSchemaMap.computeIfAbsent(tuple, t -> {
final Schema schema = new Schema.Parser().parse(schemaText);
return AvroTypeUtil.createSchema(schema, schemaText, resultSchemaIdentifier);
});
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project nifi by apache.
the class HortonworksSchemaRegistry method retrieveSchemaByIdAndVersion.
private RecordSchema retrieveSchemaByIdAndVersion(final SchemaIdentifier schemaIdentifier) throws org.apache.nifi.schema.access.SchemaNotFoundException, IOException {
final SchemaRegistryClient client = getClient();
final String schemaName;
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");
}
try {
final SchemaMetadataInfo info = client.getSchemaMetadataInfo(schemaId.getAsLong());
if (info == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with ID '" + schemaId + "' and version '" + version + "'");
}
final SchemaMetadata metadata = info.getSchemaMetadata();
schemaName = metadata.getName();
final SchemaVersionKey schemaVersionKey = new SchemaVersionKey(schemaName, version.getAsInt());
versionInfo = getSchemaVersionInfo(client, schemaVersionKey);
if (versionInfo == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with ID '" + schemaId + "' and version '" + version + "'");
}
} catch (final Exception e) {
handleException("Failed to retrieve schema with ID '" + schemaId + "' and version '" + version + "'", e);
return null;
}
final String schemaText = versionInfo.getSchemaText();
final SchemaIdentifier resultSchemaIdentifier = SchemaIdentifier.builder().name(schemaName).id(schemaId.getAsLong()).version(version.getAsInt()).build();
final Tuple<SchemaIdentifier, String> tuple = new Tuple<>(resultSchemaIdentifier, schemaText);
return schemaNameToSchemaMap.computeIfAbsent(tuple, t -> {
final Schema schema = new Schema.Parser().parse(schemaText);
return AvroTypeUtil.createSchema(schema, schemaText, resultSchemaIdentifier);
});
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project streamline by hortonworks.
the class AvroStreamsSnapshotDeserializerTest method testAvroPayloadConversions.
@Test
public void testAvroPayloadConversions() throws Exception {
try (InputStream schemaStream = AvroStreamsSnapshotDeserializerTest.class.getResourceAsStream("/avro/complex.avsc")) {
CustomAvroSerializer customAvroSerializer = new CustomAvroSerializer();
customAvroSerializer.init(Collections.singletonMap("serdes.protocol.version", (byte) 1));
final Schema schema = new Schema.Parser().parse(schemaStream);
GenericRecord inputRecord = generateGenericRecord(schema);
LOG.info("Generated record [{}]", inputRecord);
byte[] serializedBytes = customAvroSerializer.customSerialize(inputRecord);
SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("topic-1").type("avro").schemaGroup("kafka").build();
new Expectations() {
{
mockSchemaRegistryClient.getSchemaVersionInfo(withEqual(SCHEMA_ID_VERSION));
// only versions of schema matters for this mock, as getVersion is only used during de-serialization
result = new SchemaVersionInfo(1l, schemaMetadata.getName(), SCHEMA_ID_VERSION.getVersion(), "doesNotMatter", 1l, "doesNotMatter");
mockSchemaRegistryClient.getSchemaMetadataInfo(withEqual(schemaMetadata.getName()));
result = new SchemaMetadataInfo(schemaMetadata);
}
};
AvroStreamsSnapshotDeserializer avroStreamsSnapshotDeserializer = new AvroStreamsSnapshotDeserializer() {
@Override
protected Schema getSchema(SchemaVersionKey schemaVersionKey) {
return schema;
}
};
Map<String, String> config = Collections.singletonMap(SchemaRegistryClient.Configuration.SCHEMA_REGISTRY_URL.name(), "http://localhost:8080/api/v1");
avroStreamsSnapshotDeserializer.init(config);
Object deserializedObject = avroStreamsSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedBytes), 1);
Map<Object, Object> map = (Map<Object, Object>) deserializedObject;
String deserializedJson = new ObjectMapper().writeValueAsString(map);
String inputJson = GenericData.get().toString(inputRecord);
LOG.info("inputJson #{}# " + inputJson);
LOG.info("deserializedJson = #{}#" + deserializedJson);
ObjectMapper objectMapper = new ObjectMapper();
Assert.assertEquals(objectMapper.readTree(inputJson), objectMapper.readTree(deserializedJson));
}
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionKey in project registry by hortonworks.
the class AvroSchemaResolver method getIncludedSchemaVersions.
private List<SchemaVersionKey> getIncludedSchemaVersions(String schemaText) throws InvalidSchemaException {
JsonNode jsonNode = null;
try {
jsonNode = new ObjectMapper().readTree(schemaText);
} catch (IOException e) {
throw new InvalidSchemaException(e);
}
JsonNode includeSchemaNodes = jsonNode.get("includeSchemas");
List<SchemaVersionKey> includedSchemaVersions = new ArrayList<>();
if (includeSchemaNodes != null) {
if (!includeSchemaNodes.isArray()) {
throw new InvalidSchemaException("includeSchemas should be an array of strings");
}
for (JsonNode includeSchema : includeSchemaNodes) {
String name = includeSchema.get("name").asText();
JsonNode versionNode = includeSchema.get("version");
int version = versionNode != null ? versionNode.asInt() : SchemaVersionKey.LATEST_VERSION;
includedSchemaVersions.add(new SchemaVersionKey(name, version));
}
}
return includedSchemaVersions;
}
Aggregations