Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8355295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:49:09+00:00 2026-06-09T09:49:09+00:00

Avro serialization is popular with Hadoop users but examples are so hard to find.

  • 0

Avro serialization is popular with Hadoop users but examples are so hard to find.

Can anyone help me with this sample code? I’m mostly interested in using the Reflect API to read/write into files and to use the Union and Null annotations.

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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T09:49:10+00:00Added an answer on June 9, 2026 at 9:49 am

    Here’s a version of the above program that works.

    This also uses compression on the file.

    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();
      }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking at using AVRO on hadoop. But I am concerned with serialization of
I'm using Apache Avro for a project, but I can't seem to make sense
according to Apache AVRO project, Avro is a serialization system. By saying data serialization
I'm running a java code using Apache Avro. Some code gets deprecated in the
I would like to start using Cassandra with a node.js deployment, but I can't
I am interested in generating a C++ header using Apache Avro's code generation tool
I'm trying to use Avro for messages being read from/written to Kafka. Does anyone
I'm attempting to build the C++ Avro API as a DLL so I can
I have been trying to run a Avro map-reduce on oozie. I specify the
I need to convert hierarchical data (AVRO data, which boils down to JSON) into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.