nosql:avro_schema_definieren
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
Beide Seiten der vorigen RevisionVorhergehende ÜberarbeitungNächste Überarbeitung | Vorhergehende Überarbeitung | ||
nosql:avro_schema_definieren [2013/08/17 17:28] – [Schema Definition mit Avro in der Oracle NoSQL Datenbank] gpipperr | nosql:avro_schema_definieren [2013/08/17 17:35] (aktuell) – gpipperr | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | =====Schema Definition mit Avro in der Oracle NoSQL Datenbank===== | ||
+ | |||
+ | ===Vorbereitung: | ||
+ | |||
+ | Java Libraries : | ||
+ | |||
+ | * Avro Java Libraries laden von http:// | ||
+ | * avro-1.7.4.jar | ||
+ | * avro-tools-1.7.4.ja | ||
+ | * Jackson JSON library laden: http:// | ||
+ | * jackson-annotations-2.2.0.jar | ||
+ | * jackson-core-2.2.0.jar | ||
+ | * jackson-databind-2.2.0.jar | ||
+ | |||
+ | Libraries in eine Verzeichnis kopieren, in unsere Beispiel unter D: | ||
+ | |||
+ | |||
+ | === Ein Avro Schema definieren === | ||
+ | |||
+ | Datei emp.avsc mit der Schema Definition anlegen: | ||
+ | |||
+ | <code emp.avsc> | ||
+ | {" | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | | ||
+ | {" | ||
+ | {" | ||
+ | {" | ||
+ | {" | ||
+ | {" | ||
+ | {" | ||
+ | | ||
+ | ] | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Schema Definition siehe hier: [[http:// | ||
+ | |||
+ | Das erste Problem, dass ins Auge fällt, ist die fehlende native Unterstützung für den Datentyp " | ||
+ | |||
+ | Wie in der Java Welt üblich, ist hier wie immer etwas Basteln angesagt, oder eben das Speichern des Datumswerts als Long Value. | ||
+ | |||
+ | Damit das Schema in der Datenbank später auch angepasst/ | ||
+ | |||
+ | === Schema prüfen und übersetzen === | ||
+ | |||
+ | Mit den Avro Tool kann die Schema Definition überprüft und übersetzt werden. | ||
+ | |||
+ | <code powershell> | ||
+ | java -jar D: | ||
+ | </ | ||
+ | |||
+ | Damit stehen die passende Java Klassen für die Schema Definition für die Serialisierung zur Verfügung. | ||
+ | |||
+ | === Schema in der NoSQL Datenbank anlegen === | ||
+ | |||
+ | Über die Admin Konsole wird das Schema in der Datenbank angelegt: | ||
+ | |||
+ | <code powershell> | ||
+ | REM Admin Console starten: | ||
+ | java -jar D: | ||
+ | |||
+ | REM Schema in der DB anlegen: | ||
+ | |||
+ | kv-> ddl add-schema -file emp.avsc | ||
+ | Added schema: gpi.avro.emp.1 | ||
+ | |||
+ | REM Schemas in der DB anzeigen lassen: | ||
+ | |||
+ | kv-> show schemas | ||
+ | gpi.avro.emp | ||
+ | ID: 1 Modified: 2013-08-17 14:14:41 UTC, From: jupiter | ||
+ | | ||
+ | </ | ||
+ | |||
+ | === Wie kann das Avro Schema in meiner Applikation genützt werden? === | ||
+ | |||
+ | Ein Avro Schema | ||
+ | |||
+ | |||
+ | * Generic | ||
+ | * Specific | ||
+ | * JSON | ||
+ | * Raw | ||
+ | |||
+ | Für die Details siehe [[http:// | ||
+ | |||
+ | |||
+ | === Ein erster Versuch mit einem Generic Binding === | ||
+ | |||
+ | Ein einfaches Beispiel um mit Hilfe der Avro Schema Definition generisch in die DB zu schreiben: | ||
+ | |||
+ | (siehe auch [[http:// | ||
+ | |||
+ | <code java> | ||
+ | package com.gpi.oranosql; | ||
+ | |||
+ | import java.io.File; | ||
+ | import java.io.IOException; | ||
+ | |||
+ | import java.util.Date; | ||
+ | |||
+ | import oracle.kv.Consistency; | ||
+ | import oracle.kv.KVStore; | ||
+ | import oracle.kv.KVStoreConfig; | ||
+ | import oracle.kv.KVStoreFactory; | ||
+ | import oracle.kv.Key; | ||
+ | |||
+ | import org.apache.avro.generic.GenericData; | ||
+ | import org.apache.avro.generic.GenericRecord; | ||
+ | import org.apache.avro.Schema; | ||
+ | |||
+ | import oracle.kv.avro.AvroCatalog; | ||
+ | import oracle.kv.avro.GenericAvroBinding; | ||
+ | |||
+ | public class AvroReadWrite { | ||
+ | |||
+ | // KV Store | ||
+ | public static KVStore kvstore = null; | ||
+ | |||
+ | public AvroReadWrite() { | ||
+ | } | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | AvroReadWrite avroReadWrite = new AvroReadWrite(); | ||
+ | |||
+ | // get the host to connect as first parameter | ||
+ | String host = args[0]; | ||
+ | // get the store Name | ||
+ | String store = args[1]; | ||
+ | |||
+ | KVStoreConfig kvconfig = new KVStoreConfig(store, | ||
+ | // Connect to the store | ||
+ | kvstore = KVStoreFactory.getStore(kvconfig); | ||
+ | |||
+ | |||
+ | // Read the Avro Schema | ||
+ | Schema.Parser parser = null; | ||
+ | try { | ||
+ | parser = new Schema.Parser(); | ||
+ | // read our avro schema definition | ||
+ | parser.parse(new File(" | ||
+ | } | ||
+ | catch (IOException ex) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | // parse the schema definition | ||
+ | // !!! Name of the Schema is :: namespace + name !!! | ||
+ | final Schema empSchema = parser.getTypes().get(" | ||
+ | |||
+ | // connect to store and read Avro schema defintion in the database | ||
+ | AvroCatalog catalog = kvstore.getAvroCatalog(); | ||
+ | //debug | ||
+ | // | ||
+ | | ||
+ | // Generic Binding | ||
+ | GenericAvroBinding binding = catalog.getGenericBinding(empSchema); | ||
+ | |||
+ | // Define the emp record | ||
+ | GenericRecord emp = new GenericData.Record(empSchema); | ||
+ | | ||
+ | // put values in the record: | ||
+ | emp.put(" | ||
+ | emp.put(" | ||
+ | emp.put(" | ||
+ | emp.put(" | ||
+ | emp.put(" | ||
+ | emp.put(" | ||
+ | emp.put(" | ||
+ | emp.put(" | ||
+ | |||
+ | // Create the Key wiht minor und mayor component | ||
+ | Key key = Key.createKey(" | ||
+ | |||
+ | // put the emp record into the store | ||
+ | kvstore.put(key, | ||
+ | |||
+ | //close the store | ||
+ | kvstore.close(); | ||
+ | |||
+ | System.exit(0); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | Mit dem Client Tool kvcli testen: | ||
+ | |||
+ | <code powershell> | ||
+ | java -jar D: | ||
+ | kvshell-> | ||
+ | / | ||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | |||
+ | Werte im Programm auslesen: | ||
+ | <code java> | ||
+ | |||
+ | // see code above for the connection to the database | ||
+ | |||
+ | | ||
+ | Key key2 = Key.createKey(" | ||
+ | | ||
+ | |||
+ | if (vv != null) { | ||
+ | | ||
+ | emp2 = binding.toObject(vv.getValue()); | ||
+ | | ||
+ | String empno = emp2.get(" | ||
+ | String ename = emp2.get(" | ||
+ | String job = emp2.get(" | ||
+ | | ||
+ | int mgr = (Integer)emp2.get(" | ||
+ | | ||
+ | long time = (Long)emp2.get(" | ||
+ | Date hiredate = new Date(); | ||
+ | hiredate.setTime(time); | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==== Quellen ==== | ||
+ | |||
+ | Dokumentation: | ||
+ | |||
+ | * http:// | ||
+ | |||
+ | |||
+ | Allgemein: | ||
+ | * http:// | ||
nosql/avro_schema_definieren.txt · Zuletzt geändert: 2013/08/17 17:35 von gpipperr