Search in sources :

Example 1 with SimpleStatement

use of com.datastax.driver.core.SimpleStatement in project YCSB by brianfrankcooper.

the class CassandraCQLClient method scan.

/**
   * Perform a range scan for a set of records in the database. Each field/value
   * pair from the result will be stored in a HashMap.
   *
   * Cassandra CQL uses "token" method for range scan which doesn't always yield
   * intuitive results.
   *
   * @param table
   *          The name of the table
   * @param startkey
   *          The record key of the first record to read.
   * @param recordcount
   *          The number of records to read
   * @param fields
   *          The list of fields to read, or null for all of them
   * @param result
   *          A Vector of HashMaps, where each HashMap is a set field/value
   *          pairs for one record
   * @return Zero on success, a non-zero error code on error
   */
@Override
public Status scan(String table, String startkey, int recordcount, Set<String> fields, Vector<HashMap<String, ByteIterator>> result) {
    try {
        Statement stmt;
        Select.Builder selectBuilder;
        if (fields == null) {
            selectBuilder = QueryBuilder.select().all();
        } else {
            selectBuilder = QueryBuilder.select();
            for (String col : fields) {
                ((Select.Selection) selectBuilder).column(col);
            }
        }
        stmt = selectBuilder.from(table);
        // The statement builder is not setup right for tokens.
        // So, we need to build it manually.
        String initialStmt = stmt.toString();
        StringBuilder scanStmt = new StringBuilder();
        scanStmt.append(initialStmt.substring(0, initialStmt.length() - 1));
        scanStmt.append(" WHERE ");
        scanStmt.append(QueryBuilder.token(YCSB_KEY));
        scanStmt.append(" >= ");
        scanStmt.append("token('");
        scanStmt.append(startkey);
        scanStmt.append("')");
        scanStmt.append(" LIMIT ");
        scanStmt.append(recordcount);
        stmt = new SimpleStatement(scanStmt.toString());
        stmt.setConsistencyLevel(readConsistencyLevel);
        if (debug) {
            System.out.println(stmt.toString());
        }
        if (trace) {
            stmt.enableTracing();
        }
        ResultSet rs = session.execute(stmt);
        HashMap<String, ByteIterator> tuple;
        while (!rs.isExhausted()) {
            Row row = rs.one();
            tuple = new HashMap<String, ByteIterator>();
            ColumnDefinitions cd = row.getColumnDefinitions();
            for (ColumnDefinitions.Definition def : cd) {
                ByteBuffer val = row.getBytesUnsafe(def.getName());
                if (val != null) {
                    tuple.put(def.getName(), new ByteArrayByteIterator(val.array()));
                } else {
                    tuple.put(def.getName(), null);
                }
            }
            result.add(tuple);
        }
        return Status.OK;
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error scanning with startkey: " + startkey);
        return Status.ERROR;
    }
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) SimpleStatement(com.datastax.driver.core.SimpleStatement) Statement(com.datastax.driver.core.Statement) SimpleStatement(com.datastax.driver.core.SimpleStatement) ByteBuffer(java.nio.ByteBuffer) DBException(com.yahoo.ycsb.DBException) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) ByteIterator(com.yahoo.ycsb.ByteIterator) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row)

Example 2 with SimpleStatement

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

the class AbstractCassandraInputOperator method emitTuples.

/**
 * This executes the query to retrieve result from database.
 * It then converts each row into tuple and emit that into output port.
 */
@Override
public void emitTuples() {
    String query = queryToRetrieveData();
    logger.debug("select statement: {}", query);
    SimpleStatement stmt = new SimpleStatement(query);
    stmt.setFetchSize(fetchSize);
    try {
        if (nextPageState != null) {
            stmt.setPagingState(nextPageState);
        }
        ResultSet result = store.getSession().execute(stmt);
        nextPageState = result.getExecutionInfo().getPagingState();
        if (!result.isExhausted()) {
            for (Row row : result) {
                T tuple = getTuple(row);
                emit(tuple);
            }
        } else {
            // No rows available wait for some time before retrying so as to not continuously slam the database
            Thread.sleep(waitForDataTimeout);
        }
    } catch (Exception ex) {
        store.disconnect();
        DTThrowable.rethrow(ex);
    }
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row)

Example 3 with SimpleStatement

use of com.datastax.driver.core.SimpleStatement in project java-driver by datastax.

the class DowngradingRetry method read.

/**
 * Queries data, retrying if necessary with a downgraded CL.
 *
 * @param cl         the consistency level to apply.
 * @param retryCount the current retry count.
 * @throws DriverException if the current consistency level cannot be downgraded.
 */
private ResultSet read(ConsistencyLevel cl, int retryCount) {
    System.out.printf("Reading at %s (retry count: %d)%n", cl, retryCount);
    Statement stmt = new SimpleStatement("SELECT sensor_id, date, timestamp, value " + "FROM downgrading.sensor_data " + "WHERE " + "sensor_id = 756716f7-2e54-4715-9f00-91dcbea6cf50 AND " + "date = '2018-02-26' AND " + "timestamp > '2018-02-26+01:00'").setConsistencyLevel(cl);
    try {
        ResultSet rows = session.execute(stmt);
        System.out.println("Read succeeded at " + cl);
        return rows;
    } catch (DriverException e) {
        if (retryCount == maxRetries) {
            throw e;
        }
        e = unwrapNoHostAvailableException(e);
        System.out.println("Read failed: " + e);
        if (e instanceof UnavailableException) {
            // Downgrade to the number of replicas reported alive and retry.
            int aliveReplicas = ((UnavailableException) e).getAliveReplicas();
            ConsistencyLevel downgraded = downgrade(cl, aliveReplicas, e);
            return read(downgraded, retryCount + 1);
        } else if (e instanceof ReadTimeoutException) {
            ReadTimeoutException readTimeout = (ReadTimeoutException) e;
            int received = readTimeout.getReceivedAcknowledgements();
            int required = readTimeout.getRequiredAcknowledgements();
            // equal to the number of received acknowledgements.
            if (received < required) {
                ConsistencyLevel downgraded = downgrade(cl, received, e);
                return read(downgraded, retryCount + 1);
            }
            // and get the data back.
            if (!readTimeout.wasDataRetrieved()) {
                return read(cl, retryCount + 1);
            }
            // Otherwise, abort since the read timeout is unlikely to be solved by a retry.
            throw e;
        } else {
            // and hope to talk to a healthier coordinator.
            return read(cl, retryCount + 1);
        }
    }
}
Also used : ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) SimpleStatement(com.datastax.driver.core.SimpleStatement) Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) SimpleStatement(com.datastax.driver.core.SimpleStatement) ReadTimeoutException(com.datastax.driver.core.exceptions.ReadTimeoutException) ResultSet(com.datastax.driver.core.ResultSet) UnavailableException(com.datastax.driver.core.exceptions.UnavailableException) DriverException(com.datastax.driver.core.exceptions.DriverException)

Example 4 with SimpleStatement

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

the class IndexQueryPagingTest method executePagingQuery.

private void executePagingQuery(String cql, int rowCount) {
    // Execute an index query which should return all rows,
    // setting the fetch size < than the row count. Assert
    // that all rows are returned, so we know that paging
    // of the results was involved.
    Session session = sessionNet();
    Statement stmt = new SimpleStatement(String.format(cql, KEYSPACE + '.' + currentTable()));
    stmt.setFetchSize(rowCount - 1);
    assertEquals(rowCount, session.execute(stmt).all().size());
}
Also used : SimpleStatement(com.datastax.driver.core.SimpleStatement) Statement(com.datastax.driver.core.Statement) SimpleStatement(com.datastax.driver.core.SimpleStatement) Session(com.datastax.driver.core.Session)

Example 5 with SimpleStatement

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

the class GroupByTest method testGroupWithDeletesAndPaging.

@Test
public void testGroupWithDeletesAndPaging() throws Throwable {
    try (Cluster cluster = init(builder().withNodes(2).withConfig(cfg -> cfg.with(Feature.GOSSIP, NETWORK, NATIVE_PROTOCOL)).start())) {
        cluster.schemaChange(withKeyspace("CREATE TABLE %s.tbl (pk int, ck int, PRIMARY KEY (pk, ck))"));
        ICoordinator coordinator = cluster.coordinator(1);
        coordinator.execute(withKeyspace("INSERT INTO %s.tbl (pk, ck) VALUES (0, 0)"), ConsistencyLevel.ALL);
        coordinator.execute(withKeyspace("INSERT INTO %s.tbl (pk, ck) VALUES (1, 1)"), ConsistencyLevel.ALL);
        cluster.get(1).executeInternal(withKeyspace("DELETE FROM %s.tbl WHERE pk=0 AND ck=0"));
        cluster.get(2).executeInternal(withKeyspace("DELETE FROM %s.tbl WHERE pk=1 AND ck=1"));
        String query = withKeyspace("SELECT * FROM %s.tbl GROUP BY pk");
        Iterator<Object[]> rows = coordinator.executeWithPaging(query, ConsistencyLevel.ALL, 1);
        assertRows(Iterators.toArray(rows, Object[].class));
        try (com.datastax.driver.core.Cluster c = com.datastax.driver.core.Cluster.builder().addContactPoint("127.0.0.1").build();
            Session session = c.connect()) {
            SimpleStatement stmt = new SimpleStatement(withKeyspace("select * from %s.tbl where pk = 1 group by pk"));
            stmt.setFetchSize(1);
            Iterator<Row> rs = session.execute(stmt).iterator();
            Assert.assertFalse(rs.hasNext());
        }
    }
}
Also used : ICoordinator(org.apache.cassandra.distributed.api.ICoordinator) SimpleStatement(com.datastax.driver.core.SimpleStatement) Cluster(org.apache.cassandra.distributed.Cluster) Row(com.datastax.driver.core.Row) 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