java Avro模式以错误的字段顺序(字母顺序)生成

axzmvihb  于 2023-02-21  发布在  Java
关注(0)|答案(1)|浏览(123)

我有一个类,我需要转换成一个avro模式:

public class MonitorStateSchema {
    private MetaResponse c;
    private Header a;
    private MonitorStateEnvelope b;

    public MonitorStateSchema(MetaResponse c, Header a, MonitorStateEnvelope b) {
        this.c = c;
        this.a = a;
        this.b = b;
    }
}

我使用泛型方法从中获取模式

public static <D> void getFromAvro(Class<D> schemaType) {
    Schema schema = ReflectData.get().getSchema(schemaType);

   // other stuff
}

在这样做之后,我得到了一个与预期不同的结果顺序:
预期:

{
  "type": "record",
  "name": "MonitorSchema",
  "namespace": "mypackage.monitor",
  "fields": [
    {
      "name": "c",
      "type": {
        "type": "record",
        "name": "MetaResponse",
        "namespace": "mypackage.monitor",
        "fields": [
          {
            "name": "uuid",
            "type": "string"
          },
          {
            "name": "code",
            "type": "int"
          },
          {
            "name": "message",
            "type": "string"
          }
        ]
      }
    },
    {
      "name": "a",
      "type": {
        "type": "record",
        "name": "Header",
        "namespace": "mypackage.monitor",
        "fields": [
          {
            "name": "apiKey",
            "type": "string"
          },
          {
            "name": "signature",
            "type": "string"
          },
          {
            "name": "nonce",
            "type": "int"
          }
        ]
      }
    },
    {
      "name": "b",
      "type": {
        "type": "record",
        "name": "MonitorEnvelope",
        "fields": [
          {
            "name": "fields",
            "type": {
              "type": "array",
              "items": {
                "type": "record",
                "name": "Field",
                "fields": [
                  {
                    "name": "name",
                    "type": "string"
                  },
                  {
                    "name": "value",
                    "type": "string"
                  }
                ]
              },
              "java-class": "[Lmypackage.monitor.Field;"
            }
          }
        ]
      }
    }
  ]
}

实际结果:

{
  "type": "record",
  "name": "MonitorStateSchema",
  "namespace": "mypackage.monitor",
  "fields": [
    {
      "name": "a",
      "type": {
        "type": "record",
        "name": "Header",
        "namespace": "mypackage.monitor",
        "fields": [
          {
            "name": "apiKey",
            "type": "string"
          },
          {
            "name": "nonce",
            "type": "int"
          },
          {
            "name": "signature",
            "type": "string"
          }
        ]
      }
    },
    {
      "name": "b",
      "type": {
        "type": "record",
        "name": "MonitorStateEnvelope",
        "fields": [
          {
            "name": "fields",
            "type": {
              "type": "array",
              "items": {
                "type": "record",
                "name": "Field",
                "namespace": "mypackage.monitor",
                "fields": [
                  {
                    "name": "name",
                    "type": "string"
                  },
                  {
                    "name": "value",
                    "type": "string"
                  }
                ]
              },
              "java-class": "[Lmypackage.monitor.Field;"
            }
          }
        ]
      }
    },
    {
      "name": "c",
      "type": {
        "type": "record",
        "name": "MetaResponse",
        "namespace": "mypackage.monitor",
        "fields": [
          {
            "name": "code",
            "type": "int"
          },
          {
            "name": "message",
            "type": "string"
          },
          {
            "name": "uuid",
            "type": "string"
          }
        ]
      }
    }
  ]
}

看起来它是按照字段名称的字母顺序排序的,并且在反序列化字节数组时破坏了应用程序。发生这种情况有什么原因吗?

ruarlubt

ruarlubt1#

Avro版本1.10.0将ReflectData更改为sort fields into alphabetical order,因为Class.getDeclaredFields()方法API规范没有规定返回字段的特定顺序。一些Java实现按照字段在Java源文件中出现的顺序返回字段。
如果您不希望字段按字母顺序排序,则必须使用Avro 1.9.2或更低版本。

相关问题