Search in sources :

Example 11 with Statement

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

the class CassandraCacheStore method loadCache.

/**
 * {@inheritDoc}
 */
@Override
public void loadCache(IgniteBiInClosure<K, V> clo, Object... args) throws CacheLoaderException {
    if (clo == null)
        return;
    if (args == null || args.length == 0)
        args = new String[] { "select * from " + controller.getPersistenceSettings().getKeyspace() + "." + cassandraTable() + ";" };
    ExecutorService pool = null;
    Collection<Future<?>> futs = new ArrayList<>(args.length);
    try {
        pool = Executors.newFixedThreadPool(maxPoolSize, new IgniteThreadFactory(ignite.name(), CACHE_LOADER_THREAD_NAME));
        CassandraSession ses = getCassandraSession();
        for (Object obj : args) {
            LoadCacheCustomQueryWorker<K, V> task = null;
            if (obj instanceof Statement)
                task = new LoadCacheCustomQueryWorker<>(ses, (Statement) obj, controller, log, clo);
            else if (obj instanceof String) {
                String qry = ((String) obj).trim();
                if (qry.toLowerCase().startsWith("select"))
                    task = new LoadCacheCustomQueryWorker<>(ses, (String) obj, controller, log, clo);
            }
            if (task != null)
                futs.add(pool.submit(task));
        }
        for (Future<?> fut : futs) U.get(fut);
        if (log != null && log.isDebugEnabled() && storeSes != null)
            log.debug("Cache loaded from db: " + storeSes.cacheName());
    } catch (IgniteCheckedException e) {
        if (storeSes != null)
            throw new CacheLoaderException("Failed to load Ignite cache: " + storeSes.cacheName(), e.getCause());
        else
            throw new CacheLoaderException("Failed to load cache", e.getCause());
    } finally {
        U.shutdownNow(getClass(), pool, log);
    }
}
Also used : PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) ArrayList(java.util.ArrayList) CassandraSession(org.apache.ignite.cache.store.cassandra.session.CassandraSession) IgniteThreadFactory(org.apache.ignite.thread.IgniteThreadFactory) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) LoadCacheCustomQueryWorker(org.apache.ignite.cache.store.cassandra.session.LoadCacheCustomQueryWorker) CacheLoaderException(javax.cache.integration.CacheLoaderException) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 12 with Statement

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

the class CassandraCQLClient method read.

/**
   * Read a record from the database. Each field/value pair from the result will
   * be stored in a HashMap.
   *
   * @param table
   *          The name of the table
   * @param key
   *          The record key of the record to read.
   * @param fields
   *          The list of fields to read, or null for all of them
   * @param result
   *          A HashMap of field/value pairs for the result
   * @return Zero on success, a non-zero error code on error
   */
@Override
public Status read(String table, String key, Set<String> fields, 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).where(QueryBuilder.eq(YCSB_KEY, key)).limit(1);
        stmt.setConsistencyLevel(readConsistencyLevel);
        if (debug) {
            System.out.println(stmt.toString());
        }
        if (trace) {
            stmt.enableTracing();
        }
        ResultSet rs = session.execute(stmt);
        if (rs.isExhausted()) {
            return Status.NOT_FOUND;
        }
        // Should be only 1 row
        Row row = rs.one();
        ColumnDefinitions cd = row.getColumnDefinitions();
        for (ColumnDefinitions.Definition def : cd) {
            ByteBuffer val = row.getBytesUnsafe(def.getName());
            if (val != null) {
                result.put(def.getName(), new ByteArrayByteIterator(val.array()));
            } else {
                result.put(def.getName(), null);
            }
        }
        return Status.OK;
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Error reading key: " + key);
        return Status.ERROR;
    }
}
Also used : ColumnDefinitions(com.datastax.driver.core.ColumnDefinitions) ByteArrayByteIterator(com.yahoo.ycsb.ByteArrayByteIterator) SimpleStatement(com.datastax.driver.core.SimpleStatement) Statement(com.datastax.driver.core.Statement) Select(com.datastax.driver.core.querybuilder.Select) ResultSet(com.datastax.driver.core.ResultSet) Row(com.datastax.driver.core.Row) ByteBuffer(java.nio.ByteBuffer) DBException(com.yahoo.ycsb.DBException)

Example 13 with Statement

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

the class CassandraState method updateState.

public void updateState(List<TridentTuple> tuples, final TridentCollector collector) {
    List<Statement> statements = new ArrayList<>();
    for (TridentTuple tuple : tuples) {
        statements.addAll(options.cqlStatementTupleMapper.map(conf, session, tuple));
    }
    try {
        if (options.batchingType != null) {
            BatchStatement batchStatement = new BatchStatement(options.batchingType);
            batchStatement.addAll(statements);
            session.execute(batchStatement);
        } else {
            for (Statement statement : statements) {
                session.execute(statement);
            }
        }
    } catch (Exception e) {
        LOG.warn("Batch write operation is failed.");
        collector.reportError(e);
        throw new FailedException(e);
    }
}
Also used : Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) FailedException(org.apache.storm.topology.FailedException) BatchStatement(com.datastax.driver.core.BatchStatement) ArrayList(java.util.ArrayList) FailedException(org.apache.storm.topology.FailedException) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 14 with Statement

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

the class CassandraState method batchRetrieve.

public List<List<Values>> batchRetrieve(List<TridentTuple> tridentTuples) {
    Preconditions.checkNotNull(options.cqlResultSetValuesMapper, "CassandraState.Options should have cqlResultSetValuesMapper");
    List<List<Values>> batchRetrieveResult = new ArrayList<>();
    try {
        for (TridentTuple tridentTuple : tridentTuples) {
            List<Statement> statements = options.cqlStatementTupleMapper.map(conf, session, tridentTuple);
            for (Statement statement : statements) {
                List<List<Values>> values = options.cqlResultSetValuesMapper.map(session, statement, tridentTuple);
                batchRetrieveResult.addAll(values);
            }
        }
    } catch (Exception e) {
        LOG.warn("Batch retrieve operation is failed.");
        throw new FailedException(e);
    }
    return batchRetrieveResult;
}
Also used : Statement(com.datastax.driver.core.Statement) BatchStatement(com.datastax.driver.core.BatchStatement) FailedException(org.apache.storm.topology.FailedException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) FailedException(org.apache.storm.topology.FailedException) TridentTuple(org.apache.storm.trident.tuple.TridentTuple)

Example 15 with Statement

use of com.datastax.driver.core.Statement in project newts by OpenNMS.

the class CassandraIndexer method toStatements.

private List<Statement> toStatements(Set<StatementGenerator> generators) {
    List<Statement> statementsToExecute = Lists.newArrayList();
    Map<String, List<Statement>> statementsByKey = Maps.newHashMap();
    for (StatementGenerator generator : generators) {
        Statement statement = generator.toStatement().setConsistencyLevel(m_contextConfigurations.getWriteConsistency(generator.getContext()));
        String key = generator.getKey();
        if (key == null) {
            // Don't try batching these
            statementsToExecute.add(statement);
            continue;
        }
        // Group these by key
        List<Statement> statementsForKey = statementsByKey.get(key);
        if (statementsForKey == null) {
            statementsForKey = Lists.newArrayList();
            statementsByKey.put(key, statementsForKey);
        }
        statementsForKey.add(statement);
    }
    // Consolidate the grouped statements into batches
    for (List<Statement> statementsForKey : statementsByKey.values()) {
        for (List<Statement> partition : Lists.partition(statementsForKey, m_options.getMaxBatchSize())) {
            statementsToExecute.add(unloggedBatch(partition.toArray(new RegularStatement[partition.size()])));
        }
    }
    return statementsToExecute;
}
Also used : RegularStatement(com.datastax.driver.core.RegularStatement) PreparedStatement(com.datastax.driver.core.PreparedStatement) BoundStatement(com.datastax.driver.core.BoundStatement) Statement(com.datastax.driver.core.Statement) List(java.util.List) StatementGenerator(org.opennms.newts.cassandra.search.support.StatementGenerator)

Aggregations

Statement (com.datastax.driver.core.Statement)20 BoundStatement (com.datastax.driver.core.BoundStatement)9 PreparedStatement (com.datastax.driver.core.PreparedStatement)9 ResultSet (com.datastax.driver.core.ResultSet)8 BatchStatement (com.datastax.driver.core.BatchStatement)7 Row (com.datastax.driver.core.Row)7 RegularStatement (com.datastax.driver.core.RegularStatement)6 SimpleStatement (com.datastax.driver.core.SimpleStatement)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ResultSetFuture (com.datastax.driver.core.ResultSetFuture)4 DBException (com.yahoo.ycsb.DBException)3 ColumnDefinitions (com.datastax.driver.core.ColumnDefinitions)2 Session (com.datastax.driver.core.Session)2 BuiltStatement (com.datastax.driver.core.querybuilder.BuiltStatement)2 Select (com.datastax.driver.core.querybuilder.Select)2 ByteArrayByteIterator (com.yahoo.ycsb.ByteArrayByteIterator)2 Map (java.util.Map)2 IgniteException (org.apache.ignite.IgniteException)2 RandomSleeper (org.apache.ignite.cache.store.cassandra.common.RandomSleeper)2