Skip to content

Instantly share code, notes, and snippets.

@talfco
Last active July 7, 2021 07:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talfco/cebbe5a2e26aa70fd4894ea55d547dfb to your computer and use it in GitHub Desktop.
Save talfco/cebbe5a2e26aa70fd4894ea55d547dfb to your computer and use it in GitHub Desktop.
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
Copy link

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