title: Spark SQL removing white spaces
body: <p>I have a simple Spark Program which reads a JSON file and emits a CSV file. IN the JSON data the values contain leading and trailing white spaces, when I emit the CSV the leading and trailing white spaces are gone. Is there a way I can retain the spaces. I tried many options like ignoreTrailingWhiteSpace , ignoreLeadingWhiteSpace but no luck</p>
  
  <p>input.json</p>
  
  <pre><code>{"key" : "k1", "value1": "Good String", "value2": "Good String"}
  {"key" : "k1", "value1": "With Spaces      ", "value2": "With Spaces      "}
  {"key" : "k1", "value1": "with tab\t", "value2": "with tab\t"}
  </code></pre>
  
  <p>output.csv</p>
  
  <pre><code>_corrupt_record,key,value1,value2
  ,k1,Good String,Good String
  ,k1,With Spaces,With Spaces
  ,k1,with tab,with tab
  </code></pre>
  
  <p>expected.csv</p>
  
  <pre><code>_corrupt_record,key,value1,value2
  ,k1,Good String,Good String
  ,k1,With Spaces      ,With Spaces      
  ,k1,with tab\t,with tab\t
  </code></pre>
  
  <p>my code:</p>
  
  <pre><code>public static void main(String[] args) {
      SparkSession sparkSession = SparkSession
              .builder()
              .appName(TestSpark.class.getName())
              .master("local[1]").getOrCreate();
  
      SparkContext context = sparkSession.sparkContext();
      context.setLogLevel("ERROR");
      SQLContext sqlCtx = sparkSession.sqlContext();
      System.out.println("Spark context established");
  
      List&lt;StructField&gt; kvFields = new ArrayList&lt;&gt;();
      kvFields.add(DataTypes.createStructField("_corrupt_record", DataTypes.StringType, true));
      kvFields.add(DataTypes.createStructField("key", DataTypes.StringType, true));
      kvFields.add(DataTypes.createStructField("value1", DataTypes.StringType, true));
      kvFields.add(DataTypes.createStructField("value2", DataTypes.StringType, true));
      StructType employeeSchema = DataTypes.createStructType(kvFields);
  
      Dataset&lt;Row&gt; dataset =
              sparkSession.read()
                      .option("inferSchema", false)
                      .format("json")
                      .schema(employeeSchema)
                      .load("D:\\dev\\workspace\\java\\simple-kafka\\key_value.json");
  
      dataset.createOrReplaceTempView("sourceView");
      sqlCtx.sql("select * from sourceView")
              .write()
              .option("header", true)
              .format("csv")
              .save("D:\\dev\\workspace\\java\\simple-kafka\\output\\" + UUID.randomUUID().toString());
      sparkSession.close();
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>Added POM dependencies</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.spark&lt;/groupId&gt;
          &lt;artifactId&gt;spark-core_2.10&lt;/artifactId&gt;
          &lt;version&gt;2.1.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.spark&lt;/groupId&gt;
          &lt;artifactId&gt;spark-sql_2.10&lt;/artifactId&gt;
          &lt;version&gt;2.1.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.spark&lt;/groupId&gt;
          &lt;artifactId&gt;spark-sql-kafka-0-10_2.10&lt;/artifactId&gt;
          &lt;version&gt;2.1.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.apache.spark&lt;/groupId&gt;
          &lt;artifactId&gt;spark-streaming_2.10&lt;/artifactId&gt;
          &lt;version&gt;2.1.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.slf4j&lt;/groupId&gt;
          &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt;
          &lt;version&gt;1.7.22&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>