title: Applying a schema to a Spark's Dataset of a java object
body: <p>There's a similar issue here: <a href="https://stackoverflow.com/questions/44965161/how-to-add-a-schema-to-a-dataset-in-spark">How to add a schema to a Dataset in Spark?</a></p>
  
  <p>However the issue I'm facing is that I have an already predefined <code>Dataset&lt;Obj1&gt;</code> and I want to define a schema to match its data-members. The end goal is to be able to join between two java objects.</p>
  
  <p>Sample code:</p>
  
  <pre><code>Dataset&lt;Row&gt; rowDataset = spark.getSpark().sqlContext().createDataFrame(rowRDD, schema).toDF();
  Dataset&lt;MyObj&gt; objResult = rowDataset.map((MapFunction&lt;Row, MyObj&gt;) row -&gt;
          new MyObj(
                  row.getInt(row.fieldIndex("field1")),
                  row.isNullAt(row.fieldIndex("field2")) ? "" : row.getString(row.fieldIndex("field2")),
                  row.isNullAt(row.fieldIndex("field3")) ? "" : row.getString(row.fieldIndex("field3")),
                  row.isNullAt(row.fieldIndex("field4")) ? "" : row.getString(row.fieldIndex("field4"))
          ), Encoders.javaSerialization(MyObj.class));
  </code></pre>
  
  <p>If I'm printing the schema of the row dataset I get the schema as expected:</p>
  
  <pre><code>rowDataset.printSchema();
  
  root
   |-- field1: integer (nullable = false)
   |-- field2: string (nullable = false)
   |-- field3: string (nullable = false)
   |-- field4: string (nullable = false)
  </code></pre>
  
  <p>If I'm printing the Object dataset I'm losing the actual schema</p>
  
  <pre><code>objResult.printSchema();
  
  root
   |-- value: binary (nullable = true)
  </code></pre>
  
  <p>The question is how can I apply a schema for the <code>Dataset&lt;MyObj&gt;</code>?</p>