Search in sources :

Example 1 with AvroSnapshotDeserializer

use of com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer in project registry by hortonworks.

the class AvroSerDesInitClosureTest method testDeserWihtoutInit.

@Test(expected = IllegalStateException.class)
public void testDeserWihtoutInit() throws Exception {
    AvroSnapshotDeserializer deserializer = new AvroSnapshotDeserializer();
    deserializer.deserialize(new ByteArrayInputStream(new byte[] {}), 1);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) Test(org.junit.Test)

Example 2 with AvroSnapshotDeserializer

use of com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer in project registry by hortonworks.

the class SampleSchemaRegistryClientApp method runAvroSerDesApis.

public void runAvroSerDesApis() throws IOException {
    // using builtin avro serializer/deserializer
    AvroSnapshotSerializer avroSnapshotSerializer = new AvroSnapshotSerializer();
    avroSnapshotSerializer.init(config);
    AvroSnapshotDeserializer avroSnapshotDeserializer = new AvroSnapshotDeserializer();
    avroSnapshotDeserializer.init(config);
    Object deviceObject = createGenericRecordForDevice("/device.avsc");
    SchemaMetadata schemaMetadata = createSchemaMetadata("avro-serializer-schema-" + System.currentTimeMillis());
    byte[] serializedData = avroSnapshotSerializer.serialize(deviceObject, schemaMetadata);
    Object deserializedObj = avroSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedData), null);
    LOG.info("Serialized and deserialized objects are equal: [{}] ", deviceObject.equals(deserializedObj));
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) AvroSnapshotSerializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer)

Example 3 with AvroSnapshotDeserializer

use of com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer in project registry by hortonworks.

the class SampleSchemaRegistryClientApp method runDefaultSerDesApi.

public void runDefaultSerDesApi() throws Exception {
    String type = AvroSchemaProvider.TYPE;
    AvroSnapshotSerializer serializer = schemaRegistryClient.getDefaultSerializer(type);
    serializer.init(config);
    AvroSnapshotDeserializer deserializer = schemaRegistryClient.getDefaultDeserializer(type);
    deserializer.init(config);
    Object deviceObject = createGenericRecordForDevice("/device.avsc");
    SchemaMetadata schemaMetadata = createSchemaMetadata("avro-serializer-schema-" + System.currentTimeMillis());
    byte[] serializedData = serializer.serialize(deviceObject, schemaMetadata);
    Object deserializedObj = deserializer.deserialize(new ByteArrayInputStream(serializedData), null);
    LOG.info("Serialized and deserialized objects are equal: [{}] ", deviceObject.equals(deserializedObj));
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) AvroSnapshotSerializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer)

Example 4 with AvroSnapshotDeserializer

use of com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer in project registry by hortonworks.

the class AvroSchemaRegistryClientTest method _testAvroSerDesPrimitives.

private void _testAvroSerDesPrimitives(Byte protocolId) {
    Map<String, Object> config = Maps.newHashMap();
    config.putAll(SCHEMA_REGISTRY_CLIENT_CONF);
    config.put(SERDES_PROTOCOL_VERSION, protocolId);
    AvroSnapshotSerializer avroSnapshotSerializer = new AvroSnapshotSerializer();
    avroSnapshotSerializer.init(config);
    AvroSnapshotDeserializer avroSnapshotDeserializer = new AvroSnapshotDeserializer();
    avroSnapshotDeserializer.init(config);
    Object[] objects = AvroSchemaRegistryClientUtil.generatePrimitivePayloads();
    for (Object obj : objects) {
        String name = obj != null ? obj.getClass().getName() : Void.TYPE.getName();
        SchemaMetadata schemaMetadata = createSchemaMetadata(name, SchemaCompatibility.BOTH);
        byte[] serializedData = avroSnapshotSerializer.serialize(obj, schemaMetadata);
        Object deserializedObj = avroSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedData), null);
        if (obj instanceof byte[]) {
            Assert.assertArrayEquals((byte[]) obj, (byte[]) deserializedObj);
        } else if (deserializedObj instanceof Utf8) {
            Assert.assertEquals(obj, deserializedObj.toString());
        } else {
            Assert.assertEquals(obj, deserializedObj);
        }
    }
}
Also used : SchemaMetadata(com.hortonworks.registries.schemaregistry.SchemaMetadata) ByteArrayInputStream(java.io.ByteArrayInputStream) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) Utf8(org.apache.avro.util.Utf8) AvroSnapshotSerializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer)

Example 5 with AvroSnapshotDeserializer

use of com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer in project registry by hortonworks.

the class AvroSerDesInitClosureTest method testDeserClosedSer.

@Test(expected = IllegalStateException.class)
public void testDeserClosedSer() throws Exception {
    AvroSnapshotDeserializer deserializer = new AvroSnapshotDeserializer();
    deserializer.init(Collections.emptyMap());
    deserializer.close();
    deserializer.deserialize(new ByteArrayInputStream(new byte[] {}), 1);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) AvroSnapshotDeserializer(com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer) Test(org.junit.Test)

Aggregations

AvroSnapshotDeserializer (com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotDeserializer)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)5 AvroSnapshotSerializer (com.hortonworks.registries.schemaregistry.serdes.avro.AvroSnapshotSerializer)5 Test (org.junit.Test)3 SchemaIdVersion (com.hortonworks.registries.schemaregistry.SchemaIdVersion)2 SchemaVersion (com.hortonworks.registries.schemaregistry.SchemaVersion)2 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)1 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)1 SchemaVersionKey (com.hortonworks.registries.schemaregistry.SchemaVersionKey)1 Device (com.hortonworks.registries.serdes.Device)1 Expectations (mockit.Expectations)1 Utf8 (org.apache.avro.util.Utf8)1