/** * {@inheritDoc} */ @Override public List<Statement> map(Map<String, Object> conf, Session session, ITuple tuple) { final BatchStatement batch = new BatchStatement(this.type); for (CQLStatementTupleMapper m : mappers) batch.addAll(m.map(conf, session, tuple)); return Arrays.asList((Statement) batch); } }
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); } }
@Override public void flush() throws IOException { try { LOGGER.debug("Attempting to flush {} items to cassandra", insertBatch.size()); lock.writeLock().lock(); BatchStatement batchStatement = new BatchStatement(); batchStatement.addAll(insertBatch); session.execute(batchStatement); lastWrite.set(System.currentTimeMillis()); insertBatch = new ArrayList<>(); } finally { lock.writeLock().unlock(); } }
/** * {@inheritDoc} */ @Override public List<Statement> map(Map conf, Session session, ITuple tuple) { final BatchStatement batch = new BatchStatement(this.type); for(CQLStatementTupleMapper m : mappers) batch.addAll(m.map(conf, session, tuple)); return Arrays.asList((Statement)batch); } }
/** {@inheritDoc} */ @Override public void commit() throws IOException { synchronized (mMonitor) { Preconditions.checkState(mStatements != null, "commit() must be paired with a call to begin()."); Preconditions.checkState(mState == State.OPEN, "Can not commit a transaction on an AtomicFijiPutter instance in state %s.", mState); // We don't actually need the writer layout capsule here, but we want the layout update check. getCapsule(); Preconditions.checkState(mStatements.size() > 0, "No transactions to commit."); final Statement statement; if (mStatements.size() > 1) { statement = new BatchStatement(BatchStatement.Type.UNLOGGED).addAll(mStatements); } else { statement = mStatements.get(0); } ResultSet result = mTable.getAdmin().execute(statement); LOG.debug("Results from batch commit: {}.", result); reset(); } }
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); } }
private void executeUpdatesSynchronous(DBTransaction transaction) { BatchStatement batchState = new BatchStatement(Type.UNLOGGED); batchState.addAll(getMutations(transaction)); executeBatch(batchState); }
private void mutateManyLogged(final Map<String, Map<StaticBuffer, KCVMutation>> mutations, final StoreTransaction txh) throws BackendException { final MaskedTimestamp commitTime = new MaskedTimestamp(txh); final BatchStatement batchStatement = new BatchStatement(Type.LOGGED); batchStatement.setConsistencyLevel(getTransaction(txh).getWriteConsistencyLevel()); batchStatement.addAll(Iterator.ofAll(mutations.entrySet()).flatMap(tableNameAndMutations -> { final String tableName = tableNameAndMutations.getKey(); final Map<StaticBuffer, KCVMutation> tableMutations = tableNameAndMutations.getValue(); final CQLKeyColumnValueStore columnValueStore = Option.of(this.openStores.get(tableName)) .getOrElseThrow(() -> new IllegalStateException("Store cannot be found: " + tableName)); return Iterator.ofAll(tableMutations.entrySet()).flatMap(keyAndMutations -> { final StaticBuffer key = keyAndMutations.getKey(); final KCVMutation keyMutations = keyAndMutations.getValue(); final Iterator<Statement> deletions = Iterator.of(commitTime.getDeletionTime(this.times)) .flatMap(deleteTime -> Iterator.ofAll(keyMutations.getDeletions()).map(deletion -> columnValueStore.deleteColumn(key, deletion, deleteTime))); final Iterator<Statement> additions = Iterator.of(commitTime.getAdditionTime(this.times)) .flatMap(addTime -> Iterator.ofAll(keyMutations.getAdditions()).map(addition -> columnValueStore.insertColumn(key, addition, addTime))); return Iterator.concat(deletions, additions); }); })); final Future<ResultSet> result = Future.fromJavaFuture(this.executorService, this.session.executeAsync(batchStatement)); result.await(); if (result.isFailure()) { throw EXCEPTION_MAPPER.apply(result.getCause().get()); } sleepAfterWrite(txh, commitTime); }
private void mutateManyUnlogged(final Map<String, Map<StaticBuffer, KCVMutation>> mutations, final StoreTransaction txh) throws BackendException { final MaskedTimestamp commitTime = new MaskedTimestamp(txh); final Future<Seq<ResultSet>> result = Future.sequence(this.executorService, Iterator.ofAll(mutations.entrySet()).flatMap(tableNameAndMutations -> { final String tableName = tableNameAndMutations.getKey(); final Map<StaticBuffer, KCVMutation> tableMutations = tableNameAndMutations.getValue(); final CQLKeyColumnValueStore columnValueStore = Option.of(this.openStores.get(tableName)) .getOrElseThrow(() -> new IllegalStateException("Store cannot be found: " + tableName)); return Iterator.ofAll(tableMutations.entrySet()).flatMap(keyAndMutations -> { final StaticBuffer key = keyAndMutations.getKey(); final KCVMutation keyMutations = keyAndMutations.getValue(); final Iterator<Statement> deletions = Iterator.of(commitTime.getDeletionTime(this.times)) .flatMap(deleteTime -> Iterator.ofAll(keyMutations.getDeletions()).map(deletion -> columnValueStore.deleteColumn(key, deletion, deleteTime))); final Iterator<Statement> additions = Iterator.of(commitTime.getAdditionTime(this.times)) .flatMap(addTime -> Iterator.ofAll(keyMutations.getAdditions()).map(addition -> columnValueStore.insertColumn(key, addition, addTime))); return Iterator.concat(deletions, additions) .grouped(this.batchSize) .map(group -> Future.fromJavaFuture(this.executorService, this.session.executeAsync( new BatchStatement(Type.UNLOGGED) .addAll(group) .setConsistencyLevel(getTransaction(txh).getWriteConsistencyLevel())))); }); })); result.await(); if (result.isFailure()) { throw EXCEPTION_MAPPER.apply(result.getCause().get()); } sleepAfterWrite(txh, commitTime); }