You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GenericRecord向Avro嵌套数组记录填充数据

Hey there! Let's walk through how to populate that friends array using Avro's GenericRecord in Scala. Here's a step-by-step breakdown that builds on your existing code:

Populating the friends Array in Avro GenericRecord

First, you'll need to extract the schema for the nested SchoolFriends record type from your main schema. Then create individual record instances for each friend, collect them into a Java-compatible list (since Avro works with Java collections), and finally assign that list to the friends field of your main record.

Here's the complete code example:

val avschema = new RestService(URL).getLatestVersion(name)
val schema = new Schema.Parser().parse(avschema.getSchema)

// 1. Extract the nested SchoolFriends record schema from the friends array field
val friendsArraySchema = schema.getField("friends").schema()
val schoolFriendSchema = friendsArraySchema.getElementType.asInstanceOf[Schema]

// 2. Create individual SchoolFriend records and populate their fields
val friend1 = new GenericData.Record(schoolFriendSchema)
friend1.put("Name", "Alice Smith")
friend1.put("phoneNumber", "555-1234")
friend1.put("email", "alice@example.com")

val friend2 = new GenericData.Record(schoolFriendSchema)
friend2.put("Name", "Bob Johnson")
friend2.put("phoneNumber", "555-5678")
friend2.put("email", "bob@example.com")

// 3. Collect friends into a Java ArrayList (Avro expects Java collections for arrays)
import java.util.ArrayList
val friendsList = new ArrayList[GenericData.Record]()
friendsList.add(friend1)
friendsList.add(friend2)

// 4. Assign the list to the main record's fields
val record = new GenericData.Record(schema)
record.put("userid", "user_12345")
record.put("friends", friendsList)

Key Notes:

  • We use getElementType() on the array schema to access the nested SchoolFriends record definition.
  • Avro's GenericRecord relies on Java collections (like ArrayList) for array fields, so we can't use Scala's native List directly here.
  • Each nested SchoolFriends record is created using its own schema, and populated the same way as the main AgentRecommendationList record.

You can repeat step 2 to add as many friends as needed, then just add them to the friendsList before assigning it to the main record.

Content of the question originates from Stack Exchange, asked by Brian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:59:55