英文:
How can we access the array fields from Avro Schema in the Kafka producer code?
问题
我有以下的Avro模式:
{
"name": "ABC",
"type": "record",
"namespace": "com.schema.avro",
"fields": [
{
"name": "str1",
"type": "string"
},
{
"name": "arrData",
"type": [
"null",
{
"type": "array",
"items": {
"name": "arrData_record",
"type": "record",
"fields": [
{
"name": "var1",
"type": ["null", "string"],
"default": null
},
{
"name": "var2",
"type": ["null", "int"],
"default": null
}
]
}
}
],
"default": null
}
]
}
我使用了POM.XML Maven插件来生成这些类。以下是从插件生成的两个类:
i) ABC
ii) arrData_record
我能够像下面这样使用ABC类中的str1字段:
ABC abc = ABC.newBuilder()
.setStr1("随机值")
.build()
类似地,我也可以使用.setArrData()来使用arrData字段。
我想要以类似的方式设置数组字段,使用.setVar1()和.setVar2()。您可以提供一些关于如何使用Array类中的字段并进行类似设置的示例代码吗?谢谢。
英文:
I have the below Avro Schema:
`{
"name": "ABC",
"type": "record",
"namespace": "com.schema.avro",
"fields": [
{
"name": "str1",
"type": "string"
},
{
"name": "arrData",
"type":["null",
{
"type": "array",
"items": {
"name": "arrData_record",
"type": "record",
"fields": [
{
"name": "var1",
"type": ["null","string"],
"default": null
},
{
"name": "var2",
"type": ["null","int"],
"default": null
}
]
}
}
],
"default": null
}
]
}`
I used the POM.XML Maven plugin to generate the classes.
Below 2 classes were generated from the plugin-
i) ABC
ii) arrData_record
I am able to use the field str1 from ABC as below:
ABC abc = ABC.newBuilder()
.setStr1("random value")
.build()
Similarly, I am also getting an option to use .setArrData().
I want to set the array fields in a similar way using .setVar1() and .setVar2().
How can I use the fields from the Array class and set it similarly? Any sample code would be of great help.
Thanks.
答案1
得分: 3
生成的Avro模式类可以像这样进行操作:
ABC
.newBuilder()
.setArrData(Collections.singletonList(arrData_record
.newBuilder()
.setVar1("Var1")
.setVar2(2)
.build()))
.build();
英文:
The generated class from the Avro schema could be manipulated like this
ABC
.newBuilder()
.setArrData(Collections.singletonList(arrData_record
.newBuilder()
.setVar1("Var1")
.setVar2(2)
.build()))
.build();
答案2
得分: 0
尽管我接受了尼克的答案,那个答案是正确的,但最终我使用了以下代码,因为我想在Var1和Var2中添加多个值-
使用以下代码填充了arrData_record的对象-
arrData_record a1 = arrData_record.newBuilder()
.setVar1("abc")
.setVar2(1)
.build();
arrData_record a2 = arrData_record.newBuilder()
.setVar1("def")
.setVar2(2)
.build();
将其添加到arrData列表中-
arrData.add(a1);
arrData.add(a2);
最后添加了arrData-
ABC abc = ABC.newBuilder()
.setStr1("随机值")
.setarrData(arrData)
.build();
英文:
Even though I accepted Nic's answer and that answer is correct, I used the below code finally as I wanted to add multiple values in Var1 and Var2-
Populated the objects of the arrData_record using below code-
arrData_record a1 = arrData_record.newBuilder()
.setVar1("abc")
.setVar2(1)
.build();
arrData_record a2 = arrData_record.newBuilder()
.setVar1("def")
.setVar2(2)
.build();
Added it into list arrData-
arrData.add(a1);
arrData.add(a2);
Finally added arrData-
ABC abc = ABC.newBuilder()
.setStr1("random value")
.setarrData(arrData)
.build()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论