Search code examples
javaserializationstreamavro

Create Avro string from Java object


I am building a MapR-ES Java producer, that connects to Oracle via JDBC and gets a result set back to be published into a stream.

I want to serialize my populated class object into an Avro string to be the message of my publisher.

I have used the Maven Apache Avro plugin to generate an Avro string for my class object with

Schema schema = ReflectionData.get().getSchema(MyClass.class);

But, if i have a fully populated MyClass Object, how to i generate an Avro string with the schema and populated data?

I havent found any good examples on this. Any help is appreciated.


Solution

  • Let's say if i have ReflectedCustomer class in java.

    import org.apache.avro.reflect.Nullable;
    
    public class ReflectedCustomer {
    
    private String firstName;
    private String lastName;
    @Nullable private String nickName;
    
    // needed by the reflection
    public ReflectedCustomer(){}
    
    public ReflectedCustomer(String firstName, String lastName, String nickName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.nickName = nickName;
    }
    
    public String getFirstName() {
        return firstName;
    }
    
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    
    public String fullName(){
        return this.firstName + " " + this.lastName + " " + this.nickName;
    }
    
    public String getNickName() {
        return nickName;
    }
    
    public void setNickName(String nickName) {
        this.nickName = nickName;
    }
    }
    

    Below code will use the above ReflectedCustomer and generate the Avro String and it will read it back.

    import org.apache.avro.Schema;
    import org.apache.avro.file.CodecFactory;
    import org.apache.avro.file.DataFileReader;
    import org.apache.avro.file.DataFileWriter;
    import org.apache.avro.io.DatumReader;
    import org.apache.avro.io.DatumWriter;
    import org.apache.avro.reflect.ReflectData;
    import org.apache.avro.reflect.ReflectDatumReader;
    import org.apache.avro.reflect.ReflectDatumWriter;
    import java.io.File;
    import java.io.IOException;
    
    public class ReflectionExamples {
    
        public static void main(String[] args) {
    
            // here we use reflection to determine the schema
            Schema schema = ReflectData.get().getSchema(ReflectedCustomer.class);
            System.out.println("schema = " + schema.toString(true));
    
    
            // create a file of ReflectedCustomers
            try {
                System.out.println("Writing customer-reflected.avro");
                File file = new File("customer-reflected.avro");
                DatumWriter<ReflectedCustomer> writer = new ReflectDatumWriter<>(ReflectedCustomer.class);
                DataFileWriter<ReflectedCustomer> out = new DataFileWriter<>(writer)
                        .setCodec(CodecFactory.deflateCodec(9))
                        .create(schema, file);
    
                out.append(new ReflectedCustomer("Bill", "Clark", "The Rocket"));
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            // read from an avro into our Reflected class
            // open a file of ReflectedCustomers
            try {
                System.out.println("Reading customer-reflected.avro");
                File file = new File("customer-reflected.avro");
                DatumReader<ReflectedCustomer> reader = new ReflectDatumReader<>(ReflectedCustomer.class);
                DataFileReader<ReflectedCustomer> in = new DataFileReader<>(file, reader);
    
                // read ReflectedCustomers from the file & print them as JSON
                for (ReflectedCustomer reflectedCustomer : in) {
                    System.out.println(reflectedCustomer.fullName());
                }
                // close the input file
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
    
    
        }
    }