使用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:
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 nestedSchoolFriendsrecord definition. - Avro's GenericRecord relies on Java collections (like
ArrayList) for array fields, so we can't use Scala's nativeListdirectly here. - Each nested
SchoolFriendsrecord is created using its own schema, and populated the same way as the mainAgentRecommendationListrecord.
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

