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;
}
};
}
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;
}
}
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));
}
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);
}
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);
});
}
Aggregations