Java Code Examples for com.datastax.driver.core.BatchStatement#setConsistencyLevel()

The following examples show how to use com.datastax.driver.core.BatchStatement#setConsistencyLevel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SchemaInsert.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public boolean run() throws Exception
{
    List<BoundStatement> stmts = new ArrayList<>();
    partitionCount = partitions.size();

    for (PartitionIterator iterator : partitions)
        while (iterator.hasNext())
            stmts.add(bindRow(iterator.next()));

    rowCount += stmts.size();

    // 65535 is max number of stmts per batch, so if we have more, we need to manually batch them
    for (int j = 0 ; j < stmts.size() ; j += 65535)
    {
        List<BoundStatement> substmts = stmts.subList(j, Math.min(j + stmts.size(), j + 65535));
        Statement stmt;
        if (stmts.size() == 1)
        {
            stmt = substmts.get(0);
        }
        else
        {
            BatchStatement batch = new BatchStatement(batchType);
            batch.setConsistencyLevel(JavaDriverClient.from(cl));
            batch.addAll(substmts);
            stmt = batch;
        }

        try
        {
            validate(client.getSession().execute(stmt));
        }
        catch (ClassCastException e)
        {
            e.printStackTrace();
        }
    }
    return true;
}
 
Example 2
Source File: CassandraTable.java    From ingestion with Apache License 2.0 5 votes vote down vote up
public void save(final List<Event> events) {
  final BatchStatement batch = new BatchStatement();
  for (final Event event : events) {
    final Map<String, Object> parsedEvent = parse(event);
    if (parsedEvent.isEmpty()) {
      log.warn("Event {} could not be mapped. Suggestion: Cassandra is case sensitive, so maybe you can check field names.", event);
      continue;
    }
    if (!hasPrimaryKey(parsedEvent)) {
      break;
    }
    final Insert insert = QueryBuilder.insertInto(table);
    for (final Map.Entry<String, Object> entry : parsedEvent.entrySet()) {
      insert.value(entry.getKey(), entry.getValue());
    }
    if (log.isTraceEnabled()) {
      log.trace("Preparing insert for table {}: {}", table.getName(), insert.getQueryString());
    }
    batch.add(insert);
  }
  if (batch.getStatements().isEmpty()) {
    log.warn("No event produced insert query for table {}", table.getName());
    return;
  }
  batch.setConsistencyLevel(consistencyLevel);
  session.execute(batch);
}
 
Example 3
Source File: BatchHandler.java    From scalardb with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
void setConsistencyForConditionalMutation(BatchStatement statement) {
  statement.setConsistencyLevel(ConsistencyLevel.QUORUM); // for learn phase
  statement.setSerialConsistencyLevel(ConsistencyLevel.SERIAL); // for paxos phase
}