protected void executeBatch(BatchStatement batch) { LOG.debug("Execute cassandra batch {}", batch); batch.setConsistencyLevel(getWriteConsistencyLevel()); ResultSet resultSet = getSession().execute(batch); LOG.debug("Executed batch {}", resultSet); }
public BatchExecution executeBatch( Session session, String statement, ConsistencyLevel level, ConsistencyLevel serialLevel) { BatchStatement batch = new BatchStatement(); batch.add(new SimpleStatement(statement)); if (level != null) { batch.setConsistencyLevel(level); } if (serialLevel != null) { batch.setSerialConsistencyLevel(serialLevel); } session.execute(batch); List<BatchExecution> batches = sCluster.node(1).activityClient().retrieveBatches(); assertEquals(batches.size(), 1); return batches.get(0); }
@VisibleForTesting void setConsistencyForConditionalMutation(BatchStatement statement) { statement.setConsistencyLevel(ConsistencyLevel.QUORUM); // for learn phase statement.setSerialConsistencyLevel(ConsistencyLevel.SERIAL); // for paxos phase } }
@Override public void commit(Long txid) { LOG.debug("Commiting [{}]", txid); BatchStatement batch = new BatchStatement(batchType); batch.setConsistencyLevel(batchConsistencyLevel); int i = 0; for(Statement statement : this.statements) { batch.add(statement); i++; if(i >= this.maxBatchSize) { clientFactory.getSession().execute(batch); batch = new BatchStatement(batchType); i = 0; } } if(i > 0) { clientFactory.getSession().execute(batch); } this.statements.clear(); }
public void save(final List<Event> events) { final BatchStatement batch = new BatchStatement(); for (final Event event : events) { final Map<String, Object> parsedEvent = parse(event); if (parsedEvent.isEmpty()) { log.warn("Event {} could not be mapped. Suggestion: Cassandra is case sensitive, so maybe you can check field names.", event); continue; } if (!hasPrimaryKey(parsedEvent)) { break; } final Insert insert = QueryBuilder.insertInto(table); for (final Map.Entry<String, Object> entry : parsedEvent.entrySet()) { insert.value(entry.getKey(), entry.getValue()); } if (log.isTraceEnabled()) { log.trace("Preparing insert for table {}: {}", table.getName(), insert.getQueryString()); } batch.add(insert); } if (batch.getStatements().isEmpty()) { log.warn("No event produced insert query for table {}", table.getName()); return; } batch.setConsistencyLevel(consistencyLevel); session.execute(batch); }
migrationMetaHolders.get(migrationVersionStr).getVersionRank() + 1, migrationVersionStr)); batchStatement.setConsistencyLevel(this.consistencyLevel);
private BatchStatement getCachedPreparedStatement() { final List<CqlColumnListMutationImpl<?, ?>> colListMutations = getColumnMutations(); if (colListMutations == null || colListMutations.size() == 0) { return new BatchStatement(Type.UNLOGGED); } ColListMutationType mutationType = colListMutations.get(0).getType(); BatchStatement batch = new BatchStatement(Type.UNLOGGED); if (mutationType == ColListMutationType.CounterColumnsUpdate) { batch = new BatchStatement(Type.COUNTER); } else if (useAtomicBatch()) { batch = new BatchStatement(Type.LOGGED); } for (CqlColumnListMutationImpl<?, ?> colListMutation : colListMutations) { CFMutationQueryGen queryGen = colListMutation.getMutationQueryGen(); queryGen.addColumnListMutationToBatch(batch, colListMutation, useCaching); } batch.setConsistencyLevel(ConsistencyLevelMapping.getCL(this.getConsistencyLevel())); return batch; }
public BatchExecution executeBatch( Session session, String statement, ConsistencyLevel level, ConsistencyLevel serialLevel) { BatchStatement batch = new BatchStatement(); batch.add(new SimpleStatement(statement)); if (level != null) { batch.setConsistencyLevel(level); } if (serialLevel != null) { batch.setSerialConsistencyLevel(serialLevel); } session.execute(batch); List<BatchExecution> batches = sCluster.node(1).activityClient().retrieveBatches(); assertEquals(batches.size(), 1); return batches.get(0); }
@Override public String writeSingle(String key) { if(config.getRowsPerPartition() > 1) { BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED); batch.setConsistencyLevel(ConsistencyLevel.valueOf(config.getWriteConsistencyLevel())); for (int i = 0; i < config.getRowsPerPartition(); i++) { batch.add(getWriteBStmt(key,i)); } session.execute(batch); batch.clear(); } else { session.execute(getWriteBStmt(key, 1) .setConsistencyLevel(ConsistencyLevel.valueOf(config.getWriteConsistencyLevel()))); } return ResultOK; }
@Override public String writeSingle(String key) { if(config.getRowsPerPartition() > 1) { BatchStatement batch = new BatchStatement(BatchStatement.Type.UNLOGGED); batch.setConsistencyLevel(ConsistencyLevel.valueOf(config.getWriteConsistencyLevel())); for (int i = 0; i < config.getRowsPerPartition(); i++) { batch.add(getWriteBStmt(key,i)); } session.execute(batch); batch.clear(); } else { session.execute(getWriteBStmt(key, 1) .setConsistencyLevel(ConsistencyLevel.valueOf(config.getWriteConsistencyLevel()))); } return ResultOK; }
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); }
session.execute(batchStatement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)); });
session.execute(batchStatement.setConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)); });