com.datastax.driver.core.querybuilder.Insert.ifNotExists()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(103)

本文整理了Java中com.datastax.driver.core.querybuilder.Insert.ifNotExists()方法的一些代码示例,展示了Insert.ifNotExists()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Insert.ifNotExists()方法的具体详情如下:
包路径:com.datastax.driver.core.querybuilder.Insert
类名称:Insert
方法名:ifNotExists

Insert.ifNotExists介绍

[英]Sets the 'IF NOT EXISTS' option for this INSERT statement.

An insert with that option will not succeed unless the row does not exist at the time the insertion is executed. The existence check and insertions are done transactionally in the sense that if multiple clients attempt to create a given row with this option, then at most one may succeed.

Please keep in mind that using this option has a non negligible performance impact and should be avoided when possible.

This will configure the statement as non-idempotent, see com.datastax.driver.core.Statement#isIdempotent()for more information.
[中]设置此INSERT语句的“如果不存在”选项。
除非执行插入时该行不存在,否则使用该选项的插入将不会成功。存在性检查和插入是以事务方式完成的,即如果多个客户端尝试使用此选项创建给定行,则最多一个客户端可能成功。
请记住,使用此选项对性能的影响不容忽视,应尽可能避免。
这将把语句配置为非幂等元,请参见com。数据税。驾驶员果心语句#isIdempotent()了解更多信息。

代码示例

代码示例来源:origin: kaaproject/kaa

private T insertLocked(T entity) {
 Insert insert = insertInto(getColumnFamilyName()).ifNotExists();
 CassandraEntityMapper<T> entityMapper = CassandraEntityMapper.getEntityMapperForClass(
   getColumnFamilyClass(), cassandraClient);
 for (String name : entityMapper.getKeyColumnNames()) {
  insert.value(name, entityMapper.getColumnValueForName(name, entity, cassandraClient));
 }
 for (String name : entityMapper.getNonKeyColumnNames()) {
  insert.value(name, entityMapper.getColumnValueForName(name, entity, cassandraClient));
 }
 insert.setConsistencyLevel(getWriteConsistencyLevel());
 ResultSet res = execute(insert);
 if (!res.wasApplied()) {
  LOG.error("[{}] Can't insert entity. Entity already exists!", getColumnFamilyClass());
  throw new KaaOptimisticLockingFailureException("Can't insert entity. Entity already exists!");
 } else {
  Clause[] whereClauses = buildKeyClauses(entityMapper, entity);
  Select.Where where = select().from(getColumnFamilyName()).where(whereClauses[0]);
  if (whereClauses.length > 1) {
   for (int i = 1; i < whereClauses.length; i++) {
    where = where.and(whereClauses[i]);
   }
  }
  return findOneByStatement(where);
 }
}

代码示例来源:origin: scalar-labs/scalardb

/**
 * Adds {@code PutIfNotExists}-specific conditions to the statement
 *
 * @param condition {@code PutIfNotExists} condition
 */
@Override
public void visit(PutIfNotExists condition) {
 Insert insert = (Insert) statement;
 insert.ifNotExists();
}

代码示例来源:origin: com.yugabyte/cassandra-driver-mapping

@Override
void modifyQueryString(BuiltStatement query) {
  if (ifNotExists) {
    ((Insert) query).ifNotExists();
  }
}

代码示例来源:origin: otaviojava/Easy-Cassandra

@Override
public InsertBuilder<T> ifNotExists() {
  insert.ifNotExists();
  return this;
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

insert = insertInto("foo").value("k", 0).value("x", 1).ifNotExists();
assertEquals(insert.toString(), query);

代码示例来源:origin: org.apache.james/james-server-data-cassandra

private PreparedStatement prepareInsertStatement(Session session) {
  return session.prepare(insertInto(TABLE_NAME)
    .ifNotExists()
    .value(DOMAIN, bindMarker(DOMAIN)));
}

代码示例来源:origin: com.stratio.meta/meta-core

@Override
public Statement getDriverStatement() {
 if (this.typeValues == TYPE_SELECT_CLAUSE) {
  return null;
 }
 Insert insertStmt =
   this.keyspaceInc ? QueryBuilder.insertInto(this.keyspace, this.tableName) : QueryBuilder
     .insertInto(this.tableName);
 try {
  iterateValuesAndInsertThem(insertStmt);
 } catch (Exception ex) {
  return null;
 }
 if (this.ifNotExists) {
  insertStmt = insertStmt.ifNotExists();
 }
 Insert.Options optionsStmt = checkOptions(insertStmt);
 return optionsStmt == null ? insertStmt : optionsStmt;
}

代码示例来源:origin: net.oneandone/troilus-core-java7

private static Statement toInsertStatement(WriteQueryData data,Context ctx) {
  Insert insert = insertInto(ctx.getDbSession().getTablename());
  
  List<Object> values = Lists.newArrayList();
  
  for(Entry<String, Optional<Object>> entry : data.getValuesToMutate().entrySet()) {
    insert.value(entry.getKey(), bindMarker());  
    values.add(ctx.toStatementValue(entry.getKey(), entry.getValue().orNull())); 
  }
  
  if (data.getIfNotExits() != null) {
    insert.ifNotExists();
    if (ctx.getSerialConsistencyLevel() != null) {
      insert.setSerialConsistencyLevel(ctx.getSerialConsistencyLevel());
    }
  }
  if (ctx.getTtlSec() != null) {
    insert.using(ttl(bindMarker()));  
    values.add((Integer) ctx.getTtlSec());
  }
  PreparedStatement stmt = ctx.getDbSession().prepare(insert);
  return stmt.bind(values.toArray());
}

代码示例来源:origin: com.datastax.cassandra/cassandra-driver-core

update("foo").onlyIf(contains("developers", "datastax")).with(set("v", 0)),
update("foo").with(set("v", 0)).onlyIf(contains("hello", "world")),
insertInto("foo").value("k", 1).value("v", Sets.newHashSet(now())).ifNotExists(),
delete().from("foo").where(eq("k", 2)).ifExists(),
delete().from("foo").onlyIf(eq("k", 2)),

代码示例来源:origin: org.apache.camel/camel-cassandraql

/**
 * Generate Insert CQL.
 */
public static Insert generateInsert(String table, String[] columns, boolean ifNotExists, Integer ttl) {
  Insert insert = insertInto(table);
  for (String column : columns) {
    insert = insert.value(column, bindMarker());
  }
  if (ifNotExists) {
    insert = insert.ifNotExists();
  }
  if (ttl != null) {
    insert.using(ttl(ttl));
  }
  return insert;
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

private PreparedStatement prepareInsert(Session session) {
  return session.prepare(insertInto(TABLE_NAME)
    .value(NEXT_MODSEQ, bindMarker(NEXT_MODSEQ))
    .value(MAILBOX_ID, bindMarker(MAILBOX_ID))
    .ifNotExists());
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

private PreparedStatement prepareInsert(Session session) {
  return session.prepare(insertInto(TABLE_NAME)
    .value(NEXT_UID, MessageUid.MIN_VALUE.asLong())
    .value(MAILBOX_ID, bindMarker(MAILBOX_ID))
    .ifNotExists());
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

private PreparedStatement prepareConditionalInsert(Session session) {
  return session.prepare(
    insertInto(CassandraACLTable.TABLE_NAME)
      .value(CassandraACLTable.ID, bindMarker(CassandraACLTable.ID))
      .value(CassandraACLTable.ACL, bindMarker(CassandraACLTable.ACL))
      .value(CassandraACLTable.VERSION, INITIAL_VALUE)
      .ifNotExists());
}

代码示例来源:origin: net.oneandone.troilus/troilus-core-java7

private static ListenableFuture<Statement> toInsertStatementAsync(WriteQueryData data, ExecutionSpec executionSpec, UDTValueMapper udtValueMapper, DBSession dbSession) {
  final Insert insert = (data.getTablename().getKeyspacename() == null) ? insertInto(data.getTablename().getTablename()) 
                                     : insertInto(data.getTablename().getKeyspacename(), data.getTablename().getTablename());
  
  final List<Object> values = Lists.newArrayList();
  
  for(Entry<String, Optional<Object>> entry : data.getValuesToMutate().entrySet()) {
    insert.value(entry.getKey(), bindMarker());  
    values.add(udtValueMapper.toStatementValue(data.getTablename(), entry.getKey(), entry.getValue().orNull())); 
  }
  
  if (data.getIfNotExits() != null) {
    insert.ifNotExists();
    if (executionSpec.getSerialConsistencyLevel() != null) {
      insert.setSerialConsistencyLevel(executionSpec.getSerialConsistencyLevel());
    }
  }
  if (executionSpec.getTtl() != null) {
    insert.using(ttl(bindMarker()));  
    values.add((Integer) executionSpec.getTtl());
  }
  
  final ListenableFuture<PreparedStatement> preparedStatementFuture = dbSession.prepareAsync(insert);
  return dbSession.bindAsync(preparedStatementFuture, values.toArray());
}

代码示例来源:origin: org.apache.james/event-sourcing-event-store-cassandra

private PreparedStatement prepareInsert(Session session) {
  return session.prepare(insertInto(EVENTS_TABLE)
    .value(AGGREGATE_ID, bindMarker(AGGREGATE_ID))
    .value(EVENT_ID, bindMarker(EVENT_ID))
    .value(EVENT, bindMarker(EVENT))
    .ifNotExists());
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

private PreparedStatement prepareInsert(Session session) {
  return session.prepare(insertInto(TABLE_NAME)
    .value(NAMESPACE_AND_USER, bindMarker(NAMESPACE_AND_USER))
    .value(MAILBOX_NAME, bindMarker(MAILBOX_NAME))
    .value(MAILBOX_ID, bindMarker(MAILBOX_ID))
    .ifNotExists());
}

代码示例来源:origin: org.apache.james/apache-james-mailbox-cassandra

private PreparedStatement prepareInsert(Session session) {
  return session.prepare(insertInto(TABLE_NAME)
    .value(NAMESPACE, bindMarker(NAMESPACE))
    .value(USER, bindMarker(USER))
    .value(MAILBOX_NAME, bindMarker(MAILBOX_NAME))
    .value(MAILBOX_ID, bindMarker(MAILBOX_ID))
    .ifNotExists());
}

代码示例来源:origin: org.apache.james/james-server-data-cassandra

@Inject
public CassandraUsersRepository(Session session, CassandraUtils cassandraUtils) {
  this.executor = new CassandraAsyncExecutor(session);
  this.cassandraUtils = cassandraUtils;
  this.getUserStatement = prepareGetUserStatement(session);
  this.updateUserStatement = prepareUpdateUserStatement(session);
  this.removeUserStatement = prepareRemoveUserStatement(session);
  this.countUserStatement = prepareCountStatement(session);
  this.listStatement = prepareListStatement(session);
  this.insertStatement = session.prepare(insertInto(TABLE_NAME)
    .value(NAME, bindMarker(NAME))
    .value(REALNAME, bindMarker(REALNAME))
    .value(PASSWORD, bindMarker(PASSWORD))
    .value(ALGORITHM, bindMarker(ALGORITHM))
    .ifNotExists());
}

代码示例来源:origin: org.hibernate.ogm/hibernate-ogm-cassandra

private Long nextValueInsert(IdSourceKeyMetadata metadata, String sequenceName, Long value) {
  Insert insert = QueryBuilder.insertInto( quote( metadata.getName() ) )
      .value( quote( metadata.getKeyColumnName() ), QueryBuilder.bindMarker( "sequence_name" ) )
      .value( quote( metadata.getValueColumnName() ), QueryBuilder.bindMarker( "sequence_value" ) )
      .ifNotExists();
  PreparedStatement preparedStatement = provider.getSession().prepare( insert.toString() );
  BoundStatement boundStatement = preparedStatement.bind();
  boundStatement.setString( "sequence_name", sequenceName );
  boundStatement.setLong( "sequence_value", value );
  try {
    provider.getSession().execute( boundStatement );
  }
  catch (DriverException e) {
    throw e;
  }
  return nextValueSelect( metadata, sequenceName );
}

代码示例来源:origin: kloiasoft/eventapis

private Insert createInsertQuery(EntityEvent entityEvent) {
  Insert insert = QueryBuilder.insertInto(tableName);
  insert.value(ENTITY_ID, entityEvent.getEventKey().getEntityId());
  insert.value(VERSION, entityEvent.getEventKey().getVersion());
  insert.value(OP_ID, entityEvent.getOpId());
  insert.value(OP_DATE, entityEvent.getOpDate());
  insert.value(EVENT_TYPE, entityEvent.getEventType());
  insert.value(STATUS, entityEvent.getStatus().name());
  insert.value(AUDIT_INFO, entityEvent.getAuditInfo());
  insert.value(EVENT_DATA, entityEvent.getEventData());
  insert.ifNotExists();
  return insert;
}

相关文章