title: How to use Asynchronous/Batch writes feature with Datastax Java driver
body: <p>I am planning to use Datastax Java driver for writing to Cassandra.. I was mainly interested in <code>Batch Writes</code> and <code>Asycnhronous</code> features of Datastax java driver but I am not able to get any tutorials which can explain me how to incorporate these features in my below code which uses Datastax Java driver..</p>
  
  <pre><code>/**
   * Performs an upsert of the specified attributes for the specified id.
   */
  public void upsertAttributes(final String userId, final Map&lt;String, String&gt; attributes, final String columnFamily) {
  
      try {
  
          // make a sql here using the above input parameters.
  
          String sql = sqlPart1.toString()+sqlPart2.toString();
  
          DatastaxConnection.getInstance();
          PreparedStatement prepStatement = DatastaxConnection.getSession().prepare(sql);
          prepStatement.setConsistencyLevel(ConsistencyLevel.ONE);        
  
          BoundStatement query = prepStatement.bind(userId, attributes.values().toArray(new Object[attributes.size()]));
  
          DatastaxConnection.getSession().execute(query);
  
      } catch (InvalidQueryException e) {
          LOG.error("Invalid Query Exception in DatastaxClient::upsertAttributes "+e);
      } catch (Exception e) {
          LOG.error("Exception in DatastaxClient::upsertAttributes "+e);
      }
  }
  </code></pre>
  
  <p>In the below code, I am creating a Connection to Cassandra nodes using Datastax Java driver.    </p>
  
  <pre><code>/**
   * Creating Cassandra connection using Datastax Java driver
   *
   */
  private DatastaxConnection() {
  
      try{
          builder = Cluster.builder();
          builder.addContactPoint("some_nodes");
  
          builder.poolingOptions().setCoreConnectionsPerHost(
                  HostDistance.LOCAL,
                  builder.poolingOptions().getMaxConnectionsPerHost(HostDistance.LOCAL));
  
          cluster = builder
                  .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
                  .withReconnectionPolicy(new ConstantReconnectionPolicy(100L))
                  .build();
  
          StringBuilder s = new StringBuilder();
          Set&lt;Host&gt; allHosts = cluster.getMetadata().getAllHosts();
          for (Host h : allHosts) {
              s.append("[");
              s.append(h.getDatacenter());
              s.append(h.getRack());
              s.append(h.getAddress());
              s.append("]");
          }
          System.out.println("Cassandra Cluster: " + s.toString());
  
          session = cluster.connect("testdatastaxks");
  
      } catch (NoHostAvailableException e) {
          e.printStackTrace();
          throw new RuntimeException(e);
      } catch (Exception e) {
  
      }
  }
  </code></pre>
  
  <p>Can anybody help me on how to add Batch writes or Asynchronous features to my above code.. Thanks for the help..</p>
  
  <p>I am running Cassandra 1.2.9</p>