title: In Apache Spark Writing a Dataframe to Hive table in Java
body: <p>I'm trying to accomplish a simple things of "writing a dataframe to Hive table", below is the code written in Java. I'm using Cloudera VM with no changes.</p>
  
  <pre><code> public static void main(String[] args) {
      String master = "local[*]";
  
      SparkSession sparkSession = SparkSession
              .builder().appName(JsonToHive.class.getName())
              //.config("spark.sql.warehouse.dir", "hdfs://localhost:50070/user/hive/warehouse/")
              .enableHiveSupport().master(master).getOrCreate();
  
      SparkContext context = sparkSession.sparkContext();
      context.setLogLevel("ERROR");
  
      SQLContext sqlCtx = sparkSession.sqlContext();
      Dataset&lt;Row&gt; rowDataset = sqlCtx.jsonFile("employees.json");
      rowDataset.printSchema();
      rowDataset.registerTempTable("employeesData");
  
      Dataset&lt;Row&gt; firstRow = sqlCtx.sql("select employee.firstName, employee.addresses from employeesData");
      firstRow.show();
  
      sparkSession.catalog().listTables().select("*").show();
  
      firstRow.write().mode() saveAsTable("default.employee");
      sparkSession.close();
  
  }
  </code></pre>
  
  <p>I have create the managed table in HIVE using the HQL , </p>
  
  <pre><code> CREATE TABLE employee ( firstName STRING, lastName STRING, addresses  ARRAY &lt; STRUCT &lt; street:STRING,  city:STRING, state:STRING &gt; &gt; )  STORED AS PARQUET;
  </code></pre>
  
  <p>I'm reading a simple JSON file for data from "employees.json"</p>
  
  <pre><code>{"employee":{"firstName":"Neil","lastName":"Irani","addresses":[{"street":"36th","city":"NYC","state":"Ny"},{"street":"37th","city":"NYC","state":"Ny"},{"street":"38th","city":"NYC","state":"Ny"}]}}
  </code></pre>
  
  <p>It says "Table <code>default</code>.<code>employee</code> already exists.;" and it does not append the content. How to append the content to the hive table ??</p>
  
  <p>If I set the mode("append"), it does not complain but it does not write the content as well .. </p>
  
  <p>firstRow.write().mode("append") saveAsTable("default.employee");</p>
  
  <p>Any help will be appreciated... thanks.</p>
  
  <pre><code>+-------------+--------+-----------+---------+-----------+
  |         name|database|description|tableType|isTemporary|
  +-------------+--------+-----------+---------+-----------+
  |     employee| default|       null|  MANAGED|      false|
  |employeesdata|    null|       null|TEMPORARY|       true|
  +-------------+--------+-----------+---------+-----------+
  </code></pre>
  
  <p>UPDATE</p>
  
  <p>/usr/lib/hive/conf/hive-site.xml was not in the classpath so it was not reading the tables, after adding it in the classpath it worked fine ... Since I was running from IntelliJ I have this problem .. in production the spark-conf folder will  have link to hive-site.xml ...</p>