/** * De-serializes the given {@code input} according to the writer schema {@code WS}. * * @param input input payload to be de-serialized * @return O the de-serialized output * @throws SerDesException */ default O deserialize(I input) throws SerDesException { return deserialize(input, null); }
private Long getCacheExpiryInSecs(Map<String, ?> config) { Long value = (Long) getValue(config, DESERIALIZER_SCHEMA_CACHE_EXPIRY_IN_SECS, DEFAULT_DESERIALIZER_SCHEMA_CACHE_EXPIRY_IN_SECS); if (value < 0) { throw new IllegalArgumentException("Property: " + DESERIALIZER_SCHEMA_CACHE_EXPIRY_IN_SECS + " must be non negative."); } return value; }
@Override public void doInit(Map<String, ?> config) { super.doInit(config); SchemaVersionRetriever schemaVersionRetriever = createSchemaVersionRetriever(); avroSchemaResolver = new AvroSchemaResolver(schemaVersionRetriever); useSpecificAvroReader = (boolean) getValue(config, SPECIFIC_AVRO_READER, false); }
@Override public <T> T getDefaultSerializer(String type) throws SerDesException { Collection<SchemaProviderInfo> supportedSchemaProviders = getSupportedSchemaProviders(); for (SchemaProviderInfo schemaProvider : supportedSchemaProviders) { if (schemaProvider.getType().equals(type)) { try { return (T) Class.forName(schemaProvider.getDefaultSerializerClassName()).newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new SerDesException(e); } } } throw new IllegalArgumentException("No schema provider registered for the given type " + type); }
@Override public O deserialize(I input, Integer readerSchemaVersion) throws SerDesException { ensureInitialized(); // it can be enhanced to have respective protocol handlers for different versions byte protocolId = retrieveProtocolId(input); SchemaIdVersion schemaIdVersion = retrieveSchemaIdVersion(protocolId, input); SchemaVersionInfo schemaVersionInfo; SchemaMetadata schemaMetadata; try { schemaVersionInfo = schemaRegistryClient.getSchemaVersionInfo(schemaIdVersion); schemaMetadata = schemaRegistryClient.getSchemaMetadataInfo(schemaVersionInfo.getName()).getSchemaMetadata(); } catch (Exception e) { throw new RegistryException(e); } return doDeserialize(input, protocolId, schemaMetadata, schemaVersionInfo.getVersion(), readerSchemaVersion); }
@Override public final O serialize(I input, SchemaMetadata schemaMetadata) throws SerDesException { ensureInitialized(); // compute schema based on input object String schema = getSchemaText(input); // register that schema and get the version try { SchemaIdVersion schemaIdVersion = schemaRegistryClient.addSchemaVersion(schemaMetadata, new SchemaVersion(schema, "Schema registered by serializer:" + this.getClass())); // write the version and given object to the output return doSerialize(input, schemaIdVersion); } catch (SchemaNotFoundException | IncompatibleSchemaException | InvalidSchemaException | SchemaBranchNotFoundException e) { throw new RegistryException(e); } }
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)); }
@Override protected void doInit(Map<String, ?> config) { schemaCache = CacheBuilder.newBuilder() .maximumSize(getCacheMaxSize(config)) .expireAfterAccess(getCacheExpiryInSecs(config), TimeUnit.SECONDS) .build(new CacheLoader<SchemaVersionKey, S>() { @Override public S load(SchemaVersionKey schemaVersionKey) { try { return getParsedSchema(schemaVersionKey); } catch (SchemaNotFoundException | InvalidSchemaException e) { throw new RegistryException(e); } } }); }
@Override public S load(SchemaVersionKey schemaVersionKey) { try { return getParsedSchema(schemaVersionKey); } catch (SchemaNotFoundException | InvalidSchemaException e) { throw new RegistryException(e); } } });
public final void init(Map<String, ?> config) { if (closed) { throw new IllegalStateException("Closed instance can not be initialized again"); } if (initialized) { LOG.info("This instance [{}] is already inited", this); return; } LOG.debug("Initialized with config: [{}]", config); if (schemaRegistryClient == null) { schemaRegistryClient = new SchemaRegistryClient(config); } doInit(config); initialized = true; }
@Override public <T> T getDefaultDeserializer(String type) throws SerDesException { Collection<SchemaProviderInfo> supportedSchemaProviders = getSupportedSchemaProviders(); for (SchemaProviderInfo schemaProvider : supportedSchemaProviders) { if (schemaProvider.getType().equals(type)) { try { return (T) Class.forName(schemaProvider.getDefaultDeserializerClassName()).newInstance(); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { throw new SerDesException(e); } } } throw new IllegalArgumentException("No schema provider registered for the given type " + type); }
/** * De-serializes the given {@code input} according to the writer schema {@code WS}. * * @param input input payload to be de-serialized * @return O the de-serialized output * @throws SerDesException */ default O deserialize(I input) throws SerDesException { return deserialize(input, null); }
private Integer getCacheMaxSize(Map<String, ?> config) { Integer value = (Integer) getValue(config, DESERIALIZER_SCHEMA_CACHE_MAX_SIZE, DEFAULT_SCHEMA_CACHE_SIZE); if (value < 0) { throw new IllegalArgumentException("Property: " + DESERIALIZER_SCHEMA_CACHE_MAX_SIZE + " must be non negative."); } return value; }
@Override public void mapSchemaWithSerDes(String schemaName, Long serDesId) { SerDesInfo serDesInfo = getSerDes(serDesId); if (serDesInfo == null) { throw new SerDesException("Serializer with given ID " + serDesId + " does not exist"); } SchemaMetadataInfo schemaMetadataInfo = getSchemaMetadataInfo(schemaName); SchemaSerDesMapping schemaSerDesMapping = new SchemaSerDesMapping(schemaMetadataInfo.getId(), serDesId); storageManager.add(schemaSerDesMapping); }
new ClassLoaderAwareInvocationHandler(classLoader, t)); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new SerDesException(e);
@Override protected MessageContext doSerialize(Object input, SchemaIdVersion schemaIdVersion) throws SerDesException { Map<String, Object> headers = new HashMap<>(); headers.put("protocol.id", getProtocolId()); headers.put("schema.metadata.id", schemaIdVersion.getSchemaMetadataId()); headers.put("schema.version", schemaIdVersion.getVersion()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(baos)) { serializePayload(bufferedOutputStream, input); } catch (IOException e) { throw new SerDesException(e); } ByteArrayInputStream payload = new ByteArrayInputStream(baos.toByteArray()); return new MessageContext(headers, payload); } }