use of com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient in project nifi by apache.
the class HortonworksSchemaRegistry method getClient.
protected synchronized SchemaRegistryClient getClient() {
if (!initialized) {
schemaRegistryClient = new SchemaRegistryClient(schemaRegistryConfig);
initialized = true;
}
return schemaRegistryClient;
}
use of com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient 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.client.SchemaRegistryClient in project registry by hortonworks.
the class AbstractSerDes method init.
public final void init(Map<String, ?> config) {
if (closed) {
throw new IllegalStateException("Closed instance can not be initialized again");
}
if (initialized) {
LOG.info("This instance [{}] is already inited", this);
return;
}
LOG.debug("Initialized with config: [{}]", config);
if (schemaRegistryClient == null) {
schemaRegistryClient = new SchemaRegistryClient(config);
}
doInit(config);
initialized = true;
}
use of com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient in project registry by hortonworks.
the class LocalRegistryServerTest method testSanity.
@Test
public void testSanity() throws Exception {
SchemaRegistryTestServerClientWrapper schemaRegistryTestServerClientWrapper = new SchemaRegistryTestServerClientWrapper(SCHEMA_REGISTRY_TEST_CONFIGURATION);
schemaRegistryTestServerClientWrapper.startTestServer();
SchemaRegistryClient schemaRegistryClient = schemaRegistryTestServerClientWrapper.getClient();
// registering schema metadata
SchemaMetadata schemaMetadata = new SchemaMetadata.Builder("foo").type("avro").build();
Long schemaId = schemaRegistryClient.registerSchemaMetadata(schemaMetadata);
Assert.assertNotNull(schemaId);
// registering a new schema
String schemaName = schemaMetadata.getName();
String schema1 = IOUtils.toString(LocalRegistryServerTest.class.getResourceAsStream("/schema-1.avsc"), "UTF-8");
SchemaIdVersion v1 = schemaRegistryClient.addSchemaVersion(schemaName, new SchemaVersion(schema1, "Initial version of the schema"));
schemaRegistryTestServerClientWrapper.stopTestServer();
}
Aggregations