【问题标题】:Create Avro string from Java object从 Java 对象创建 Avro 字符串
【发布时间】:2019-02-09 21:01:00
【问题描述】:

我正在构建一个 MapR-ES Java 生产者,它通过 JDBC 连接到 Oracle 并获取返回的结果集以发布到流中。

我想将我填充的类对象序列化为一个 Avro 字符串作为我的发布者的消息。

我使用 Maven Apache Avro 插件为我的类对象生成了一个 Avro 字符串

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

但是,如果我有一个完全填充的 MyClass 对象,我如何生成一个带有架构和填充数据的 Avro 字符串?

我还没有找到任何好的例子。任何帮助表示赞赏。

【问题讨论】:

    标签: java serialization stream avro


    【解决方案1】:

    假设我在 java 中有 ReflectedCustomer 类。

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

    以下代码将使用上面的 ReflectedCustomer 并生成 Avro 字符串并将其读回。

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

    【讨论】:

      猜你喜欢
      • 2014-11-22
      • 1970-01-01
      • 2015-09-08
      • 1970-01-01
      • 2011-02-26
      • 2012-05-22
      • 1970-01-01
      • 2015-08-19
      相关资源
      最近更新 更多