Search in sources :

Example 11 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project cassandra by apache.

the class BatchTests method testOversizedBatch.

@Test(expected = InvalidQueryException.class)
public void testOversizedBatch() {
    int SIZE_FOR_FAILURE = 2500;
    BatchStatement b = new BatchStatement(BatchStatement.Type.UNLOGGED);
    for (int i = 0; i < SIZE_FOR_FAILURE; i++) {
        b.add(noncounter.bind(i, "foobar"));
    }
    session.execute(b);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) Test(org.junit.Test)

Example 12 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project cassandra by apache.

the class BatchTests method sendBatch.

public void sendBatch(BatchStatement.Type type, boolean addCounter, boolean addNonCounter, boolean addClustering) {
    assert addCounter || addNonCounter || addClustering;
    BatchStatement b = new BatchStatement(type);
    for (int i = 0; i < 10; i++) {
        if (addNonCounter)
            b.add(noncounter.bind(i, "foo"));
        if (addCounter)
            b.add(counter.bind((long) i, i));
        if (addClustering) {
            b.add(clustering.bind(i, i, i, i, "foo"));
        }
    }
    session.execute(b);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement)

Example 13 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project cassandra by apache.

the class AuditLoggerTest method testCqlBatch_MultipleTablesAuditing.

@Test
public void testCqlBatch_MultipleTablesAuditing() {
    createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    String table1 = currentTable();
    Session session = sessionNet();
    BatchStatement batchStatement = new BatchStatement();
    String cqlInsert1 = "INSERT INTO " + KEYSPACE + "." + table1 + " (id, v1, v2) VALUES (?, ?, ?)";
    PreparedStatement prep = session.prepare(cqlInsert1);
    AuditLogEntry logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert1, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
    createTable("CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    String table2 = currentTable();
    String cqlInsert2 = "INSERT INTO " + KEYSPACE + "." + table2 + " (id, v1, v2) VALUES (?, ?, ?)";
    prep = session.prepare(cqlInsert2);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert2, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false);
    batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
    createKeyspace("CREATE KEYSPACE %s WITH replication={ 'class' : 'SimpleStrategy', 'replication_factor' : 1 }");
    String ks2 = currentKeyspace();
    createTable(ks2, "CREATE TABLE %s (id int primary key, v1 text, v2 text)");
    String table3 = currentTable();
    String cqlInsert3 = "INSERT INTO " + ks2 + "." + table3 + " (id, v1, v2) VALUES (?, ?, ?)";
    prep = session.prepare(cqlInsert3);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert3, AuditLogEntryType.PREPARE_STATEMENT, logEntry, false, ks2);
    batchStatement.add(prep.bind(1, "Apapche", "Cassandra"));
    ResultSet rs = session.execute(batchStatement);
    assertEquals(4, ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.size());
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert1, table1, AuditLogEntryType.UPDATE, logEntry, false, KEYSPACE);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert2, table2, AuditLogEntryType.UPDATE, logEntry, false, KEYSPACE);
    logEntry = ((InMemoryAuditLogger) AuditLogManager.instance.getLogger()).inMemQueue.poll();
    assertLogEntry(cqlInsert3, table3, AuditLogEntryType.UPDATE, logEntry, false, ks2);
    int size = rs.all().size();
    assertEquals(0, size);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) ResultSet(com.datastax.driver.core.ResultSet) PreparedStatement(com.datastax.driver.core.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 14 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project cassandra by apache.

the class TableMetricsTest method executeBatch.

private void executeBatch(boolean isLogged, int distinctPartitions, int statementsPerPartition, String... tables) {
    if (tables == null || tables.length == 0) {
        tables = new String[] { TABLE };
    }
    BatchStatement.Type batchType;
    if (isLogged) {
        batchType = BatchStatement.Type.LOGGED;
    } else {
        batchType = BatchStatement.Type.UNLOGGED;
    }
    BatchStatement batch = new BatchStatement(batchType);
    for (String table : tables) populateBatch(batch, table, distinctPartitions, statementsPerPartition);
    session.execute(batch);
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement)

Example 15 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project storm by apache.

the class CassandraState method updateState.

public void updateState(List<TridentTuple> tuples, final TridentCollector collector) {
    List<Statement> statements = new ArrayList<>();
    for (TridentTuple tuple : tuples) {
        statements.addAll(options.cqlStatementTupleMapper.map(conf, session, tuple));
    }
    try {
        if (options.batchingType != null) {
            BatchStatement batchStatement = new BatchStatement(options.batchingType);
            batchStatement.addAll(statements);
            session.execute(batchStatement);
        } else {
            for (Statement statement : statements) {
                session.execute(statement);
            }
        }
    } catch (Exception e) {
        LOG.warn("Batch write operation is failed.");
        collector.reportError(e);
        throw new FailedException(e);
    }
}
Also used : Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) FailedException(org.apache.storm.topology.FailedException) BatchStatement(com.datastax.driver.core.BatchStatement) ArrayList(java.util.ArrayList) FailedException(org.apache.storm.topology.FailedException) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Aggregations

BatchStatement (com.datastax.driver.core.BatchStatement)22 Statement (com.datastax.driver.core.Statement)11 UUID (java.util.UUID)7 PreparedStatement (com.datastax.driver.core.PreparedStatement)5 Session (com.datastax.driver.core.Session)4 Test (org.junit.Test)4 ResultSet (com.datastax.driver.core.ResultSet)3 StringAssertions.validUUID (tech.sirwellington.alchemy.arguments.assertions.StringAssertions.validUUID)3 BoundStatement (com.datastax.driver.core.BoundStatement)2 StringAssertions.nonEmptyString (tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString)2 Type (com.datastax.driver.core.BatchStatement.Type)1 Cluster (com.datastax.driver.core.Cluster)1 Builder (com.datastax.driver.core.Cluster.Builder)1 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)1 HostDistance (com.datastax.driver.core.HostDistance)1 JdkSSLOptions (com.datastax.driver.core.JdkSSLOptions)1 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)1 PoolingOptions (com.datastax.driver.core.PoolingOptions)1 ProtocolVersion (com.datastax.driver.core.ProtocolVersion)1 SimpleStatement (com.datastax.driver.core.SimpleStatement)1