@Override @SuppressWarnings("unchecked") public void configure(Map<String, ?> configs, boolean isKey) { this.isKey = isKey; this.keySchemaVersionIdHeaderName = Utils.getOrDefault(configs, KafkaAvroSerde.KEY_SCHEMA_VERSION_ID_HEADER_NAME, KafkaAvroSerde.DEFAULT_KEY_SCHEMA_VERSION_ID); if (keySchemaVersionIdHeaderName == null || keySchemaVersionIdHeaderName.isEmpty()) { throw new IllegalArgumentException("keySchemaVersionIdHeaderName should not be null or empty"); } this.valueSchemaVersionIdHeaderName = Utils.getOrDefault(configs, KafkaAvroSerde.VALUE_SCHEMA_VERSION_ID_HEADER_NAME, KafkaAvroSerde.DEFAULT_VALUE_SCHEMA_VERSION_ID); if (valueSchemaVersionIdHeaderName == null || valueSchemaVersionIdHeaderName.isEmpty()) { throw new IllegalArgumentException("valueSchemaVersionIdHeaderName should not be null or empty"); } Map<String, Integer> versions = (Map<String, Integer>) ((Map<String, Object>) configs).get(READER_VERSIONS); readerVersions = versions != null ? versions : Collections.emptyMap(); avroSnapshotDeserializer.init(configs); messageAndMetadataAvroDeserializer.init(configs); }
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)); }
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)); }
@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); }
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); } } }
private void _testAvroSerDesGenericObj(Byte protocolId) throws IOException, InvalidSchemaException, IncompatibleSchemaException, SchemaNotFoundException, SchemaBranchNotFoundException { 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); String deviceSchema = AvroSchemaRegistryClientUtil.getSchema("/device.avsc"); SchemaMetadata schemaMetadata = createSchemaMetadata(TEST_NAME_RULE.getMethodName(), SchemaCompatibility.BOTH); SchemaIdVersion v1 = SCHEMA_REGISTRY_CLIENT.addSchemaVersion(schemaMetadata, new SchemaVersion(deviceSchema, "Initial version of the schema")); Assert.assertNotNull(v1); Object deviceObject = AvroSchemaRegistryClientUtil.createGenericRecordForDevice(); byte[] serializedData = avroSnapshotSerializer.serialize(deviceObject, schemaMetadata); Object deserializedObj = avroSnapshotDeserializer.deserialize(new ByteArrayInputStream(serializedData), null); Assert.assertEquals(deviceObject, deserializedObj); }
deserializer.init(Collections.emptyMap());