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);
}
}
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;
}
}
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);
}
}
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;
}
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;
}
Aggregations