title: Convert Spark Dataset to JSON and Write to Kafka Producer
body: <p>I want to read a table from Hive and write to Kafka Producer(batch job).</p>
  
  <p>Currently, I am reading the table as <code>Dataset&lt;Row&gt;</code> in my java class and trying to convert to a json so that i can write as json message using KafkaProducer.</p>
  
  <pre><code>Dataset&lt;Row&gt; data = spark.sql("select * from tablename limit 5");
  List&lt;Row&gt; rows = data.collectAsList();
  for(Row row: rows) {
          List&lt;String&gt; stringList = new ArrayList&lt;String&gt;(Arrays.asList(row.schema().fieldNames())); 
          Seq&lt;String&gt; row_seq = JavaConverters.asScalaIteratorConverter(stringList.iterator()).asScala().toSeq();
          Map map = (Map) row.getValuesMap(row_seq);
          JSONObject json = new JSONObject();
          json.putAll( map);
  
          ProducerRecord&lt;String, String&gt; record = new ProducerRecord&lt;String, String&gt;(SPARK_CONF.get("topic.name"), json.toString());
          producer.send(record);
  </code></pre>
  
  <p>I am getting ClassCastException </p>