本文整理匯總了Java中com.datastax.driver.core.BatchStatement類的典型用法代碼示例。如果您正苦於以下問題:Java BatchStatement類的具體用法?Java BatchStatement怎麽用?Java BatchStatement使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
BatchStatement類屬於com.datastax.driver.core包,在下文中一共展示了BatchStatement類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: untilApplied
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
public static boolean untilApplied(Session session, BatchStatement.Type type, Consumer<BatchStatement> transaction) {
for (int i = 1; i <= MAX_RETRY; i ++) {
BatchStatement batchStatement = new BatchStatement(type);
transaction.accept(batchStatement);
if (batchStatement.size() == 0) return false;
boolean applied;
if (batchStatement.size() > 1) {
applied = session.execute(batchStatement).wasApplied();
} else {
Statement statement = Iterables.getOnlyElement(batchStatement.getStatements());
applied = session.execute(statement).wasApplied();
}
if (applied) return true;
log.warn("Attempt {}/{} failed executing {}", i, MAX_RETRY, batchStatement);
try {
Thread.sleep(100 * i);
} catch (InterruptedException e) {
throw new AttemptsFailedException(e);
}
}
throw new AttemptsFailedException();
}
示例2: tuneStatementExecutionOptions
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
/**
* Tunes CQL statement execution options (consistency level, fetch option and etc.).
*
* @param statement Statement.
* @return Modified statement.
*/
private Statement tuneStatementExecutionOptions(Statement statement) {
String qry = "";
if (statement instanceof BoundStatement) {
qry = ((BoundStatement)statement).preparedStatement().getQueryString().trim().toLowerCase();
}
else if (statement instanceof PreparedStatement) {
qry = ((PreparedStatement)statement).getQueryString().trim().toLowerCase();
}
boolean readStatement = qry.startsWith("select");
boolean writeStatement = statement instanceof Batch || statement instanceof BatchStatement ||
qry.startsWith("insert") || qry.startsWith("delete") || qry.startsWith("update");
if (readStatement && readConsistency != null) {
statement.setConsistencyLevel(readConsistency);
}
if (writeStatement && writeConsistency != null) {
statement.setConsistencyLevel(writeConsistency);
}
if (fetchSize != null) {
statement.setFetchSize(fetchSize);
}
return statement;
}
示例3: main
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
public static void main(String[] args) {
Session session = Connection.connect();
BatchStatement batchStatement = new BatchStatement();
PreparedStatement preparedStatement = session.prepare("insert into user (id, name) values (?, ?)");
int i = 0;
while(i < 10) {
batchStatement.add(preparedStatement.bind(UUIDs.timeBased(), "user-" + i));
++i;
}
try {
ResultSet rs = session.execute(batchStatement);
System.out.println(rs);
} catch (Exception ex) {
ex.printStackTrace();
}
Connection.close();
}
示例4: CassandraPublisher
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
public CassandraPublisher() {
super();
PropertyManagement properties = PropertyManagement.getProperties();
cassandraNode =
properties.getProperty(CassandraPublisherPropertyValues.CASSANDRA_CONNECT_NODE,
CassandraPublisherPropertyValues.CASSANDRA_CONNECT_NODE_DEFAULT);
batchSize =
properties.asInt(CassandraPublisherPropertyValues.CASSANDRA_BATCH_SIZE,
CassandraPublisherPropertyValues.CASSANDRA_BATCH_SIZE_DEFAULT);
flushFreq =
properties.asInt(CassandraPublisherPropertyValues.CASSANDRA_FLUSH_FREQ,
CassandraPublisherPropertyValues.CASSANDRA_FLUSH_FREQ_DEFAULT);
insertOnly =
properties.asBoolean(CassandraPublisherPropertyValues.CASSANDRA_INSERT_ONLY,
CassandraPublisherPropertyValues.CASSANDRA_INSERT_ONLY_DEFAULT);
batch = new BatchStatement(BatchStatement.Type.LOGGED);
map = new HashMap<>();
timer = new Timer();
// reinitialize things
publishEvents();
}
示例5: store
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
@Override
public void store(ConfigPath key, String data) throws ConfigDbException {
final BatchStatement batchStat = new BatchStatement();
batchStat.add(getStatement(StatementName.PUT_SETTING).bind(key.toString(), "", data));
ConfigPath parent;
ConfigPath child = key;
while ((parent = child.getParent()) != null) {
batchStat.add(getStatement(StatementName.PUT_SETTING).bind(parent.toString(), child.toString(), data));
child = parent;
}
session.execute(batchStat);
}
示例6: getStatementArguments
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
/**
* Get the arguments of a statement in an ordered key-value array.
*
* @param arg0
* The statement.
* @return The key-value array.
*/
public static String[] getStatementArguments( Statement arg0 ) {
String[] returnValue = EMTPY_STRING_ARRAY;
if ( arg0 instanceof ProfiledBoundStatement ) {
returnValue = ( (ProfiledBoundStatement) arg0 ).getArgumentList();
} else if ( arg0 instanceof BatchStatement ) {
List<String> argumentList = new ArrayList<String>();
Collection<Statement> statements = ( (BatchStatement) arg0 ).getStatements();
for ( Statement statement : statements ) {
String[] statementArguments = getStatementArguments( statement );
Collections.addAll( argumentList, statementArguments );
}
returnValue = argumentList.toArray( new String[argumentList.size()] );
}
return returnValue;
}
示例7: getStatementName
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
/**
* Get the name of a statement.
*
* @param arg0 The statement.
* @return The name used for logging.
*/
public static String getStatementName( Statement arg0 ) {
String returnValue = "unknown";
if ( arg0 instanceof RegularStatement ) {
returnValue = ( (RegularStatement) arg0 ).getQueryString();
} else if ( arg0 instanceof BoundStatement ) {
PreparedStatement preparedStatement = ( (BoundStatement) arg0 ).preparedStatement();
returnValue = preparedStatement.getQueryString();
} else if ( arg0 instanceof BatchStatement ) {
StringBuilder value = new StringBuilder( "Batch : " );
Collection<Statement> statements = ( (BatchStatement) arg0 ).getStatements();
boolean first = true;
for ( Statement statement : statements ) {
if ( first ) {
first = false;
} else {
value.append( ", " );
}
String statementName = getStatementName( statement );
value.append( statementName );
}
returnValue = value.toString();
}
return returnValue;
}
示例8: batchInsert
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
public ResultSet batchInsert(final Class<?> targetClass, final Collection<? extends Map<String, Object>> propsList, final BatchStatement.Type type) {
N.checkArgument(N.notNullOrEmpty(propsList), "'propsList' can't be null or empty.");
final BatchStatement batchStatement = new BatchStatement(type == null ? BatchStatement.Type.LOGGED : type);
if (settings != null) {
batchStatement.setConsistencyLevel(settings.getConsistency());
batchStatement.setSerialConsistencyLevel(settings.getSerialConsistency());
batchStatement.setRetryPolicy(settings.getRetryPolicy());
if (settings.traceQuery) {
batchStatement.enableTracing();
} else {
batchStatement.disableTracing();
}
}
CP pair = null;
for (Map<String, Object> props : propsList) {
pair = prepareAdd(targetClass, props);
batchStatement.add(prepareStatement(pair.cql, pair.parameters.toArray()));
}
return session.execute(batchStatement);
}
示例9: update
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
@Override
public Document update(Document entity)
{
Document old = read(entity.getId()); //will throw exception of doc is not found
entity.setCreatedAt(old.getCreatedAt());//copy over the original create date
Table table = entity.getTable();
PreparedStatement updateStmt = PreparedStatementFactory.getPreparedStatement(String.format(CREATE_CQL, table.toDbTable(), Columns.ID), getSession());
BoundStatement bs = new BoundStatement(updateStmt);
bindCreate(bs, entity);
BatchStatement batch = new BatchStatement(BatchStatement.Type.LOGGED);
batch.add(bs);//the actual update
try
{
List<BoundStatement> indexStatements = IndexMaintainerHelper.generateDocumentUpdateIndexEntriesStatements(getSession(), entity, bucketLocator);
for (BoundStatement boundIndexStatement : indexStatements)
{
batch.add(boundIndexStatement);//the index updates
}
getSession().execute(batch);
return entity;
} catch (IndexParseException e)
{
throw new RuntimeException(e);
}
}
示例10: should_generate_batch_statement
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
@Test
public void should_generate_batch_statement() throws Exception {
//Given
Statement st1 = new SimpleStatement("SELECT * FROM users LIMIT 10;");
Statement st2 = new SimpleStatement("INSERT INTO users(id) VALUES(10);");
Statement st3 = new SimpleStatement("UPDATE users SET name = 'John DOE' WHERE id=10;");
CassandraQueryOptions options = new CassandraQueryOptions(Option.apply(QUORUM),
Option.<ConsistencyLevel>empty(),
Option.empty(),
Option.<RetryPolicy>empty(),
Option.empty());
//When
BatchStatement actual = helper.generateBatchStatement(UNLOGGED, options, toScalaList(asList(st1, st2, st3)));
//Then
assertThat(actual).isNotNull();
final List<Statement> statements = new ArrayList<>(actual.getStatements());
assertThat(statements).hasSize(3);
assertThat(statements.get(0)).isSameAs(st1);
assertThat(statements.get(1)).isSameAs(st2);
assertThat(statements.get(2)).isSameAs(st3);
assertThat(actual.getConsistencyLevel()).isSameAs(QUORUM);
}
示例11: sendBatch
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
public void sendBatch(BatchStatement.Type type, boolean addCounter, boolean addNonCounter)
{
assert addCounter || addNonCounter;
BatchStatement b = new BatchStatement(type);
for (int i = 0; i < 10; i++)
{
if (addNonCounter)
b.add(noncounter.bind(i, "foo"));
if (addCounter)
b.add(counter.bind((long)i, i));
}
session.execute(b);
}
示例12: perform
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
public void perform(CassandraPersistenceSession session, Object parameter, BatchStatement flush) {
String deploymentId = (String) parameter;
Session s = session.getSession();
List<Row> processDefinitionsToDelete = s.execute(QueryBuilder.select("id", "key", "version").from(ProcessDefinitionTableHandler.TABLE_NAME).where(eq("deployment_id", deploymentId))).all();
List<String> ids = new ArrayList<String>();
for (Row processDefinitionToDelete : processDefinitionsToDelete) {
ids.add(processDefinitionToDelete.getString("id"));
flush.add(QueryBuilder.delete().all().from(ProcessDefinitionTableHandler.TABLE_NAME_IDX_VERSION)
.where(eq("key", processDefinitionToDelete.getString("key")))
.and(eq("version", processDefinitionToDelete.getInt("version"))));
}
flush.add(QueryBuilder.delete().all().from(ProcessDefinitionTableHandler.TABLE_NAME).where(in("id", ids)));
}
開發者ID:camunda,項目名稱:camunda-engine-cassandra,代碼行數:19,代碼來源:BulkDeleteProcessDefinitionByDeploymentId.java
示例13: should_generate_batch_statement
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
@Test
public void should_generate_batch_statement() throws Exception {
//Given
Statement st1 = new SimpleStatement("SELECT * FROM users LIMIT 10;");
Statement st2 = new SimpleStatement("INSERT INTO users(id) VALUES(10);");
Statement st3 = new SimpleStatement("UPDATE users SET name = 'John DOE' WHERE id=10;");
CassandraQueryOptions options = new CassandraQueryOptions(Option.apply(QUORUM),
Option.<ConsistencyLevel>empty(),
Option.empty(),
Option.<RetryPolicy>empty(),
Option.empty(),
Option.empty());
//When
BatchStatement actual = helper.generateBatchStatement(UNLOGGED, options, toScalaList(asList(st1, st2, st3)));
//Then
assertThat(actual).isNotNull();
final List<Statement> statements = new ArrayList<>(actual.getStatements());
assertThat(statements).hasSize(3);
assertThat(statements.get(0)).isSameAs(st1);
assertThat(statements.get(1)).isSameAs(st2);
assertThat(statements.get(2)).isSameAs(st3);
assertThat(actual.getConsistencyLevel()).isSameAs(QUORUM);
}
示例14: load
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
public void load(Iterator<List<Object>> rows)
{
PreparedStatement statement = session.prepare(insertQuery);
BatchStatement batch = createBatchStatement();
while (rows.hasNext()) {
if (batch.size() >= batchRowsCount) {
session.execute(batch);
batch = createBatchStatement();
}
List<Object> row = rows.next();
checkState(row.size() == columnsCount, "values count in a row is expected to be %d, but found: %d", columnsCount, row.size());
batch.add(statement.bind(row.toArray()));
}
if (batch.size() > 0) {
session.execute(batch);
}
}
示例15: tuneStatementExecutionOptions
import com.datastax.driver.core.BatchStatement; //導入依賴的package包/類
/**
* Tunes CQL statement execution options (consistency level, fetch option and etc.).
*
* @param statement Statement.
* @return Modified statement.
*/
private Statement tuneStatementExecutionOptions(Statement statement) {
String qry = "";
if (statement instanceof BoundStatement)
qry = ((BoundStatement)statement).preparedStatement().getQueryString().trim().toLowerCase();
else if (statement instanceof PreparedStatement)
qry = ((PreparedStatement)statement).getQueryString().trim().toLowerCase();
boolean readStatement = qry.startsWith("select");
boolean writeStatement = statement instanceof Batch || statement instanceof BatchStatement ||
qry.startsWith("insert") || qry.startsWith("delete") || qry.startsWith("update");
if (readStatement && readConsistency != null)
statement.setConsistencyLevel(readConsistency);
if (writeStatement && writeConsistency != null)
statement.setConsistencyLevel(writeConsistency);
if (fetchSize != null)
statement.setFetchSize(fetchSize);
return statement;
}