title: Validate CSV file columns with Spark
body: <p>I am trying to read a CSV file (which is supposed to have a header) in Spark and load the data into an existing table (with predefined columns and datatypes). The csv file can be very large, so it would be great if I could avoid doing it if the columns header from the csv is not "valid".</p>
  
  <p>When I'm currently reading the file, I'm specyfing a StructType as the schema, but this does not validate that the header contains the right columns in the right order.
  This is what I have so far (I'm building the "schema" StructType in another place):</p>
  
  <pre><code>sqlContext
    .read()
    .format("csv")
    .schema(schema)
    .load("pathToFile");
  </code></pre>
  
  <p>If I add the <code>.option("header", "true)"</code> line it will skill over the first line of the csv file and use the names I'm passing in the StructType's <code>add</code> method. (e.g. if I build the StructType with "id" and "name" and the first row in the csv is "idzzz,name", the resulting dataframe will have columns "id" and "name". I want to be able to validate that the csv header has the same name for columns as the table I'm planning on loading the csv.</p>
  
  <p>I tried reading the file with <code>.head()</code>, and doing some checks on that first row, but that downloads the whole file.</p>
  
  <p>Any suggestion is more than welcomed.</p>