Last active
July 7, 2021 07:35
-
-
Save talfco/cebbe5a2e26aa70fd4894ea55d547dfb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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