title: Avro and Kafka by making use of SchemaBuilder
body: <p>I went through the tutorial from <a href="https://www.baeldung.com/java-apache-avro" rel="nofollow noreferrer">baeldung</a>. They mention there are two ways to create a schema.</p>
  
  <ul>
  <li>By writing the json representation and adding the maven plugin to produce the class </li>
  <li>By using the <code>SchemaBuilder</code>, which they also mention is a better choice. </li>
  </ul>
  
  <p>Unfortunately in the git example I only see the json way. </p>
  
  <p>Lets say I have this Avro schema:</p>
  
  <pre><code>{
    "type":"record",
    "name":"TestFile",
    "namespace":"com.example.kafka.data.ingestion.model",
    "fields":[
      {
        "name":"date",
        "type":"long"
      },
      {
        "name":"counter",
        "type":"int"
      },
      {
        "name":"mc",
        "type":"string"
      }
    ]
  }
  </code></pre>
  
  <p>By adding this plugin in my pom file:</p>
  
  <pre><code>&lt;plugin&gt;
     &lt;groupId&gt;org.apache.avro&lt;/groupId&gt;
     &lt;artifactId&gt;avro-maven-plugin&lt;/artifactId&gt;
     &lt;version&gt;1.8.0&lt;/version&gt;
     &lt;executions&gt;
        &lt;execution&gt;
           &lt;id&gt;schemas&lt;/id&gt;
           &lt;phase&gt;generate-sources&lt;/phase&gt;
           &lt;goals&gt;
              &lt;goal&gt;schema&lt;/goal&gt;
              &lt;goal&gt;protocol&lt;/goal&gt;
              &lt;goal&gt;idl-protocol&lt;/goal&gt;
           &lt;/goals&gt;
           &lt;configuration&gt;
              &lt;sourceDirectory&gt;${project.basedir}/src/main/resources/&lt;/sourceDirectory&gt;
              &lt;outputDirectory&gt;${project.basedir}/src/main/java/&lt;/outputDirectory&gt;
           &lt;/configuration&gt;
        &lt;/execution&gt;
     &lt;/executions&gt;
  &lt;/plugin&gt;
  </code></pre>
  
  <p>and building with generate-sources a <code>TestFile.java</code> is created to the destination I said. 
  Then for sending to a kafka topic I can do the following:</p>
  
  <pre><code>TestFile test = TestFile.newBuilder()
                                              .setDate(102928374747)
                                              .setCounter(2)
                                              .setMc("Some string")
                                              .build();
  kafkaTemplate.send(topicName, test);
  </code></pre>
  
  <p>The equivalent of creating the schema with <code>SchemaBuilder</code> would be:</p>
  
  <pre><code>Schema testFileSchema = SchemaBuilder   .record("TestFile")
                                              .namespace("com.example.kafka.data.ingestion.model")
                                              .fields()
                                              .requiredLong("date")
                                              .requiredInt("counter")
                                              .requiredString("mc")
                                              .endRecord();
  </code></pre>
  
  <p>But how can I now generate the POJO and send my <code>TestFile</code> data to my kafka topic?</p>