title: How to insert, update rows in database from Spark Dataframe
body: <p>I made Dataframe in Spark.</p>
  <p>The Dataframe has new rows and the same rows by key columns that table of database has.</p>
  <p>I need insert new rows and update existing rows.</p>
  <p>For example:</p>
  <p>Dataframe:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Key1</th>
  <th>Key2</th>
  <th>Value</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>11</td>
  <td>new value</td>
  </tr>
  <tr>
  <td>2</td>
  <td>22</td>
  <td>value</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Table in database:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Key1</th>
  <th>Key2</th>
  <th>Value</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>11</td>
  <td>old value</td>
  </tr>
  <tr>
  <td>3</td>
  <td>33</td>
  <td>other value</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I need to write my DataFrame into database and get next result:</p>
  <p>Table in database:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>Key1</th>
  <th>Key2</th>
  <th>Value</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>11</td>
  <td><strong>new value</strong></td>
  </tr>
  <tr>
  <td><strong>2</strong></td>
  <td><strong>22</strong></td>
  <td><strong>value</strong></td>
  </tr>
  <tr>
  <td>3</td>
  <td>33</td>
  <td>other value</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>Where</p>
  <p>(1, 11) was updated</p>
  <p>(2, 22) was inserted</p>
  <p>(3, 33) wasn't changed</p>
  <p>I guess there are two possible solutions:</p>
  <ol>
  <li>Merge data in new DataFrame and fully rewrite table in Database</li>
  <li>Or insert/update data from DataFrame directly into Database</li>
  </ol>
  <p>I have no idea how to proceed with this. Which instruments to use for this task? Help me to understand in which direction to move.</p>