public void init(Properties props, String topicName) { super.init(props, topicName); this.props = props; this.topicName = topicName; this.schemaIDField = props.getProperty(CAMUS_SCHEMA_ID_FIELD, DEFAULT_SCHEMA_ID_FIELD); try { SchemaRegistry<Schema> registry = (SchemaRegistry<Schema>) Class.forName( props.getProperty(KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS)).newInstance(); log.info("Prop " + KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS + " is: " + props.getProperty(KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS)); log.info("Underlying schema registry for topic: " + topicName + " is: " + registry); registry.init(props); this.registry = new CachedSchemaRegistry<Schema>(registry, props); this.latestSchema = ((Schema) registry.getLatestSchemaByTopic(topicName).getSchema()); } catch (Exception e) { throw new MessageDecoderException(e); } this.decoderFactory = DecoderFactory.get(); }
@Override public SchemaDetails<S> getLatestSchemaByTopic(String topicName) { File topicDir = getTopicPath(topicName); if (topicDir.exists()) { for (File file : topicDir.listFiles()) { if (file.getName().endsWith(".latest")) { String id = file.getName().replace(".schema.latest", ""); return new SchemaDetails<S>(topicName, id, serde.fromBytes(readBytes(file))); } } } throw new SchemaNotFoundException("Unable to find a latest schema for topic " + topicName + "."); }
@Override public SchemaDetails<S> getLatestSchemaByTopic(String topicName) { MemorySchemaRegistryTuple tuple = latest.get(topicName); if (tuple == null) { throw new SchemaNotFoundException(); } S schema = schemasById.get(tuple); if (schema == null) { throw new SchemaNotFoundException(); } return new SchemaDetails<S>(topicName, Long.toString(tuple.getId()), schema); }
@Override public SchemaDetails<S> getLatestSchemaByTopic(String topicName) { MemorySchemaRegistryTuple tuple = latest.get(topicName); if (tuple == null) { throw new SchemaNotFoundException(); } S schema = schemasById.get(tuple); if (schema == null) { throw new SchemaNotFoundException(); } return new SchemaDetails<S>(topicName, Long.toString(tuple.getId()), schema); }
@Override public void init(Properties props, String topicName) { super.init(props, topicName); try { SchemaRegistry<Schema> registry = (SchemaRegistry<Schema>) Class.forName( props.getProperty(KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS)).newInstance(); log.info("Prop " + KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS + " is: " + props.getProperty(KafkaAvroMessageEncoder.KAFKA_MESSAGE_CODER_SCHEMA_REGISTRY_CLASS)); log.info("Underlying schema registry for topic: " + topicName + " is: " + registry); registry.init(props); this.registry = new CachedSchemaRegistry<Schema>(registry, props); this.latestSchema = registry.getLatestSchemaByTopic(topicName).getSchema(); } catch (Exception e) { throw new MessageDecoderException(e); } decoderFactory = DecoderFactory.get(); }
@Override public SchemaDetails<S> getLatestSchemaByTopic(String topicName) { File topicDir = getTopicPath(topicName); if (topicDir.exists()) { for (File file : topicDir.listFiles()) { if (file.getName().endsWith(".latest")) { String id = file.getName().replace(".schema.latest", ""); return new SchemaDetails<S>(topicName, id, serde.fromBytes(readBytes(file))); } } } throw new SchemaNotFoundException("Unable to find a latest schema for topic " + topicName + "."); }
@Override public CamusWrapper<Record> decode(Message message) { try { GenericDatumReader<Record> reader = new GenericDatumReader<Record>(); Schema schema = super.registry.getLatestSchemaByTopic(super.topicName).getSchema(); reader.setSchema(schema); return new CamusWrapper<Record>(reader.read(null, decoderFactory.jsonDecoder(schema, new String(message.getPayload(), //Message.payloadOffset(message.magic()), kafka.message.Message.MagicOffset(), message.getPayload().length - kafka.message.Message.MagicOffset())))); } catch (Exception e) { throw new RuntimeException(e); } } }
@Override public SchemaDetails<Schema> getLatestSchemaByTopic(String topicName) { Subject subject = client.lookup(topicName); if (subject == null) { throw new SchemaNotFoundException("Schema not found for " + topicName); } SchemaEntry entry = subject.latest(); if (entry == null) throw new SchemaNotFoundException("Schema not found for " + topicName); return new SchemaDetails<Schema>(topicName, entry.getId(), Schema.parse(entry.getSchema())); } }
assertEquals(new SchemaDetails("test", id, getSchema1()), registry.getLatestSchemaByTopic("test")); assertEquals(new SchemaDetails("test", secondId, getSchema2()), registry.getLatestSchemaByTopic("test")); assertEquals(new SchemaDetails("test", secondId, getSchema2()), registry.getLatestSchemaByTopic("test"));