2012-08-08 14 views
14

एवरो सीरियलाइजेशन हैडोप उपयोगकर्ताओं के साथ लोकप्रिय है लेकिन उदाहरण ढूंढना बहुत मुश्किल है।अपाचे एवरो का उपयोग

क्या कोई मुझे इस नमूना कोड के साथ मदद कर सकता है? मैं ज्यादातर फ़ाइलों में पढ़ने/लिखने और संघ और नल एनोटेशन का उपयोग करने के लिए प्रतिबिंब एपीआई का उपयोग करने में रूचि रखता हूं।

public class Reflect { 

    public class Packet { 
     int cost; 
     @Nullable TimeStamp stamp; 
     public Packet(int cost, TimeStamp stamp){ 
      this.cost = cost; 
      this.stamp = stamp; 
     } 
    } 

    public class TimeStamp { 
     int hour = 0; 
     int second = 0; 
     public TimeStamp(int hour, int second){ 
      this.hour = hour; 
      this.second = second; 
     } 
    } 

    public static void main(String[] args) throws IOException { 
     TimeStamp stamp; 
     Packet packet; 

     stamp = new TimeStamp(12, 34); 
     packet = new Packet(9, stamp); 
     write(file, packet); 

     packet = new Packet(8, null); 
     write(file, packet); 
     file.close(); 

     // open file to read. 
     packet = read(file); 
     packet = read(file); 
    } 
} 

उत्तर

27

यहां उपर्युक्त प्रोग्राम का एक संस्करण है जो काम करता है।

यह फ़ाइल पर संपीड़न का भी उपयोग करता है।

import java.io.File; 
import org.apache.avro.Schema; 
import org.apache.avro.file.DataFileWriter; 
import org.apache.avro.file.DataFileReader; 
import org.apache.avro.file.CodecFactory; 
import org.apache.avro.io.DatumWriter; 
import org.apache.avro.io.DatumReader; 
import org.apache.avro.reflect.ReflectData; 
import org.apache.avro.reflect.ReflectDatumWriter; 
import org.apache.avro.reflect.ReflectDatumReader; 
import org.apache.avro.reflect.Nullable; 

public class Reflect { 

    public static class Packet { 
    int cost; 
    @Nullable TimeStamp stamp; 
    public Packet() {}      // required to read 
    public Packet(int cost, TimeStamp stamp){ 
     this.cost = cost; 
     this.stamp = stamp; 
    } 
    } 

    public static class TimeStamp { 
    int hour = 0; 
    int second = 0; 
    public TimeStamp() {}      // required to read 
    public TimeStamp(int hour, int second){ 
     this.hour = hour; 
     this.second = second; 
    } 
    } 

    public static void main(String[] args) throws Exception { 
    // one argument: a file name 
    File file = new File(args[0]); 

    // get the reflected schema for packets 
    Schema schema = ReflectData.get().getSchema(Packet.class); 

    // create a file of packets 
    DatumWriter<Packet> writer = new ReflectDatumWriter<Packet>(Packet.class); 
    DataFileWriter<Packet> out = new DataFileWriter<Packet>(writer) 
     .setCodec(CodecFactory.deflateCodec(9)) 
     .create(schema, file); 

    // write 100 packets to the file, odds with null timestamp 
    for (int i = 0; i < 100; i++) { 
     out.append(new Packet(i, (i%2==0) ? new TimeStamp(12, i) : null)); 
    } 

    // close the output file 
    out.close(); 

    // open a file of packets 
    DatumReader<Packet> reader = new ReflectDatumReader<Packet>(Packet.class); 
    DataFileReader<Packet> in = new DataFileReader<Packet>(file, reader); 

    // read 100 packets from the file & print them as JSON 
    for (Packet packet : in) { 
     System.out.println(ReflectData.get().toString(packet)); 
    } 

    // close the input file 
    in.close(); 
    } 

} 
-1

https://sites.google.com/site/developertips/Home/java/apache-avro पर उदाहरण के लिए 3 देखें यह दिखाता है कि आप लिख सकते हैं और जावा वर्गों को पढ़ने के लिए प्रतिबिंब एपीआई का उपयोग कर सकते हैं।

+1

उसको देखा गया। यह एक धारा को लिखने के लिए है। इसलिए, अगर किसी फ़ाइल में लिखा गया है, तो मुझे संदेह है कि इसमें हेडर नहीं होगा। यदि ऐसा है, तो शायद यह एक अलग प्रोग्रामिंग lagnguage में व्याख्या करना संभव नहीं होगा। इसके अलावा, उदाहरण निरर्थक फ़ील्ड या यूनियनों के साथ अधिक जटिल डेटास्ट्रक्चर के उपयोग को संबोधित नहीं करता है। – fodon

+0

मैं @fodon से सहमत हूं। मैं अधिक वर्बोज़ उदाहरणों को देखने में सक्षम होना चाहता हूं क्योंकि मुझे लगता है कि कुछ बग मौजूद हैं कि परीक्षणों के लिए लेखांकन नहीं है। – Dan

+1

@LordAragon लिंक अब और काम नहीं करता है। संदर्भ से भी वास्तविक सामग्री साझा करने का यह एक और कारण है। – Sankalp

संबंधित मुद्दे