Search in sources :

Example 76 with ResultSet

use of com.datastax.driver.core.ResultSet in project ignite by apache.

the class CassandraSessionImpl method execute.

/**
 * {@inheritDoc}
 */
@Override
public <V> V execute(ExecutionAssistant<V> assistant) {
    int attempt = 0;
    Throwable error = null;
    String errorMsg = "Failed to execute Cassandra CQL statement: " + assistant.getStatement();
    RandomSleeper sleeper = newSleeper();
    incrementSessionRefs();
    try {
        while (attempt < CQL_EXECUTION_ATTEMPTS_COUNT) {
            error = null;
            if (attempt != 0) {
                log.warning("Trying " + (attempt + 1) + " attempt to execute Cassandra CQL statement: " + assistant.getStatement());
            }
            try {
                PreparedStatement preparedSt = prepareStatement(assistant.getTable(), assistant.getStatement(), assistant.getPersistenceSettings(), assistant.tableExistenceRequired());
                if (preparedSt == null)
                    return null;
                Statement statement = tuneStatementExecutionOptions(assistant.bindStatement(preparedSt));
                ResultSet res = session().execute(statement);
                Row row = res == null || !res.iterator().hasNext() ? null : res.iterator().next();
                return row == null ? null : assistant.process(row);
            } catch (Throwable e) {
                error = e;
                if (CassandraHelper.isTableAbsenceError(e)) {
                    if (!assistant.tableExistenceRequired()) {
                        log.warning(errorMsg, e);
                        return null;
                    }
                    handleTableAbsenceError(assistant.getTable(), assistant.getPersistenceSettings());
                } else if (CassandraHelper.isHostsAvailabilityError(e))
                    handleHostsAvailabilityError(e, attempt, errorMsg);
                else if (CassandraHelper.isPreparedStatementClusterError(e))
                    handlePreparedStatementClusterError(e);
                else
                    // For an error which we don't know how to handle, we will not try next attempts and terminate.
                    throw new IgniteException(errorMsg, e);
            }
            if (!CassandraHelper.isTableAbsenceError(error))
                sleeper.sleep();
            attempt++;
        }
    } catch (Throwable e) {
        error = e;
    } finally {
        decrementSessionRefs();
    }
    log.error(errorMsg, error);
    throw new IgniteException(errorMsg, error);
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement) IgniteException(org.apache.ignite.IgniteException) ResultSet(com.datastax.driver.core.ResultSet) RandomSleeper(org.apache.ignite.cache.store.cassandra.common.RandomSleeper) PreparedStatement(com.datastax.driver.core.PreparedStatement) Row(com.datastax.driver.core.Row)

Example 77 with ResultSet

use of com.datastax.driver.core.ResultSet in project ignite by apache.

the class CassandraSessionImplTest method executeFailureTest.

@SuppressWarnings("unchecked")
@Test
public void executeFailureTest() {
    Session session1 = mock(Session.class);
    Session session2 = mock(Session.class);
    when(session1.prepare(any(String.class))).thenReturn(preparedStatement1);
    when(session2.prepare(any(String.class))).thenReturn(preparedStatement2);
    ResultSetFuture rsFuture = mock(ResultSetFuture.class);
    ResultSet rs = mock(ResultSet.class);
    Iterator it = mock(Iterator.class);
    when(it.hasNext()).thenReturn(true);
    when(it.next()).thenReturn(mock(Row.class));
    when(rs.iterator()).thenReturn(it);
    when(rsFuture.getUninterruptibly()).thenReturn(rs);
    /* @formatter:off */
    when(session1.executeAsync(any(Statement.class))).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance")).thenThrow(new RuntimeException("this session should be refreshed / recreated"));
    when(session2.executeAsync(boundStatement1)).thenThrow(new InvalidQueryException("You may have used a PreparedStatement that was created with another Cluster instance"));
    when(session2.executeAsync(boundStatement2)).thenReturn(rsFuture);
    /* @formatter:on */
    Cluster cluster = mock(Cluster.class);
    when(cluster.connect()).thenReturn(session1).thenReturn(session2);
    when(session1.getCluster()).thenReturn(cluster);
    when(session2.getCluster()).thenReturn(cluster);
    Cluster.Builder builder = mock(Cluster.Builder.class);
    when(builder.build()).thenReturn(cluster);
    CassandraSessionImpl cassandraSession = new CassandraSessionImpl(builder, null, ConsistencyLevel.ONE, ConsistencyLevel.ONE, 0, mock(IgniteLogger.class));
    BatchExecutionAssistant<String, String> batchExecutionAssistant = new MyBatchExecutionAssistant();
    ArrayList<String> data = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        data.add(String.valueOf(i));
    }
    cassandraSession.execute(batchExecutionAssistant, data);
    verify(cluster, times(2)).connect();
    verify(session1, times(1)).prepare(any(String.class));
    verify(session2, times(1)).prepare(any(String.class));
    assertEquals(10, batchExecutionAssistant.processedCount());
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) ArrayList(java.util.ArrayList) Cluster(com.datastax.driver.core.Cluster) CassandraSessionImpl(org.apache.ignite.cache.store.cassandra.session.CassandraSessionImpl) ResultSet(com.datastax.driver.core.ResultSet) Iterator(java.util.Iterator) Row(com.datastax.driver.core.Row) IgniteLogger(org.apache.ignite.IgniteLogger) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 78 with ResultSet

use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.

the class AbstractUpsertOutputOperatorCompositePKTest method testForCompositeRowKeyBasedTable.

@Test
public void testForCompositeRowKeyBasedTable() throws Exception {
    CompositePrimaryKeyRow aCompositeRowKey = new CompositePrimaryKeyRow();
    String userId = new String("user1" + System.currentTimeMillis());
    String employeeId = new String(userId + System.currentTimeMillis());
    int day = 1;
    int month = 12;
    int year = 2017;
    aCompositeRowKey.setDay(day);
    aCompositeRowKey.setMonth(month);
    aCompositeRowKey.setYear(year);
    aCompositeRowKey.setCurrentstatus("status" + System.currentTimeMillis());
    aCompositeRowKey.setUserid(userId);
    aCompositeRowKey.setEmployeeid(employeeId);
    UpsertExecutionContext<CompositePrimaryKeyRow> anUpdate = new UpsertExecutionContext<>();
    anUpdate.setPayload(aCompositeRowKey);
    compositePrimaryKeysOperator.beginWindow(12);
    compositePrimaryKeysOperator.input.process(anUpdate);
    compositePrimaryKeysOperator.endWindow();
    ResultSet results = compositePrimaryKeysOperator.session.execute("SELECT * FROM unittests.userstatus WHERE userid = '" + userId + "' and day=" + day + " and month=" + month + " and year=" + year + " and employeeid='" + employeeId + "'");
    List<Row> rows = results.all();
    assertEquals(rows.size(), 1);
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 79 with ResultSet

use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.

the class AbstractUpsertOutputOperatorCountersTest method testForSingleRowInsertForCounterTables.

@Test
public void testForSingleRowInsertForCounterTables() throws Exception {
    CounterColumnTableEntry aCounterEntry = new CounterColumnTableEntry();
    String userId = new String("user1" + System.currentTimeMillis());
    aCounterEntry.setUserId(userId);
    aCounterEntry.setUpdatecount(3);
    UpsertExecutionContext<CounterColumnTableEntry> anUpdate = new UpsertExecutionContext<>();
    anUpdate.setOverridingConsistencyLevel(ConsistencyLevel.LOCAL_ONE);
    anUpdate.setPayload(aCounterEntry);
    counterUpdatesOperator.beginWindow(9);
    counterUpdatesOperator.input.process(anUpdate);
    counterUpdatesOperator.endWindow();
    ResultSet results = counterUpdatesOperator.session.execute("SELECT * FROM unittests.userupdates WHERE userid = '" + userId + "'");
    List<Row> rows = results.all();
    assertEquals(rows.size(), 1);
    assertEquals(3, rows.get(0).getLong("updatecount"));
    aCounterEntry = new CounterColumnTableEntry();
    aCounterEntry.setUserId(userId);
    aCounterEntry.setUpdatecount(2);
    anUpdate = new UpsertExecutionContext<>();
    anUpdate.setOverridingConsistencyLevel(ConsistencyLevel.LOCAL_ONE);
    anUpdate.setPayload(aCounterEntry);
    counterUpdatesOperator.beginWindow(10);
    counterUpdatesOperator.input.process(anUpdate);
    counterUpdatesOperator.endWindow();
    results = counterUpdatesOperator.session.execute("SELECT * FROM unittests.userupdates WHERE userid = '" + userId + "'");
    rows = results.all();
    assertEquals(5, rows.get(0).getLong("updatecount"));
    aCounterEntry = new CounterColumnTableEntry();
    aCounterEntry.setUserId(userId);
    aCounterEntry.setUpdatecount(-1);
    anUpdate = new UpsertExecutionContext<>();
    anUpdate.setOverridingConsistencyLevel(ConsistencyLevel.LOCAL_ONE);
    anUpdate.setPayload(aCounterEntry);
    counterUpdatesOperator.beginWindow(11);
    counterUpdatesOperator.input.process(anUpdate);
    counterUpdatesOperator.endWindow();
    results = counterUpdatesOperator.session.execute("SELECT * FROM unittests.userupdates WHERE userid = '" + userId + "'");
    rows = results.all();
    assertEquals(4, rows.get(0).getLong("updatecount"));
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) Test(org.junit.Test)

Example 80 with ResultSet

use of com.datastax.driver.core.ResultSet in project apex-malhar by apache.

the class CassandraOperatorTest method testupdateQueryWithParameters.

@Test
public void testupdateQueryWithParameters() throws InterruptedException {
    UUID id = UUID.fromString("94ab597c-a5ff-4997-8343-68993d446b14");
    TestPojo testPojo = new TestPojo(id, 20, "Laura", true, 10, 2.0, new HashSet<Integer>(), new ArrayList<Integer>(), null, new Date(System.currentTimeMillis()));
    String insert = "INSERT INTO " + KEYSPACE + "." + TABLE_NAME + " (ID, age, lastname, test, floatValue, doubleValue)" + " VALUES (94ab597c-a5ff-4997-8343-68993d446b14, 20, 'Laura', true, 10, 2.0);";
    session.execute(insert);
    String recordsQuery = "SELECT * from " + TABLE_NAME + ";";
    ResultSet resultSetRecords = session.execute(recordsQuery);
    Row row = resultSetRecords.iterator().next();
    Assert.assertEquals("Updated last name", "Laura", row.getString("lastname"));
    // wait till cassandra writes the record
    Thread.sleep(1000);
    // update record
    String updateLastName = "Laurel";
    String updateQuery = "update " + KEYSPACE + "." + TABLE_NAME + " set lastname='" + updateLastName + "' where id=?";
    // set specific files required by update command in order as per query
    List<FieldInfo> fieldInfos = Lists.newArrayList();
    fieldInfos.add(new FieldInfo("id", "id", null));
    // reset the operator to run new query
    TestOutputOperator outputOperator = setupForOutputOperatorTest();
    outputOperator.setQuery(updateQuery);
    outputOperator.setFieldInfos(fieldInfos);
    outputOperator.setup(context);
    outputOperator.input.setup(tpc);
    outputOperator.activate(context);
    outputOperator.beginWindow(1);
    outputOperator.input.process(testPojo);
    outputOperator.endWindow();
    recordsQuery = "SELECT * from " + TABLE_NAME + ";";
    resultSetRecords = session.execute(recordsQuery);
    row = resultSetRecords.iterator().next();
    Assert.assertEquals("Updated last name", updateLastName, row.getString("lastname"));
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) UUID(java.util.UUID) Date(java.util.Date) FieldInfo(org.apache.apex.malhar.lib.util.FieldInfo) Test(org.junit.Test)

Aggregations

ResultSet (com.datastax.driver.core.ResultSet)95 Row (com.datastax.driver.core.Row)61 Test (org.junit.Test)43 ArrayList (java.util.ArrayList)17 BoundStatement (com.datastax.driver.core.BoundStatement)13 Session (com.datastax.driver.core.Session)12 Cluster (com.datastax.driver.core.Cluster)10 Statement (com.datastax.driver.core.Statement)8 List (java.util.List)8 PreparedStatement (com.datastax.driver.core.PreparedStatement)7 ByteBuffer (java.nio.ByteBuffer)5 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)5 BatchStatement (com.datastax.driver.core.BatchStatement)4 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 Select (com.datastax.driver.core.querybuilder.Select)4 Tuple (io.vavr.Tuple)4 IOException (java.io.IOException)4 HashSet (java.util.HashSet)4 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)4 Entry (org.janusgraph.diskstorage.Entry)4