Search in sources :

Example 21 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project aroma-data-operations by RedRoma.

the class CassandraUserRepository method createStatementToSaveUser.

private Statement createStatementToSaveUser(User user) {
    BatchStatement batchStatement = new BatchStatement(BatchStatement.Type.LOGGED);
    Statement insertIntoUsersTable = createInsertIntoUserTable(user);
    batchStatement.add(insertIntoUsersTable);
    Statement insertIntoUsersByEmailTable = createInsertIntoUsersByEmailTable(user);
    batchStatement.add(insertIntoUsersByEmailTable);
    if (!isNullOrEmpty(user.githubProfile)) {
        Statement insertIntoUsersByGithubTable = createInsertIntoUsersByGithubTable(user);
        batchStatement.add(insertIntoUsersByGithubTable);
    }
    Statement insertIntoRecentUsersTable = createInsertIntoRecentUsersTable(user);
    batchStatement.add(insertIntoRecentUsersTable);
    return batchStatement;
}
Also used : BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement)

Example 22 with BatchStatement

use of com.datastax.driver.core.BatchStatement in project pinpoint by naver.

the class CassandraDatastaxITBase method testBatchStatement_and_StatementWrapper.

@Test
public void testBatchStatement_and_StatementWrapper() throws Exception {
    final String testId1 = "998";
    final String testValue1 = "testValue998";
    final String testId2 = "999";
    final String testValue2 = "testValue999";
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    try (Session session = openSession(cluster)) {
        // ===============================================
        // Insert Data 2 x (PreparedStatement, BoundStatement)
        PreparedStatement preparedStatement = session.prepare(CQL_INSERT);
        BoundStatement boundStatement1 = new BoundStatement(preparedStatement);
        boundStatement1.bind(testId1, testValue1);
        BoundStatement boundStatement2 = new BoundStatement(preparedStatement);
        boundStatement2.bind(testId2, testValue2);
        BatchStatement batchStatement = new BatchStatement();
        batchStatement.add(boundStatement1);
        batchStatement.add(boundStatement2);
        session.execute(batchStatement);
        verifier.printCache();
        // Cluster#connect(String)
        Class<?> clusterClass = Class.forName("com.datastax.driver.core.Cluster");
        Method connect = clusterClass.getDeclaredMethod("connect", String.class);
        verifier.verifyTrace(event(CASSANDRA, connect, null, CASSANDRA_ADDRESS, TEST_KEYSPACE));
        // SessionManager#prepare(String) OR AbstractSession#prepare(String)
        Class<?> sessionClass;
        try {
            sessionClass = Class.forName("com.datastax.driver.core.AbstractSession");
        } catch (ClassNotFoundException e) {
            sessionClass = Class.forName("com.datastax.driver.core.SessionManager");
        }
        Method prepare = sessionClass.getDeclaredMethod("prepare", String.class);
        verifier.verifyTrace(event(CASSANDRA, prepare, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(CQL_INSERT, null)));
        // SessionManager#execute(Statement) OR AbstractSession#execute(Statement)
        Method execute = sessionClass.getDeclaredMethod("execute", Statement.class);
        verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE));
        // ====================
        final String cqlDelete = String.format("DELETE FROM %s.%s WHERE %s IN (? , ?)", TEST_KEYSPACE, TEST_TABLE, TEST_COL_ID);
        PreparedStatement deletePreparedStatement = session.prepare(cqlDelete);
        BoundStatement deleteBoundStatement = new BoundStatement(deletePreparedStatement);
        deleteBoundStatement.bind(testId1, testId2);
        Statement wrappedDeleteStatement = new StatementWrapper(deleteBoundStatement) {
        };
        session.execute(wrappedDeleteStatement);
        verifier.printCache();
        // SessionManager#prepare(String) OR AbstractSession#prepare(String)
        verifier.verifyTrace(event(CASSANDRA, prepare, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(cqlDelete, null)));
        // SessionManager#execute(String, Object[]) OR AbstractSession#execute(String, Object[])
        verifier.verifyTrace(event(CASSANDRA_EXECUTE_QUERY, execute, null, CASSANDRA_ADDRESS, TEST_KEYSPACE, sql(cqlDelete, null)));
    }
}
Also used : StatementWrapper(com.datastax.driver.core.StatementWrapper) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) BatchStatement(com.datastax.driver.core.BatchStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) Method(java.lang.reflect.Method) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) BoundStatement(com.datastax.driver.core.BoundStatement) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

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