Java読み書きavro例


一、avroはデータのシーケンス化のフレームワークで、効率的にシーケンス化と逆シーケンス化を行うことができて、C、C++、C#、Java、PHP、Python、Ruby言語をサポートします.Javaを使用して読み書きを行います.
二、環境構築
1、avro-1.7.7をダウンロードする.jar and avro-tools-1.7.7.jar 2つのjarパッケージを指定したファイルディレクトリに配置します.ダウンロードアドレスhttp://www.trieuvan.com/apache/avro/avro-1.7.7/java/
D:softavroフォルダに入れて、ディレクトリを変更してjavaフォルダを新規作成して、生成したJavaコードを保存します.
2、このディレクトリの下に新しいuser.AVscファイル、内容は:
{"namespace": "example.avro",
 "type": "record",
 "name": "User",
 "fields": [
     {
      "name": "name", "type": "string"},
     {
      "name": "favorite_number",  "type": ["int", "null"]},
     {
      "name": "favorite_color", "type": ["string", "null"]}
 ]
}   

3、cmdを開き、このディレクトリに入り、コマンドを実行してUserクラスを生成し、コマンドの後ろに「.」があることに注意する.生成されたコードがこのディレクトリの下に置かれていることを示します.
java -jar avro-tools-1.7.7.jar compile schema user.avsc java .

  
このフォルダの下にあるJavaファイルの下にある.../Excample/avro/ディレクトリの下にUserが生成されます.JAvaファイル.
  4.eclipseを使用してmavenプロジェクトを新規作成しpom.xmlはavroの依存を加える.  

  org.apache.avro
  avro
  1.7.7

三、生成したUser.JAvaファイルの内容は以下の通りです.
生成されたUserをJAvaクラスはプロジェクトにコピーされます.このUserに注意してください.JAvaで生成されたUserクラスとその内部クラスのパッケージ名はデフォルトでuser.AVscファイルのnamespaceの値、
この例ではexample.avro.すべて自分のパッケージ名に置き換える必要があります.
最も簡単な方法はUserをJAvaのexample.avroはすべて自分のバッグ名に置き換えます.
/**
 * Autogenerated by Avro
 * 
 * DO NOT EDIT DIRECTLY
 */
package example.avro;  
@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public class User extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
  public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"User\",\"namespace\":\"example.avro\",\"fields\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"favorite_number\",\"type\":[\"int\",\"null\"]},{\"name\":\"favorite_color\",\"type\":[\"string\",\"null\"]}]}");
  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
  @Deprecated public java.lang.CharSequence name;
  @Deprecated public java.lang.Integer favorite_number;
  @Deprecated public java.lang.CharSequence favorite_color;

  /**
   * Default constructor.  Note that this does not initialize fields
   * to their default values from the schema.  If that is desired then
   * one should use newBuilder(). 
   */
  public User() {}

  /**
   * All-args constructor.
   */
  public User(java.lang.CharSequence name, java.lang.Integer favorite_number, java.lang.CharSequence favorite_color) {
    this.name = name;
    this.favorite_number = favorite_number;
    this.favorite_color = favorite_color;
  }

  public org.apache.avro.Schema getSchema() { return SCHEMA$; }
  // Used by DatumWriter.  Applications should not call. 
  public java.lang.Object get(int field$) {
    switch (field$) {
    case 0: return name;
    case 1: return favorite_number;
    case 2: return favorite_color;
    default: throw new org.apache.avro.AvroRuntimeException("Bad index");
    }
  }
  // Used by DatumReader.  Applications should not call. 
  @SuppressWarnings(value="unchecked")
  public void put(int field$, java.lang.Object value$) {
    switch (field$) {
    case 0: name = (java.lang.CharSequence)value$; break;
    case 1: favorite_number = (java.lang.Integer)value$; break;
    case 2: favorite_color = (java.lang.CharSequence)value$; break;
    default: throw new org.apache.avro.AvroRuntimeException("Bad index");
    }
  }

  /**
   * Gets the value of the 'name' field.
   */
  public java.lang.CharSequence getName() {
    return name;
  }

  /**
   * Sets the value of the 'name' field.
   * @param value the value to set.
   */
  public void setName(java.lang.CharSequence value) {
    this.name = value;
  }

  /**
   * Gets the value of the 'favorite_number' field.
   */
  public java.lang.Integer getFavoriteNumber() {
    return favorite_number;
  }

  /**
   * Sets the value of the 'favorite_number' field.
   * @param value the value to set.
   */
  public void setFavoriteNumber(java.lang.Integer value) {
    this.favorite_number = value;
  }

  /**
   * Gets the value of the 'favorite_color' field.
   */
  public java.lang.CharSequence getFavoriteColor() {
    return favorite_color;
  }

  /**
   * Sets the value of the 'favorite_color' field.
   * @param value the value to set.
   */
  public void setFavoriteColor(java.lang.CharSequence value) {
    this.favorite_color = value;
  }

  /** Creates a new User RecordBuilder */
  public static example.avro.User.Builder newBuilder() {
    return new example.avro.User.Builder();
  }
  
  /** Creates a new User RecordBuilder by copying an existing Builder */
  public static example.avro.User.Builder newBuilder(example.avro.User.Builder other) {
    return new example.avro.User.Builder(other);
  }
  
  /** Creates a new User RecordBuilder by copying an existing User instance */
  public static example.avro.User.Builder newBuilder(example.avro.User other) {
    return new example.avro.User.Builder(other);
  }
  
  /**
   * RecordBuilder for User instances.
   */
  public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase
    implements org.apache.avro.data.RecordBuilder {

    private java.lang.CharSequence name;
    private java.lang.Integer favorite_number;
    private java.lang.CharSequence favorite_color;

    /** Creates a new Builder */
    private Builder() {
      super(example.avro.User.SCHEMA$);
    }
    
    /** Creates a Builder by copying an existing Builder */
    private Builder(example.avro.User.Builder other) {
      super(other);
      if (isValidValue(fields()[0], other.name)) {
        this.name = data().deepCopy(fields()[0].schema(), other.name);
        fieldSetFlags()[0] = true;
      }
      if (isValidValue(fields()[1], other.favorite_number)) {
        this.favorite_number = data().deepCopy(fields()[1].schema(), other.favorite_number);
        fieldSetFlags()[1] = true;
      }
      if (isValidValue(fields()[2], other.favorite_color)) {
        this.favorite_color = data().deepCopy(fields()[2].schema(), other.favorite_color);
        fieldSetFlags()[2] = true;
      }
    }
    
    /** Creates a Builder by copying an existing User instance */
    private Builder(example.avro.User other) {
            super(example.avro.User.SCHEMA$);
      if (isValidValue(fields()[0], other.name)) {
        this.name = data().deepCopy(fields()[0].schema(), other.name);
        fieldSetFlags()[0] = true;
      }
      if (isValidValue(fields()[1], other.favorite_number)) {
        this.favorite_number = data().deepCopy(fields()[1].schema(), other.favorite_number);
        fieldSetFlags()[1] = true;
      }
      if (isValidValue(fields()[2], other.favorite_color)) {
        this.favorite_color = data().deepCopy(fields()[2].schema(), other.favorite_color);
        fieldSetFlags()[2] = true;
      }
    }

    /** Gets the value of the 'name' field */
    public java.lang.CharSequence getName() {
      return name;
    }
    
    /** Sets the value of the 'name' field */
    public example.avro.User.Builder setName(java.lang.CharSequence value) {
      validate(fields()[0], value);
      this.name = value;
      fieldSetFlags()[0] = true;
      return this; 
    }
    
    /** Checks whether the 'name' field has been set */
    public boolean hasName() {
      return fieldSetFlags()[0];
    }
    
    /** Clears the value of the 'name' field */
    public example.avro.User.Builder clearName() {
      name = null;
      fieldSetFlags()[0] = false;
      return this;
    }

    /** Gets the value of the 'favorite_number' field */
    public java.lang.Integer getFavoriteNumber() {
      return favorite_number;
    }
    
    /** Sets the value of the 'favorite_number' field */
    public example.avro.User.Builder setFavoriteNumber(java.lang.Integer value) {
      validate(fields()[1], value);
      this.favorite_number = value;
      fieldSetFlags()[1] = true;
      return this; 
    }
    
    /** Checks whether the 'favorite_number' field has been set */
    public boolean hasFavoriteNumber() {
      return fieldSetFlags()[1];
    }
    
    /** Clears the value of the 'favorite_number' field */
    public example.avro.User.Builder clearFavoriteNumber() {
      favorite_number = null;
      fieldSetFlags()[1] = false;
      return this;
    }

    /** Gets the value of the 'favorite_color' field */
    public java.lang.CharSequence getFavoriteColor() {
      return favorite_color;
    }
    
    /** Sets the value of the 'favorite_color' field */
    public example.avro.User.Builder setFavoriteColor(java.lang.CharSequence value) {
      validate(fields()[2], value);
      this.favorite_color = value;
      fieldSetFlags()[2] = true;
      return this; 
    }
    
    /** Checks whether the 'favorite_color' field has been set */
    public boolean hasFavoriteColor() {
      return fieldSetFlags()[2];
    }
    
    /** Clears the value of the 'favorite_color' field */
    public example.avro.User.Builder clearFavoriteColor() {
      favorite_color = null;
      fieldSetFlags()[2] = false;
      return this;
    }

    @Override
    public User build() {
      try {
        User record = new User();
        record.name = fieldSetFlags()[0] ? this.name : (java.lang.CharSequence) defaultValue(fields()[0]);
        record.favorite_number = fieldSetFlags()[1] ? this.favorite_number : (java.lang.Integer) defaultValue(fields()[1]);
        record.favorite_color = fieldSetFlags()[2] ? this.favorite_color : (java.lang.CharSequence) defaultValue(fields()[2]);
        return record;
      } catch (Exception e) {
        throw new org.apache.avro.AvroRuntimeException(e);
      }
    }
  }
}

四、Javaでシーケンス化を実現し、avroファイルを書く.
Javaクラスを新規作成
     public static void main(String[] args) throws IOException {
     
     // User
     //
     User user1 = new User(); user1.setName("zhangsan"); user1.setFavoriteNumber(21); user1.setFavoriteColor(null);
     //
// Alternate constructor User user2 = new User("Ben", 7, "red");       
    // , Build
// Construct via builder User user3 = User.newBuilder() .setName("Charlie") .setFavoriteColor("blue") .setFavoriteNumber(null) .build(); String path = "D:\\tmp\\user.avro"; // avro DatumWriter userDatumWriter = new SpecificDatumWriter(User.class); DataFileWriter dataFileWriter = new DataFileWriter(userDatumWriter); dataFileWriter.create(user1.getSchema(), new File(path));
     // user avro dataFileWriter.append(user1); dataFileWriter.append(user2); dataFileWriter.append(user3); dataFileWriter.close();
}

runコードを見て、指定したファイルディレクトリがavroファイルを生成したかどうかを確認します.
五、Javaはavroファイルを読み込み、avroの逆シーケンス化を実現する.
public static void main(String[] args) throws IOException {
       DatumReader reader = new SpecificDatumReader(User.class);
       DataFileReader dataFileReader = new DataFileReader(new File("D:\\tmp\\user.avro"), reader);
       User user = null;
       while (dataFileReader.hasNext()) {
            user = dataFileReader.next();
            System.out.println(user);
       }      
}

実行結果:
{"name": "zhangsan", "favorite_number": 21, "favorite_color": null}
{
      "name": "Ben", "favorite_number": 7, "favorite_color": "red"}
{
      "name": "Charlie", "favorite_number": null, "favorite_color": "blue"}

これで、例を書き終わります.
転載先:https://www.cnblogs.com/fillPv/p/5009737.html