2012-08-08 30 views
14

Tuần tự hóa Avro phổ biến với người dùng Hadoop nhưng các ví dụ rất khó tìm.Sử dụng apache avro phản ánh

Có ai có thể giúp tôi với mã mẫu này không? Tôi chủ yếu quan tâm đến việc sử dụng API phản chiếu để đọc/ghi vào các tệp và sử dụng chú thích Liên kết và Null.

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); 
    } 
} 

Trả lời

27

Đây là phiên bản của chương trình trên hoạt động.

Điều này cũng sử dụng nén trên tệp.

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

Xem ví dụ 3 tại https://sites.google.com/site/developertips/Home/java/apache-avro Nó cho thấy cách bạn có thể sử dụng API phản chiếu để viết và đọc các lớp Java.

+1

Thấy điều đó. Điều đó có nghĩa là để ghi vào luồng. Vì vậy, nếu được ghi vào một tập tin, tôi nghi ngờ nó sẽ không chứa tiêu đề. Nếu có, nó có thể sẽ không thể diễn giải nó trong một lagnguage lập trình khác nhau. Ngoài ra, ví dụ này không giải quyết việc sử dụng các datastructures phức tạp hơn với các trường hoặc các công đoàn không có giá trị. – fodon

+0

Tôi đồng ý với @fodon. Tôi muốn có thể thấy nhiều ví dụ tiết hơn như tôi tin rằng một số lỗi tồn tại mà các thử nghiệm không được tính toán. – Dan

+1

@LordAragon liên kết không hoạt động nữa. Đây là một lý do khác để thực hành tốt để chia sẻ nội dung thực tế cũng từ tham chiếu. – Sankalp

Các vấn đề liên quan