Search in sources :

Example 1 with SchemaRegistryClient

use of com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient 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;
        }
    };
}
Also used : SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Before(org.junit.Before)

Example 2 with SchemaRegistryClient

use of com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient in project streamline by hortonworks.

the class StreamlineEventSerializer method configure.

@Override
public void configure(Map<String, ?> configs, boolean isKey) {
    // ignoring the isKey since this class is expected to be used only as a value serializer for now, value being StreamlineEvent
    avroSnapshotSerializer.init(configs);
    schemaRegistryClient = new SchemaRegistryClient(configs);
    String writerSchemaVersion = (String) configs.get("writer.schema.version");
    if (writerSchemaVersion != null && !writerSchemaVersion.isEmpty()) {
        this.writerSchemaVersion = Integer.parseInt(writerSchemaVersion);
    } else {
        this.writerSchemaVersion = null;
    }
}
Also used : SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)

Example 3 with SchemaRegistryClient

use of com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient in project registry by hortonworks.

the class ReviewServiceApp method run.

@Override
public void run(ReviewServiceConfig config, Environment environment) throws Exception {
    SchemaRegistryClient schemaRegistryClient = new SchemaRegistryClient(createConfig(config.getSchemaRegistryUrl()));
    environment.jersey().register(new ReviewServiceResource(schemaRegistryClient));
}
Also used : SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)

Example 4 with SchemaRegistryClient

use of com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient in project registry by hortonworks.

the class LocalRegistryServerHATest method testHASanity.

@Test
public void testHASanity() throws Exception {
    SchemaRegistryTestServerClientWrapper followerServer = followerSchemaRegistryServer();
    SchemaRegistryClient schemaRegistryClient = followerServer.getClient();
    // registering schema metadata on follower, this should have been redirected to leader.
    String schemaName = "foo";
    SchemaMetadata schemaMetadata = new SchemaMetadata.Builder(schemaName).type("avro").build();
    Long schemaId = schemaRegistryClient.registerSchemaMetadata(schemaMetadata);
    Assert.assertNotNull(schemaId);
    // registering a new schema on follower, this should have been redirected to leader.
    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"));
    // retrieve schema on leader as the schema data is stored in memory in leader. this data does not exist on
    // followers as the storage is inmemory.
    SchemaRegistryTestServerClientWrapper leaderServer = leaderSchemaRegistryServer();
    int leaderPort = leaderServer.getLocalPort();
    SchemaRegistryClient leaderClient = leaderServer.getClient();
    SchemaMetadataInfo schemaMetadataInfo = leaderClient.getSchemaMetadataInfo(schemaName);
    Assert.assertEquals(schemaMetadata, schemaMetadataInfo.getSchemaMetadata());
    // stop the leader server
    leaderServer.stopTestServer();
    // get the new leader server and run operations.
    SchemaRegistryTestServerClientWrapper newLeaderServer = leaderSchemaRegistryServer();
    Assert.assertNotEquals(leaderPort, newLeaderServer.getLocalPort());
    leaderClient = newLeaderServer.getClient();
    String receivedSchema = leaderClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v1.getVersion())).getSchemaText();
    Assert.assertEquals(schema1, receivedSchema);
}
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) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Test(org.junit.Test)

Example 5 with SchemaRegistryClient

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

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