我有一些数据遵循下面给出的 avro 模式。我通过使用 avro-tools 实用程序编译该模式来生成 Java 类。然而,Java 类仅包含标准 getter、setter 和构造函数。如何在我的 java 代码中访问自定义属性“mapto”的值。目的是将输入 avro 文件转换为输出 avro 文件,其中输出字段名称来自“mapto”字段。
{
"type":"record",
"name":"Products",
"namespace":"com.example.datasets",
"fields":[
{
"name":"ProductDetails",
"type":["null",{
"type":"record",
"name":"ProductDetailsRecord",
"fields":[
{
"name":"itemName",
"type":["null","string"],
"default":null,
"mapto":"productName"
},
{
"name":"itemCode",
"type":["null","string"],
"default":null,
"mapto":"productId"
}
]
}]
}
]
}
// I assume details is not null
ProductDetailsRecord details;
for (var avroField : details.getSchema().getFields()) {
var value = details.get(avroField.name());
var mapTo = avroField.getProp("mapto");
}
Schema 类还具有 getType 方法,这对于您递归访问
Products
类的嵌套字段可能很有用