Search in sources :

Example 6 with SimpleStatement

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

the class NativeProtocolTest method withCounters.

@Test
public void withCounters() throws Throwable {
    try (ICluster ignored = init(builder().withNodes(3).withConfig(config -> config.with(GOSSIP, NETWORK, NATIVE_PROTOCOL)).start())) {
        final com.datastax.driver.core.Cluster cluster = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").build();
        Session session = cluster.connect();
        session.execute("CREATE TABLE " + KEYSPACE + ".tbl (pk int, ck counter, PRIMARY KEY (pk));");
        session.execute("UPDATE " + KEYSPACE + ".tbl set ck = ck + 10 where pk = 1;");
        Statement select = new SimpleStatement("select * from " + KEYSPACE + ".tbl;").setConsistencyLevel(ConsistencyLevel.ALL);
        final ResultSet resultSet = session.execute(select);
        assertRows(RowUtil.toObjects(resultSet), row(1, 10L));
        Assert.assertEquals(3, cluster.getMetadata().getAllHosts().size());
        session.close();
        cluster.close();
    }
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement) CQLStatement(org.apache.cassandra.cql3.CQLStatement) Statement(com.datastax.driver.core.Statement) SimpleStatement(com.datastax.driver.core.SimpleStatement) ICluster(org.apache.cassandra.distributed.api.ICluster) ResultSet(com.datastax.driver.core.ResultSet) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Example 7 with SimpleStatement

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

the class DescribeStatementTest method testSchemaChangeDuringPaging.

@Test
public void testSchemaChangeDuringPaging() {
    SimpleStatement stmt = new SimpleStatement("DESCRIBE KEYSPACES");
    stmt.setFetchSize(1);
    ResultSet rs = executeNet(ProtocolVersion.CURRENT, stmt);
    Iterator<Row> iter = rs.iterator();
    assertTrue(iter.hasNext());
    iter.next();
    createKeyspace("CREATE KEYSPACE %s WITH REPLICATION = {'class' : 'SimpleStrategy', 'replication_factor' : 1};");
    try {
        iter.next();
        fail("Expected InvalidQueryException");
    } catch (InvalidQueryException e) {
        assertEquals(DescribeStatement.SCHEMA_CHANGED_WHILE_PAGING_MESSAGE, e.getMessage());
    }
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Test(org.junit.Test)

Example 8 with SimpleStatement

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

the class SimpleCQLStatementMapper method map.

/**
 * {@inheritDoc}.
 */
@Override
public List<Statement> map(Map<String, Object> conf, Session session, ITuple tuple) {
    List<Column> columns = mapper.map(tuple);
    SimpleStatement statement = new SimpleStatement(queryString, Column.getVals(columns));
    if (hasRoutingKeys()) {
        List<ByteBuffer> keys = rkGenerator.getRoutingKeys(tuple);
        if (keys.size() == 1) {
            statement.setRoutingKey(keys.get(0));
        } else {
            statement.setRoutingKey(keys.toArray(new ByteBuffer[keys.size()]));
        }
    }
    return Arrays.asList((Statement) statement);
}
Also used : Column(org.apache.storm.cassandra.query.Column) SimpleStatement(com.datastax.driver.core.SimpleStatement) ByteBuffer(java.nio.ByteBuffer)

Example 9 with SimpleStatement

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

the class FQLReplayTest method compareStatements.

private void compareStatements(Statement statement1, Statement statement2) {
    assertTrue(statement1 instanceof SimpleStatement && statement2 instanceof SimpleStatement);
    SimpleStatement simpleStmt1 = (SimpleStatement) statement1;
    SimpleStatement simpleStmt2 = (SimpleStatement) statement2;
    assertEquals(simpleStmt1.getQueryString(CodecRegistry.DEFAULT_INSTANCE), simpleStmt2.getQueryString(CodecRegistry.DEFAULT_INSTANCE));
    assertArrayEquals(simpleStmt1.getValues(com.datastax.driver.core.ProtocolVersion.fromInt(QueryOptions.DEFAULT.getProtocolVersion().asInt()), CodecRegistry.DEFAULT_INSTANCE), simpleStmt2.getValues(com.datastax.driver.core.ProtocolVersion.fromInt(QueryOptions.DEFAULT.getProtocolVersion().asInt()), CodecRegistry.DEFAULT_INSTANCE));
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement)

Example 10 with SimpleStatement

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

the class SASICQLTest method testBlobCompare.

/**
 * Tests query condition '>' on blob columns.
 */
@Test
public void testBlobCompare() throws Throwable {
    for (String mode : new String[] { "PREFIX", "CONTAINS", "SPARSE" }) {
        for (boolean forceFlush : new boolean[] { false, true }) {
            try {
                createTable("CREATE TABLE %s (pk int primary key, v blob);");
                createIndex(String.format("CREATE CUSTOM INDEX ON %%s (v) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS = {'mode': '%s'};", mode));
                execute("INSERT INTO %s (pk, v) VALUES (?, ?);", 0, 0x1234);
                execute("INSERT INTO %s (pk, v) VALUES (?, ?);", 1, 0x12345678);
                execute("INSERT INTO %s (pk, v) VALUES (?, ?);", 2, 0x12350000);
                flush(forceFlush);
                Session session = sessionNet();
                SimpleStatement stmt = new SimpleStatement("SELECT * FROM " + KEYSPACE + '.' + currentTable() + " WHERE v > 0x1234");
                stmt.setFetchSize(5);
                List<Row> rs = session.execute(stmt).all();
                Assert.assertFalse("CONTAINS mode on non-literal blob type should not support RANGE operators", "CONTAINS".equals(mode));
                Assert.assertEquals(2, rs.size());
                Assert.assertEquals(1, rs.get(0).getInt("pk"));
            } catch (InvalidQueryException ex) {
                if (!"CONTAINS".equals(mode))
                    throw ex;
            } catch (Throwable th) {
                throw new AssertionError(String.format("Failure with mode:%s and flush:%s ", mode, forceFlush), th);
            }
        }
    }
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement) Row(com.datastax.driver.core.Row) InvalidQueryException(com.datastax.driver.core.exceptions.InvalidQueryException) Session(com.datastax.driver.core.Session) Test(org.junit.Test)

Aggregations

SimpleStatement (com.datastax.driver.core.SimpleStatement)38 ResultSet (com.datastax.driver.core.ResultSet)17 Row (com.datastax.driver.core.Row)17 Test (org.junit.Test)15 Session (com.datastax.driver.core.Session)13 Statement (com.datastax.driver.core.Statement)10 ArrayList (java.util.ArrayList)10 CassandraResultSet (org.apache.gora.cassandra.query.CassandraResultSet)7 GoraException (org.apache.gora.util.GoraException)7 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)6 ByteBuffer (java.nio.ByteBuffer)5 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)4 AbstractGettableData (com.datastax.driver.core.AbstractGettableData)3 BatchStatement (com.datastax.driver.core.BatchStatement)2 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)2 DriverException (com.datastax.driver.core.exceptions.DriverException)2 UnavailableException (com.datastax.driver.core.exceptions.UnavailableException)2 CQLStatement (org.apache.cassandra.cql3.CQLStatement)2 BatchStatement (org.apache.cassandra.cql3.statements.BatchStatement)2 ICluster (org.apache.cassandra.distributed.api.ICluster)2