Search in sources :

Example 16 with ResultSet

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

the class ViewTest method testPrimaryKeyOnlyTable.

@Test
public void testPrimaryKeyOnlyTable() throws Throwable {
    createTable("CREATE TABLE %s (" + "a int," + "b int," + "PRIMARY KEY (a, b))");
    executeNet(protocolVersion, "USE " + keyspace());
    // Cannot use SELECT *, as those are always handled by the includeAll shortcut in View.updateAffectsView
    createView("mv1", "CREATE MATERIALIZED VIEW %s AS SELECT a, b FROM %%s WHERE b IS NOT NULL PRIMARY KEY (b, a)");
    updateView("INSERT INTO %s (a, b) VALUES (?, ?)", 1, 1);
    ResultSet mvRows = executeNet(protocolVersion, "SELECT a, b FROM mv1");
    assertRowsNet(protocolVersion, mvRows, row(1, 1));
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Test(org.junit.Test)

Example 17 with ResultSet

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

the class ViewTest method testDeleteSingleColumnInViewClustering.

@Test
public void testDeleteSingleColumnInViewClustering() throws Throwable {
    createTable("CREATE TABLE %s (" + "a int," + "b int," + "c int," + "d int," + "PRIMARY KEY (a, b))");
    executeNet(protocolVersion, "USE " + keyspace());
    createView("mv1", "CREATE MATERIALIZED VIEW %s AS SELECT * FROM %%s WHERE a IS NOT NULL AND b IS NOT NULL AND d IS NOT NULL PRIMARY KEY (a, d, b)");
    updateView("INSERT INTO %s (a, b, c, d) VALUES (?, ?, ?, ?)", 0, 0, 0, 0);
    ResultSet mvRows = executeNet(protocolVersion, "SELECT a, d, b, c FROM mv1");
    assertRowsNet(protocolVersion, mvRows, row(0, 0, 0, 0));
    updateView("DELETE c FROM %s WHERE a = ? AND b = ?", 0, 0);
    mvRows = executeNet(protocolVersion, "SELECT a, d, b, c FROM mv1");
    assertRowsNet(protocolVersion, mvRows, row(0, 0, 0, null));
    updateView("DELETE d FROM %s WHERE a = ? AND b = ?", 0, 0);
    mvRows = executeNet(protocolVersion, "SELECT a, d, b FROM mv1");
    assertTrue(mvRows.isExhausted());
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) Test(org.junit.Test)

Example 18 with ResultSet

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

the class CassandraConnectorITCase method testCassandraTupleAtLeastOnceSink.

// ------------------------------------------------------------------------
//  At-least-once Tests
// ------------------------------------------------------------------------
@Test
public void testCassandraTupleAtLeastOnceSink() throws Exception {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setParallelism(1);
    DataStream<Tuple3<String, Integer, Integer>> source = env.fromCollection(collection);
    source.addSink(new CassandraTupleSink<Tuple3<String, Integer, Integer>>(INSERT_DATA_QUERY, builder));
    env.execute();
    ResultSet rs = session.execute(SELECT_DATA_QUERY);
    Assert.assertEquals(20, rs.all().size());
}
Also used : Tuple3(org.apache.flink.api.java.tuple.Tuple3) ResultSet(com.datastax.driver.core.ResultSet) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with ResultSet

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

the class CassandraConnectorITCase method verifyResultsDataPersistenceUponMissedNotify.

@Override
protected void verifyResultsDataPersistenceUponMissedNotify(CassandraTupleWriteAheadSink<Tuple3<String, Integer, Integer>> sink) {
    ResultSet result = session.execute(SELECT_DATA_QUERY);
    ArrayList<Integer> list = new ArrayList<>();
    for (int x = 1; x <= 60; x++) {
        list.add(x);
    }
    for (Row s : result) {
        list.remove(new Integer(s.getInt("counter")));
    }
    Assert.assertTrue("The following ID's were not found in the ResultSet: " + list.toString(), list.isEmpty());
}
Also used : ResultSet(com.datastax.driver.core.ResultSet) ArrayList(java.util.ArrayList) Row(com.datastax.driver.core.Row) TypeHint(org.apache.flink.api.common.typeinfo.TypeHint)

Example 20 with ResultSet

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

the class CassandraTupleWriteAheadSink method sendValues.

@Override
protected boolean sendValues(Iterable<IN> values, long timestamp) throws Exception {
    final AtomicInteger updatesCount = new AtomicInteger(0);
    final AtomicInteger updatesConfirmed = new AtomicInteger(0);
    final AtomicReference<Throwable> exception = new AtomicReference<>();
    FutureCallback<ResultSet> callback = new FutureCallback<ResultSet>() {

        @Override
        public void onSuccess(ResultSet resultSet) {
            updatesConfirmed.incrementAndGet();
            if (updatesCount.get() > 0) {
                // only set if all updates have been sent
                if (updatesCount.get() == updatesConfirmed.get()) {
                    synchronized (updatesConfirmed) {
                        updatesConfirmed.notifyAll();
                    }
                }
            }
        }

        @Override
        public void onFailure(Throwable throwable) {
            if (exception.compareAndSet(null, throwable)) {
                LOG.error("Error while sending value.", throwable);
                synchronized (updatesConfirmed) {
                    updatesConfirmed.notifyAll();
                }
            }
        }
    };
    //set values for prepared statement
    int updatesSent = 0;
    for (IN value : values) {
        for (int x = 0; x < value.getArity(); x++) {
            fields[x] = value.getField(x);
        }
        //insert values and send to cassandra
        BoundStatement s = preparedStatement.bind(fields);
        s.setDefaultTimestamp(timestamp);
        ResultSetFuture result = session.executeAsync(s);
        updatesSent++;
        if (result != null) {
            //add callback to detect errors
            Futures.addCallback(result, callback);
        }
    }
    updatesCount.set(updatesSent);
    synchronized (updatesConfirmed) {
        while (exception.get() == null && updatesSent != updatesConfirmed.get()) {
            updatesConfirmed.wait();
        }
    }
    if (exception.get() != null) {
        LOG.warn("Sending a value failed.", exception.get());
        return false;
    } else {
        return true;
    }
}
Also used : ResultSetFuture(com.datastax.driver.core.ResultSetFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ResultSet(com.datastax.driver.core.ResultSet) AtomicReference(java.util.concurrent.atomic.AtomicReference) BoundStatement(com.datastax.driver.core.BoundStatement) FutureCallback(com.google.common.util.concurrent.FutureCallback)

Aggregations

ResultSet (com.datastax.driver.core.ResultSet)64 Row (com.datastax.driver.core.Row)35 Test (org.junit.Test)25 BoundStatement (com.datastax.driver.core.BoundStatement)11 Session (com.datastax.driver.core.Session)10 ArrayList (java.util.ArrayList)9 Cluster (com.datastax.driver.core.Cluster)8 Statement (com.datastax.driver.core.Statement)7 PreparedStatement (com.datastax.driver.core.PreparedStatement)5 List (java.util.List)5 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 Select (com.datastax.driver.core.querybuilder.Select)4 TypeHint (org.apache.flink.api.common.typeinfo.TypeHint)4 BatchStatement (com.datastax.driver.core.BatchStatement)3 RegularStatement (com.datastax.driver.core.RegularStatement)3 Update (com.datastax.driver.core.querybuilder.Update)3 ImmutableList (com.google.common.collect.ImmutableList)3 IgniteException (org.apache.ignite.IgniteException)3 RandomSleeper (org.apache.ignite.cache.store.cassandra.common.RandomSleeper)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)2