我们如何在Kafka生产者代码中访问Avro模式中的数组字段?

huangapple go评论105阅读模式
英文:

How can we access the array fields from Avro Schema in the Kafka producer code?

问题

我有以下的Avro模式:

  1. {
  2. "name": "ABC",
  3. "type": "record",
  4. "namespace": "com.schema.avro",
  5. "fields": [
  6. {
  7. "name": "str1",
  8. "type": "string"
  9. },
  10. {
  11. "name": "arrData",
  12. "type": [
  13. "null",
  14. {
  15. "type": "array",
  16. "items": {
  17. "name": "arrData_record",
  18. "type": "record",
  19. "fields": [
  20. {
  21. "name": "var1",
  22. "type": ["null", "string"],
  23. "default": null
  24. },
  25. {
  26. "name": "var2",
  27. "type": ["null", "int"],
  28. "default": null
  29. }
  30. ]
  31. }
  32. }
  33. ],
  34. "default": null
  35. }
  36. ]
  37. }

我使用了POM.XML Maven插件来生成这些类。以下是从插件生成的两个类:
i) ABC
ii) arrData_record

我能够像下面这样使用ABC类中的str1字段:

  1. ABC abc = ABC.newBuilder()
  2. .setStr1("随机值")
  3. .build()

类似地,我也可以使用.setArrData()来使用arrData字段。

我想要以类似的方式设置数组字段,使用.setVar1()和.setVar2()。您可以提供一些关于如何使用Array类中的字段并进行类似设置的示例代码吗?谢谢。

英文:

I have the below Avro Schema:

  1. `{
  2. "name": "ABC",
  3. "type": "record",
  4. "namespace": "com.schema.avro",
  5. "fields": [
  6. {
  7. "name": "str1",
  8. "type": "string"
  9. },
  10. {
  11. "name": "arrData",
  12. "type":["null",
  13. {
  14. "type": "array",
  15. "items": {
  16. "name": "arrData_record",
  17. "type": "record",
  18. "fields": [
  19. {
  20. "name": "var1",
  21. "type": ["null","string"],
  22. "default": null
  23. },
  24. {
  25. "name": "var2",
  26. "type": ["null","int"],
  27. "default": null
  28. }
  29. ]
  30. }
  31. }
  32. ],
  33. "default": null
  34. }
  35. ]
  36. }`

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:

  1. ABC abc = ABC.newBuilder()
  2. .setStr1("random value")
  3. .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模式类可以像这样进行操作:

  1. ABC
  2. .newBuilder()
  3. .setArrData(Collections.singletonList(arrData_record
  4. .newBuilder()
  5. .setVar1("Var1")
  6. .setVar2(2)
  7. .build()))
  8. .build();
英文:

The generated class from the Avro schema could be manipulated like this

  1. ABC
  2. .newBuilder()
  3. .setArrData(Collections.singletonList(arrData_record
  4. .newBuilder()
  5. .setVar1("Var1")
  6. .setVar2(2)
  7. .build()))
  8. .build();

答案2

得分: 0

尽管我接受了尼克的答案,那个答案是正确的,但最终我使用了以下代码,因为我想在Var1和Var2中添加多个值-

使用以下代码填充了arrData_record的对象-

  1. arrData_record a1 = arrData_record.newBuilder()
  2. .setVar1("abc")
  3. .setVar2(1)
  4. .build();
  5. arrData_record a2 = arrData_record.newBuilder()
  6. .setVar1("def")
  7. .setVar2(2)
  8. .build();

将其添加到arrData列表中-

  1. arrData.add(a1);
  2. arrData.add(a2);

最后添加了arrData-

  1. ABC abc = ABC.newBuilder()
  2. .setStr1("随机值")
  3. .setarrData(arrData)
  4. .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()

huangapple
  • 本文由 发表于 2020年8月25日 06:50:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63569679.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定