java - Apache avro 仅生成公共(public)属性

标签 java avro

我正在构建 Kaa project 的源代码他们使用 Apache avro 来生成源代码。

我使用avro库作为here的指令但我只得到“@Deprecated”和“public”属性,我认为它们应该是“private”。

这是我的 .avsc 文件:

{
   "type":"record",
   "name":"User",
   "namespace":"com.example",
   "fields":[
      {
         "name":"username",
         "type":"string"
      },
      {
         "name":"password",
         "type":"string"
      }
   ]
}

这是我的 User.java:

/**
 * Autogenerated by Avro
 * 
 * DO NOT EDIT DIRECTLY
 */
package com.example;  
@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\":\"com.example\",\"fields\":[{\"name\":\"username\",\"type\":\"string\"},{\"name\":\"password\",\"type\":\"string\"}]}");
  public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
  @Deprecated public java.lang.CharSequence username;
  @Deprecated public java.lang.CharSequence password;

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

  /**
   * All-args constructor.
   */
  public User(java.lang.CharSequence username, java.lang.CharSequence password) {
    this.username = username;
    this.password = password;
  }

  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 username;
    case 1: return password;
    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: username = (java.lang.CharSequence)value$; break;
    case 1: password = (java.lang.CharSequence)value$; break;
    default: throw new org.apache.avro.AvroRuntimeException("Bad index");
    }
  }

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

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

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

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

  /** Creates a new User RecordBuilder */
  public static com.example.User.Builder newBuilder() {
    return new com.example.User.Builder();
  }

  /** Creates a new User RecordBuilder by copying an existing Builder */
  public static com.example.User.Builder newBuilder(com.example.User.Builder other) {
    return new com.example.User.Builder(other);
  }

  /** Creates a new User RecordBuilder by copying an existing User instance */
  public static com.example.User.Builder newBuilder(com.example.User other) {
    return new com.example.User.Builder(other);
  }

  /**
   * RecordBuilder for User instances.
   */
  public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<User>
    implements org.apache.avro.data.RecordBuilder<User> {

    private java.lang.CharSequence username;
    private java.lang.CharSequence password;

    /** Creates a new Builder */
    private Builder() {
      super(com.example.User.SCHEMA$);
    }

    /** Creates a Builder by copying an existing Builder */
    private Builder(com.example.User.Builder other) {
      super(other);
      if (isValidValue(fields()[0], other.username)) {
        this.username = data().deepCopy(fields()[0].schema(), other.username);
        fieldSetFlags()[0] = true;
      }
      if (isValidValue(fields()[1], other.password)) {
        this.password = data().deepCopy(fields()[1].schema(), other.password);
        fieldSetFlags()[1] = true;
      }
    }

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

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

    /** Sets the value of the 'username' field */
    public com.example.User.Builder setUsername(java.lang.CharSequence value) {
      validate(fields()[0], value);
      this.username = value;
      fieldSetFlags()[0] = true;
      return this; 
    }

    /** Checks whether the 'username' field has been set */
    public boolean hasUsername() {
      return fieldSetFlags()[0];
    }

    /** Clears the value of the 'username' field */
    public com.example.User.Builder clearUsername() {
      username = null;
      fieldSetFlags()[0] = false;
      return this;
    }

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

    /** Sets the value of the 'password' field */
    public com.example.User.Builder setPassword(java.lang.CharSequence value) {
      validate(fields()[1], value);
      this.password = value;
      fieldSetFlags()[1] = true;
      return this; 
    }

    /** Checks whether the 'password' field has been set */
    public boolean hasPassword() {
      return fieldSetFlags()[1];
    }

    /** Clears the value of the 'password' field */
    public com.example.User.Builder clearPassword() {
      password = null;
      fieldSetFlags()[1] = false;
      return this;
    }

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

我是java新手,所以请告诉我哪里不正确。

最佳答案

我认为没有办法将它们标记为私有(private),但您应该使用 Builder 对象来创建类,并使用 getter 方法来检索字段,而不是直接访问公共(public)字段

关于java - Apache avro 仅生成公共(public)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56577425/

相关文章:

java - Q : Converting Avro to Parquet in Memory

file - 文件格式存储的最佳实践 (Hadoop)

java - 由于 ClosedChannelException (DFSOutputStream.checkClosed) 而导致的 Spark 作业失败

java - Avro 序列化对象不可序列化问题

java - 为什么很多 Java Stream 接口(interface)方法在参数中使用下界通配符而不是泛型类型?

Java 套接字 : can you send from one thread and receive on another?

java - 用户在 fragment 的某个 Activity 上花费了多少时间?

java - Netty Decoder 在 Windows 和 Linux 中生成不同的输出

Java 将图像添加到 BLOB 数据字段

java - 如何从Java读取avro?