title: How to convert nested avro GenericRecord to Row
body: <p>I have a code to convert my avro record to Row using function <code>avroToRowConverter()</code></p>
  
  <pre><code>directKafkaStream.foreachRDD(rdd -&gt; {
          JavaRDD&lt;Row&gt; newRDD= rdd.map(x-&gt;{
  
              Injection&lt;GenericRecord, byte[]&gt; recordInjection = GenericAvroCodecs.toBinary(SchemaRegstryClient.getLatestSchema("poc2"));
              return avroToRowConverter(recordInjection.invert(x._2).get());
              });
  </code></pre>
  
  <p>This function is not working for nested schema <code>(TYPE= UNION)</code>. </p>
  
  <pre><code>private static Row avroToRowConverter(GenericRecord avroRecord) {
      if (null == avroRecord) {
          return null;
      }
      //GenericData
      Object[] objectArray = new Object[avroRecord.getSchema().getFields().size()];
      StructType structType = (StructType) SchemaConverters.toSqlType(avroRecord.getSchema()).dataType();
      for (Schema.Field field : avroRecord.getSchema().getFields()) {
  
          if(field.schema().getType().toString().equalsIgnoreCase("STRING") || field.schema().getType().toString().equalsIgnoreCase("ENUM")){
              objectArray[field.pos()] = ""+avroRecord.get(field.pos());
          }else {
              objectArray[field.pos()] = avroRecord.get(field.pos());
          }
      }
  
      return new GenericRowWithSchema(objectArray, structType);
  }
  </code></pre>
  
  <p>Can anyone suggest how can I convert complex schema to ROW?</p>