title: How to set array of records Using GenericRecordBuilder
body: <p>I'm trying to turn a Scala object (i.e case class) into byte array.</p>
  
  <p>In order to do so, I'm inserting the object content into a GenericRecordBuilder using its specific schema, and eventually using GenericDatumWriter i turn it into a byte array. </p>
  
  <p>I have no problem to set primitive types, and array of primitive types into the GenericRecordBuilder.</p>
  
  <p>But, I need help with Inserting array of records into the GenericRecordBuilder, and create a byte array from it.</p>
  
  <p>What is the right way to insert array of records into the GenericRecordBuilder?</p>
  
  <p>Here is part of what i'm trying to do:</p>
  
  <p>This is the Schema:</p>
  
  <pre><code>{
      "type": "record",
      "name": "test1",
      "namespace": "ns",
      "fields": [
        {
          "name": "t_name",
          "type": "string",
          "default": "a"
        },
        {
          "name": "t_num",
          "type": "int",
          "default": 0
        },
        {"name" : "t_arr", "type":
          ["null",
           {"type": "array", "items": {
                "name": "t_arr_a",
                "type": "record",
                "fields": [
                  {
                    "name": "t_arr_f1",
                    "type": "int",
                    "default": 0
                  },
                  {
                    "name": "t_arr_f2",
                    "type": "int",
                    "default": 0
                  }
                ]
              }
              }
           ]
         }
      ]
  }
  </code></pre>
  
  <p>This is the Scala class that populate the GenericRecordBuilder and transform it into byte Array:</p>
  
  <pre><code>package utils
  
  import java.io.ByteArrayOutputStream
  
  import org.apache.avro.{Schema, generic}
  import org.apache.avro.generic.{GenericData, GenericDatumWriter}
  import org.apache.avro.io.EncoderFactory
  import org.apache.avro.generic.GenericRecordBuilder
  
  object CheckRecBuilder extends App {
  
    val avroSchema: Schema = new Schema.Parser().parse(this.getClass.getResourceAsStream("/data/myschema.avsc"))
    val recordBuilder = new GenericRecordBuilder(avroSchema)
  
    recordBuilder.set("t_name", "X")
    recordBuilder.set("t_num", 100)
  
  
    recordBuilder.set("t_arr", ???)
  
    val record = recordBuilder.build()
  
  
    val w = new GenericDatumWriter[GenericData.Record](avroSchema)
    val outputStream = new ByteArrayOutputStream()
    val e = EncoderFactory.get.binaryEncoder(outputStream, null)
    w.write(record, e)
    val barr =  outputStream.toByteArray
  
    println("End")
  
  }
  </code></pre>