use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project nifi by apache.
the class HortonworksSchemaRegistry method getLatestSchemaVersionInfo.
private SchemaVersionInfo getLatestSchemaVersionInfo(final SchemaRegistryClient client, final String schemaName, final String branchName) throws org.apache.nifi.schema.access.SchemaNotFoundException {
try {
// Try to fetch the SchemaVersionInfo from the cache.
final Tuple<String, String> nameAndBranch = new Tuple<>(schemaName, branchName);
final Tuple<SchemaVersionInfo, Long> timestampedVersionInfo = schemaVersionByNameCache.get(nameAndBranch);
// Determine if the timestampedVersionInfo is expired
boolean fetch = false;
if (timestampedVersionInfo == null) {
fetch = true;
} else {
final long minTimestamp = System.nanoTime() - versionInfoCacheNanos;
fetch = timestampedVersionInfo.getValue() < minTimestamp;
}
// If not expired, use what we got from the cache
if (!fetch) {
return timestampedVersionInfo.getKey();
}
// schema version info was expired or not found in cache. Fetch from schema registry
final SchemaVersionInfo versionInfo;
if (StringUtils.isBlank(branchName)) {
versionInfo = client.getLatestSchemaVersionInfo(schemaName);
} else {
versionInfo = client.getLatestSchemaVersionInfo(branchName, schemaName);
}
if (versionInfo == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with name '" + schemaName + "'");
}
// Store new version in cache.
final Tuple<SchemaVersionInfo, Long> tuple = new Tuple<>(versionInfo, System.nanoTime());
schemaVersionByNameCache.put(nameAndBranch, tuple);
return versionInfo;
} catch (final SchemaNotFoundException e) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException(e);
}
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project nifi by apache.
the class HortonworksSchemaRegistry method getSchemaVersionInfo.
private SchemaVersionInfo getSchemaVersionInfo(final SchemaRegistryClient client, final SchemaVersionKey key) throws org.apache.nifi.schema.access.SchemaNotFoundException {
try {
// Try to fetch the SchemaVersionInfo from the cache.
final Tuple<SchemaVersionInfo, Long> timestampedVersionInfo = schemaVersionByKeyCache.get(key);
// Determine if the timestampedVersionInfo is expired
boolean fetch = false;
if (timestampedVersionInfo == null) {
fetch = true;
} else {
final long minTimestamp = System.nanoTime() - versionInfoCacheNanos;
fetch = timestampedVersionInfo.getValue() < minTimestamp;
}
// If not expired, use what we got from the cache
if (!fetch) {
return timestampedVersionInfo.getKey();
}
// schema version info was expired or not found in cache. Fetch from schema registry
final SchemaVersionInfo versionInfo = client.getSchemaVersionInfo(key);
if (versionInfo == null) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException("Could not find schema with name '" + key.getSchemaName() + "' and version " + key.getVersion());
}
// Store new version in cache.
final Tuple<SchemaVersionInfo, Long> tuple = new Tuple<>(versionInfo, System.nanoTime());
schemaVersionByKeyCache.put(key, tuple);
return versionInfo;
} catch (final SchemaNotFoundException e) {
throw new org.apache.nifi.schema.access.SchemaNotFoundException(e);
}
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project nifi by apache.
the class TestHortonworksSchemaRegistry method setup.
@Before
public void setup() throws SchemaNotFoundException {
schemaVersionInfoMap.clear();
schemaMetadataInfoMap.clear();
client = mock(SchemaRegistryClient.class);
doAnswer(new Answer<SchemaVersionInfo>() {
@Override
public SchemaVersionInfo answer(final InvocationOnMock invocation) throws Throwable {
final String schemaName = invocation.getArgumentAt(0, String.class);
final SchemaVersionInfo info = schemaVersionInfoMap.get(schemaName);
if (info == null) {
throw new SchemaNotFoundException();
}
return info;
}
}).when(client).getLatestSchemaVersionInfo(any(String.class));
doAnswer(new Answer<SchemaMetadataInfo>() {
@Override
public SchemaMetadataInfo answer(InvocationOnMock invocation) throws Throwable {
final String schemaName = invocation.getArgumentAt(0, String.class);
final SchemaMetadataInfo info = schemaMetadataInfoMap.get(schemaName);
if (info == null) {
throw new SchemaNotFoundException();
}
return info;
}
}).when(client).getSchemaMetadataInfo(any(String.class));
registry = new HortonworksSchemaRegistry() {
@Override
protected synchronized SchemaRegistryClient getClient() {
return client;
}
};
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project streamline by hortonworks.
the class StreamlineEventSerializer method serialize.
@Override
public byte[] serialize(String topic, StreamlineEvent streamlineEvent) {
SchemaMetadata schemaMetadata = getSchemaKey(topic, false);
SchemaVersionInfo schemaVersionInfo;
try {
schemaMetadata = schemaRegistryClient.getSchemaMetadataInfo(schemaMetadata.getName()).getSchemaMetadata();
if (writerSchemaVersion != null) {
schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaMetadata.getName(), writerSchemaVersion));
} else {
schemaVersionInfo = schemaRegistryClient.getLatestSchemaVersionInfo(schemaMetadata.getName());
}
} catch (SchemaNotFoundException e) {
LOG.error("Exception occured while getting SchemaVersionInfo for " + schemaMetadata, e);
throw new RuntimeException(e);
}
if (streamlineEvent == null || streamlineEvent.isEmpty()) {
return null;
} else {
return avroSnapshotSerializer.serialize(getAvroRecord(streamlineEvent, new Schema.Parser().parse(schemaVersionInfo.getSchemaText())), schemaMetadata);
}
}
use of com.hortonworks.registries.schemaregistry.SchemaVersionInfo in project streamline by hortonworks.
the class SchemaResource method getSchemaForVersion.
@GET
@Path("/{schemaName}/versions/{version}")
@Timed
@Produces(MediaType.APPLICATION_JSON)
public Response getSchemaForVersion(@PathParam("schemaName") String schemaName, @PathParam("version") String version, @Context SecurityContext securityContext) {
try {
LOG.info("Get schema:version [{}:{}]", schemaName, version);
SchemaVersionInfo schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, Integer.parseInt(version)));
if (schemaVersionInfo != null) {
LOG.debug("Received schema version from schema registry: [{}]", schemaVersionInfo);
String schema = schemaVersionInfo.getSchemaText();
if (schema != null && !schema.isEmpty()) {
schema = AvroStreamlineSchemaConverter.convertAvroSchemaToStreamlineSchema(schema);
}
LOG.debug("Converted schema: [{}]", schema);
return WSUtils.respondEntity(schema, OK);
} else {
throw new SchemaNotFoundException("Version " + version + " of schema " + schemaName + " not found ");
}
} catch (SchemaNotFoundException e) {
LOG.error("Schema not found: [{}]", schemaName, e);
throw EntityNotFoundException.byName(schemaName);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations