Search in sources :

Example 6 with SchemaRegistryClient

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;
}
Also used : SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)

Example 7 with 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);
    });
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Schema(org.apache.avro.Schema) OptionalInt(java.util.OptionalInt) InitializationException(org.apache.nifi.reporting.InitializationException) IOException(java.io.IOException) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) OptionalLong(java.util.OptionalLong) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) Tuple(org.apache.nifi.util.Tuple) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)

Example 8 with SchemaRegistryClient

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;
}
Also used : ISchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.ISchemaRegistryClient) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)

Example 9 with SchemaRegistryClient

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();
}
Also used : SchemaRegistryTestServerClientWrapper(com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) Test(org.junit.Test)

Aggregations

SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)9 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)4 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)3 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)3 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)3 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)3 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)2 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)2 SchemaRegistryTestServerClientWrapper (com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper)2 IOException (java.io.IOException)2 OptionalInt (java.util.OptionalInt)2 OptionalLong (java.util.OptionalLong)2 Schema (org.apache.avro.Schema)2 InitializationException (org.apache.nifi.reporting.InitializationException)2 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)2 SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)2 Tuple (org.apache.nifi.util.Tuple)2 Test (org.junit.Test)2 ISchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.ISchemaRegistryClient)1 Before (org.junit.Before)1