title: Encoder for Row Type Spark Datasets
body: <p>I would like to write an encoder for a <a href="https://spark.apache.org/docs/2.0.0/api/java/index.html?org/apache/spark/sql/Row.html" rel="noreferrer">Row</a> type in DataSet, for a map operation that I am doing. Essentially, I do not understand how to write encoders.</p>
  
  <p>Below is an example of a map operation:</p>
  
  <p><code>In the example below, instead of returning Dataset&lt;String&gt;, I would like to return Dataset&lt;Row&gt;</code></p>
  
  <pre><code>Dataset&lt;String&gt; output = dataset1.flatMap(new FlatMapFunction&lt;Row, String&gt;() {
              @Override
              public Iterator&lt;String&gt; call(Row row) throws Exception {
  
                  ArrayList&lt;String&gt; obj = //some map operation
                  return obj.iterator();
              }
          },Encoders.STRING());
  </code></pre>
  
  <p>I understand that instead of a string Encoder needs to be written as follows:</p>
  
  <pre><code>    Encoder&lt;Row&gt; encoder = new Encoder&lt;Row&gt;() {
          @Override
          public StructType schema() {
              return join.schema();
              //return null;
          }
  
          @Override
          public ClassTag&lt;Row&gt; clsTag() {
              return null;
          }
      };
  </code></pre>
  
  <p>However, I do not understand the clsTag() in the encoder, and I am trying to find a running example which can demostrate something similar (i.e. an encoder for a row type)</p>
  
  <p>Edit - This is not a copy of the question mentioned : <a href="https://stackoverflow.com/questions/39433419/encoder-error-while-trying-to-map-dataframe-row-to-updated-row">Encoder error while trying to map dataframe row to updated row</a> as the answer talks about using Spark 1.x in Spark 2.x (I am not doing so), also I am looking for an encoder for a Row class rather than resolve an error. Finally, I was looking for a solution in Java, not in Scala.</p>