Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package net.cloudburo.avro.registry;
import com.google.gson.JsonObject;
import org.apache.avro.Schema;
import net.cloudburo.elasticsearch.ESPersistencyManager;
public class ElasticSearchSchemaRegistry extends SchemaRegistry {
private static final String esIndex = "avroschema";
ESPersistencyManager esManager;
public void setESPersistencyManager(ESPersistencyManager esManager) {
this.esManager = esManager;
}
public long registerSchema(Schema schema) throws IOException {
long fingerprint = getSchemaFingerprint(schema);
String avroJSONSchema = schema.toString(true);
String doc = "{";
doc+= "\"namespace\":"+"\""+schema.getNamespace()+"."+schema.getName()+"\",";
doc+= "\"avroschema\":"+avroJSONSchema;
doc+= "}";
esManager.createUpdateDocument(esIndex,"schema",doc,Long.valueOf(fingerprint).toString());
return fingerprint;
}
public Schema getSchema(long fingerprint) throws IOException {
JsonObject jsonDoc = esManager.readDocumentByIdAsObject(esIndex, Long.valueOf(fingerprint).toString());
String json = jsonDoc.get("_source").getAsJsonObject().get("avroschema").toString();
Schema schema = new Schema.Parser().parse(json);
return schema;
}
}
@elghali

This comment has been minimized.

Copy link

@elghali elghali commented Jul 7, 2021

Hello, is this the only way to get the Schema JSON from the fingerprint long? I had the impression that the fingerprint is the encoded value of a schema JSON, and to get back the JSON me must decode it somehow. could you shed some light please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment