Search in sources :

Example 1 with SchemaVersion

use of com.hortonworks.registries.schemaregistry.SchemaVersion in project registry by hortonworks.

the class SampleSchemaRegistryClientApp method runCustomSerDesApi.

public void runCustomSerDesApi() throws Exception {
    // upload jar file
    String serdesJarName = "/serdes-examples.jar";
    InputStream serdesJarInputStream = SampleSchemaRegistryClientApp.class.getResourceAsStream(serdesJarName);
    if (serdesJarInputStream == null) {
        throw new RuntimeException("Jar " + serdesJarName + " could not be loaded");
    }
    String fileId = schemaRegistryClient.uploadFile(serdesJarInputStream);
    SchemaMetadata schemaMetadata = createSchemaMetadata("serdes-device-" + System.currentTimeMillis());
    SchemaIdVersion v1 = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(getSchema("/device.avsc"), "Initial version of the schema"));
    // register serializer/deserializer
    Long serDesId = registerSimpleSerDes(fileId);
    // map serializer and deserializer with schemakey
    // for each schema, one serializer/deserializer is sufficient unless someone want to maintain multiple implementations of serializers/deserializers
    String schemaName = schemaMetadata.getName();
    schemaRegistryClient.mapSchemaWithSerDes(schemaName, serDesId);
    SnapshotSerializer<Object, byte[], SchemaMetadata> snapshotSerializer = getSnapshotSerializer(schemaMetadata);
    String payload = "Random text: " + new Random().nextLong();
    byte[] serializedBytes = snapshotSerializer.serialize(payload, schemaMetadata);
    SnapshotDeserializer<byte[], Object, Integer> snapshotdeserializer = getSnapshotDeserializer(schemaMetadata);
    Object deserializedObject = snapshotdeserializer.deserialize(serializedBytes, null);
    LOG.info("Given payload and deserialized object are equal: " + payload.equals(deserializedObject));
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) Random(java.util.Random)

Example 2 with SchemaVersion

use of com.hortonworks.registries.schemaregistry.SchemaVersion in project registry by hortonworks.

the class SampleSchemaRegistryClientApp method runSchemaApis.

public void runSchemaApis() throws Exception {
    String schemaFileName = "/device.avsc";
    String schema1 = getSchema(schemaFileName);
    SchemaMetadata schemaMetadata = createSchemaMetadata("com.hwx.schemas.sample-" + System.currentTimeMillis());
    // registering a new schema
    SchemaIdVersion v1 = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "Initial version of the schema"));
    LOG.info("Registered schema [{}] and returned version [{}]", schema1, v1);
    // adding a new version of the schema
    String schema2 = getSchema("/device-next.avsc");
    SchemaVersion schemaInfo2 = new SchemaVersion(schema2, "second version");
    SchemaIdVersion v2 = schemaRegistryClient.addSchemaVersion(schemaMetadata, schemaInfo2);
    LOG.info("Registered schema [{}] and returned version [{}]", schema2, v2);
    // adding same schema returns the earlier registered version
    SchemaIdVersion version = schemaRegistryClient.addSchemaVersion(schemaMetadata, schemaInfo2);
    LOG.info("Received version [{}] for schema metadata [{}]", version, schemaMetadata);
    // get a specific version of the schema
    String schemaName = schemaMetadata.getName();
    SchemaVersionInfo schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(new SchemaVersionKey(schemaName, v2.getVersion()));
    LOG.info("Received schema version info [{}] for schema metadata [{}]", schemaVersionInfo, schemaMetadata);
    // get latest version of the schema
    SchemaVersionInfo latest = schemaRegistryClient.getLatestSchemaVersionInfo(schemaName);
    LOG.info("Latest schema with schema key [{}] is : [{}]", schemaMetadata, latest);
    // get all versions of the schema
    Collection<SchemaVersionInfo> allVersions = schemaRegistryClient.getAllVersions(schemaName);
    LOG.info("All versions of schema key [{}] is : [{}]", schemaMetadata, allVersions);
    // finding schemas containing a specific field
    SchemaFieldQuery md5FieldQuery = new SchemaFieldQuery.Builder().name("md5").build();
    Collection<SchemaVersionKey> md5SchemaVersionKeys = schemaRegistryClient.findSchemasByFields(md5FieldQuery);
    LOG.info("Schemas containing field query [{}] : [{}]", md5FieldQuery, md5SchemaVersionKeys);
    SchemaFieldQuery txidFieldQuery = new SchemaFieldQuery.Builder().name("txid").build();
    Collection<SchemaVersionKey> txidSchemaVersionKeys = schemaRegistryClient.findSchemasByFields(txidFieldQuery);
    LOG.info("Schemas containing field query [{}] : [{}]", txidFieldQuery, txidSchemaVersionKeys);
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaFieldQuery(com.hortonworks.registries.schemaregistry.SchemaFieldQuery) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey)

Example 3 with SchemaVersion

use of com.hortonworks.registries.schemaregistry.SchemaVersion in project registry by hortonworks.

the class CustomStateTransitionTest method testTransitionToRejectedState.

@Test
public void testTransitionToRejectedState() throws IOException, SchemaNotFoundException, InvalidSchemaException, IncompatibleSchemaException, SchemaBranchAlreadyExistsException, SchemaLifecycleException {
    SchemaMetadata schemaMetadata = createSchemaMetadata("test", SchemaCompatibility.NONE);
    SchemaBranch branch1 = new SchemaBranch("BRANCH1", schemaMetadata.getName());
    String schema1 = getSchema("/device.avsc");
    schemaRegistryClient.addSchemaMetadata(schemaMetadata);
    SchemaIdVersion schemaIdVersion1 = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema1, "initial version"));
    schemaRegistryClient.createSchemaBranch(schemaIdVersion1.getSchemaVersionId(), branch1);
    String schema2 = AvroSchemaRegistryClientUtil.getSchema("/device1.avsc");
    SchemaIdVersion schemaIdVersion2 = schemaRegistryClient.addSchemaVersion(branch1.getName(), schemaMetadata, new SchemaVersion(schema2, "modify version"));
    schemaRegistryClient.startSchemaVersionReview(schemaIdVersion2.getSchemaVersionId());
    SchemaVersionInfo schemaVersionInfoAtReviewState = schemaRegistryClient.getSchemaVersionInfo(new SchemaIdVersion(schemaIdVersion2.getSchemaVersionId()));
    Assert.assertTrue(schemaVersionInfoAtReviewState.getStateId().equals(CustomReviewCycleStates.PEER_REVIEW_STATE.getId()));
    schemaRegistryClient.transitionState(schemaIdVersion2.getSchemaVersionId(), CustomReviewCycleStates.REJECTED_REVIEW_STATE.getId(), "Rejected by X".getBytes());
    SchemaVersionInfo schemaVersionInfoAtRejectedState = schemaRegistryClient.getSchemaVersionInfo(new SchemaIdVersion(schemaIdVersion2.getSchemaVersionId()));
    Assert.assertTrue(schemaVersionInfoAtRejectedState.getStateId().equals(CustomReviewCycleStates.REJECTED_REVIEW_STATE.getId()));
}
Also used : SchemaBranch(com.hortonworks.registries.schemaregistry.SchemaBranch) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) Test(org.junit.Test)

Example 4 with SchemaVersion

use of com.hortonworks.registries.schemaregistry.SchemaVersion in project registry by hortonworks.

the class AvroSchemaRegistryClientTest method testValidationLevels.

@Test
public void testValidationLevels() throws Exception {
    SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BOTH);
    String schemaName = schemaMetadata.getName();
    Long id = SCHEMA_REGISTRY_CLIENT.registerSchemaMetadata(schemaMetadata);
    SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-1.avsc"), "Initial version of the schema"));
    SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-2.avsc"), "Second version of the schema"));
    SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-3.avsc"), "Third version of the schema, removes name field"));
    try {
        SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-4.avsc"), "Forth version of the schema, adds back name field, but different type"));
        Assert.fail("Should throw IncompatibleSchemaException as check against all schema's would find name field is not compatible with v1 and v2");
    } catch (IncompatibleSchemaException ise) {
    // expected
    }
    SchemaMetadata currentSchemaMetadata = SCHEMA_REGISTRY_CLIENT.getSchemaMetadataInfo(schemaName).getSchemaMetadata();
    SchemaMetadata schemaMetadataToUpdateTo = new SchemaMetadata.Builder(currentSchemaMetadata).validationLevel(SchemaValidationLevel.LATEST).build();
    SchemaMetadataInfo updatedSchemaMetadata = SCHEMA_REGISTRY_CLIENT.updateSchemaMetadata(schemaName, schemaMetadataToUpdateTo);
    Assert.assertEquals(SchemaValidationLevel.LATEST, updatedSchemaMetadata.getSchemaMetadata().getValidationLevel());
    try {
        SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaName, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/schema-4.avsc"), "Forth version of the schema, adds back name field, but different type"));
    } catch (IncompatibleSchemaException ise) {
        Assert.fail("Should not throw IncompatibleSchemaException as check against only latest schema as such should ignore v1 and v2");
    }
}
Also used : IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) IntegrationTest(com.hortonworks.registries.common.test.IntegrationTest) Test(org.junit.Test)

Example 5 with SchemaVersion

use of com.hortonworks.registries.schemaregistry.SchemaVersion in project registry by hortonworks.

the class AvroSchemaRegistryClientTest method testSerializerOps.

@Test
public void testSerializerOps() throws Exception {
    String fileId = uploadFile();
    SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BOTH);
    SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaMetadata, new SchemaVersion(AvroSchemaRegistryClientUtil.getSchema("/device.avsc"), "Initial version of the schema"));
    SerDesPair serDesPair = createSerDesInfo(fileId);
    Long serDesId = SCHEMA_REGISTRY_CLIENT.addSerDes(serDesPair);
    Assert.assertNotNull("Returned serDesId can not be null", serDesId);
    String schemaName = schemaMetadata.getName();
    SCHEMA_REGISTRY_CLIENT.mapSchemaWithSerDes(schemaName, serDesId);
    Collection<SerDesInfo> serializers = SCHEMA_REGISTRY_CLIENT.getSerDes(schemaName);
    Assert.assertTrue(serializers.stream().map(x -> x.getSerDesPair()).collect(Collectors.toList()).contains(serDesPair));
}
Also used : Arrays(java.util.Arrays) SchemaBranchNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchNotFoundException) SchemaRegistryTestProfileType(com.hortonworks.registries.schemaregistry.avro.conf.SchemaRegistryTestProfileType) SchemaRegistryTestServerClientWrapper(com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper) SERDES_PROTOCOL_VERSION(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer.SERDES_PROTOCOL_VERSION) SerDesPair(com.hortonworks.registries.schemaregistry.SerDesPair) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) AvroSnapshotSerializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer) Map(java.util.Map) BadRequestException(javax.ws.rs.BadRequestException) SchemaVersionInfo(com.hortonworks.registries.schemaregistry.SchemaVersionInfo) SerDesInfo(com.hortonworks.registries.schemaregistry.SerDesInfo) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) Utf8(org.apache.avro.util.Utf8) SerDesProtocolHandlerRegistry(com.hortonworks.registries.schemaregistry.serdes.avro.SerDesProtocolHandlerRegistry) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) SchemaRegistryTestName(com.hortonworks.registries.schemaregistry.avro.util.SchemaRegistryTestName) Collection(java.util.Collection) BAD_REQUEST_PARAM_MISSING(com.hortonworks.registries.common.catalog.CatalogResponse.ResponseMessage.BAD_REQUEST_PARAM_MISSING) Set(java.util.Set) SchemaRegistryClient(com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient) UUID(java.util.UUID) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) SchemaBranch(com.hortonworks.registries.schemaregistry.SchemaBranch) Response(javax.ws.rs.core.Response) SchemaNotFoundException(com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException) CustomParameterizedRunner(com.hortonworks.registries.schemaregistry.avro.util.CustomParameterizedRunner) SchemaBranchAlreadyExistsException(com.hortonworks.registries.schemaregistry.errors.SchemaBranchAlreadyExistsException) IntegrationTest(com.hortonworks.registries.common.test.IntegrationTest) UNSUPPORTED_SCHEMA_TYPE(com.hortonworks.registries.common.catalog.CatalogResponse.ResponseMessage.UNSUPPORTED_SCHEMA_TYPE) IntStream(java.util.stream.IntStream) SchemaVersionLifecycleStateMachineInfo(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateMachineInfo) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SchemaVersionLifecycleStates(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStates) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) AvroSchemaRegistryClientUtil(com.hortonworks.registries.schemaregistry.avro.util.AvroSchemaRegistryClientUtil) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SchemaValidationLevel(com.hortonworks.registries.schemaregistry.SchemaValidationLevel) SchemaVersionLifecycleStateTransition(com.hortonworks.registries.schemaregistry.state.SchemaVersionLifecycleStateTransition) IncompatibleSchemaException(com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException) SchemaCompatibility(com.hortonworks.registries.schemaregistry.SchemaCompatibility) Files(java.nio.file.Files) InvalidSchemaException(com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SchemaLifecycleException(com.hortonworks.registries.schemaregistry.state.SchemaLifecycleException) FileOutputStream(java.io.FileOutputStream) CatalogResponse(com.hortonworks.registries.common.catalog.CatalogResponse) Test(org.junit.Test) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Maps(com.google.common.collect.Maps) File(java.io.File) SchemaMetadataInfo(com.hortonworks.registries.schemaregistry.SchemaMetadataInfo) Rule(org.junit.Rule) SchemaVersionKey(com.hortonworks.registries.schemaregistry.SchemaVersionKey) Assert(org.junit.Assert) Collections(java.util.Collections) SchemaIdVersion(com.hortonworks.registries.schemaregistry.SchemaIdVersion) InputStream(java.io.InputStream) SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) SchemaVersion(com.hortonworks.registries.schemaregistry.SchemaVersion) SerDesPair(com.hortonworks.registries.schemaregistry.SerDesPair) SerDesInfo(com.hortonworks.registries.schemaregistry.SerDesInfo) IntegrationTest(com.hortonworks.registries.common.test.IntegrationTest) Test(org.junit.Test)

Aggregations

SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)27 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)25 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)18 Test (org.junit.Test)16 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)7 IntegrationTest (com.hortonworks.registries.common.test.IntegrationTest)6 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)6 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)6 SchemaBranch (com.hortonworks.registries.schemaregistry.SchemaBranch)4 IncompatibleSchemaException (com.hortonworks.registries.schemaregistry.errors.IncompatibleSchemaException)4 IOException (java.io.IOException)4 Timed (com.codahale.metrics.annotation.Timed)3 CatalogResponse (com.hortonworks.registries.common.catalog.CatalogResponse)3 SchemaRegistryTestServerClientWrapper (com.hortonworks.registries.schemaregistry.avro.helper.SchemaRegistryTestServerClientWrapper)3 SchemaRegistryClient (com.hortonworks.registries.schemaregistry.client.SchemaRegistryClient)3 InvalidSchemaException (com.hortonworks.registries.schemaregistry.errors.InvalidSchemaException)3 SchemaNotFoundException (com.hortonworks.registries.schemaregistry.errors.SchemaNotFoundException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Response (javax.ws.rs.core.Response)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2