title: Unable to generate avro generic record from object
body: <p>I am trying to send avro records to A Kafka topic using Kafka producer. I have a User class and I am sending object of that class. Below code works fine if i use <code>avroRecord.put();</code> to set each attribute. But what i want is to create A Generic Record from an object without using avroRecord.put(); for each and every attribute. </p>
  
  <p>User class</p>
  
  <pre><code>public class User {
      int id;
  
      String name;
  
      public User(int id, String name) {
  
          super();
  
          this.id = id;
  
          this.name = name;
  
      }
  
      public int getId() {
  
          return id;
  
      }
  
      public void setId(int id) {
  
          this.id = id;
  
      }
  
      public String getName() {
  
          return name;
  
      }
  
      public void setName(String name) {
  
          this.name = name;
  
      }
  
  }
  </code></pre>
  
  <p>Sender class   </p>
  
  <pre><code>import org.apache.avro.Schema;
  
  import org.apache.avro.generic.GenericData;
  
  import org.apache.avro.generic.GenericDatumWriter;
  
  import org.apache.avro.generic.GenericRecord;
  
  import org.apache.avro.io.DatumWriter;
  
  import org.apache.avro.io.Encoder;
  
  import org.apache.avro.io.EncoderFactory;
  
  import org.apache.avro.reflect.ReflectData;
  
  import org.apache.kafka.clients.producer.KafkaProducer;
  
  import org.apache.kafka.clients.producer.ProducerConfig;
  
  import org.apache.kafka.clients.producer.ProducerRecord;
  
  
  
  import java.io.ByteArrayOutputStream;
  
  import java.io.IOException;
  
  import java.util.Properties;
  
  
  
  import vo.User;
  
  
  
  public class Sender {
  
      public static void main(String[] args) {
  
  
  
          User user = new User(10,"testName");
  
          Schema schema = ReflectData.get().getSchema(user.getClass());
  
          GenericRecord avroRecord = new GenericData.Record(schema);
  
  
  
          //working fine
  
          /*avroRecord.put("id", user.getId());
  
          avroRecord.put("name", user.getName());*/
  
  
  
          //not working
  
          DatumWriter&lt;Object&gt; datumWriter = new GenericDatumWriter&lt;Object&gt;(schema);
  
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  
          Encoder encoder = EncoderFactory.get().binaryEncoder(outputStream, null);
  
          try {
  
              datumWriter.write(user, encoder);
  
              encoder.flush();
  
          } catch (IOException e1) {
  
              e1.printStackTrace();
  
          }
  
  
  
          ProducerRecord&lt;String, GenericRecord&gt; record = new ProducerRecord&lt;&gt;("avrotesttopic1",avroRecord);
  
  
  
          Properties props = new Properties();
  
          props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "127.0.0.1:9092");
  
          props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
  
          props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,io.confluent.kafka.serializers.KafkaAvroSerializer.class);
  
          props.put("schema.registry.url", "http://127.0.0.1:8081");
  
  
  
          KafkaProducer&lt;String, GenericRecord&gt; producer = new KafkaProducer&lt;String, GenericRecord&gt;(props);
  
  
  
          try {
  
              producer.send(record);
  
              producer.flush();
  
          } catch (Exception e) {
  
              e.printStackTrace();
  
          }
  
  
  
          producer.close();
  
      }
  
  }
  </code></pre>
  
  <p>How can i publish this object to my Kafka topic as Avro? </p>
  
  <p>I have referred the below links</p>
  
  <p><a href="https://github.com/akmalmuqeeth/confluent-kafka-spring-demo/blob/master/src/main/java/ConfluentProducerApp.java" rel="nofollow noreferrer">https://github.com/akmalmuqeeth/confluent-kafka-spring-demo/blob/master/src/main/java/ConfluentProducerApp.java</a></p>
  
  <p><a href="https://findusages.com/search/org.apache.avro.io.DatumWriter/write" rel="nofollow noreferrer">https://findusages.com/search/org.apache.avro.io.DatumWriter/write</a>$2?offset=23</p>
  
  <p><a href="https://www.ctheu.com/2017/03/02/serializing-data-efficiently-with-apache-avro-and-dealing-with-a-schema-registry/" rel="nofollow noreferrer">https://www.ctheu.com/2017/03/02/serializing-data-efficiently-with-apache-avro-and-dealing-with-a-schema-registry/</a></p>
  
  <p>Thank you. </p>