com.couchbase.client.java.Bucket.insert()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.2k)|赞(0)|评价(0)|浏览(137)

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

Bucket.insert介绍

[英]Insert a Document if it does not exist already with the default key/value timeout. If the given Document (identified by its unique ID) already exists, the observable errors with a DocumentAlreadyExistsException. If the operation should also override the existing Document, #upsert(Document) should be used instead. It will always either return a document or fail with an error. The returned Document contains original properties, but has the refreshed CAS value set. This operation will return successfully if the Document has been acknowledged in the managed cache layer on the master server node. If increased data durability is a concern, #insert(Document,PersistTo,ReplicateTo) should be used instead. This method throws under the following conditions: - The operation takes longer than the specified timeout: TimeoutException wrapped in a RuntimeException- The producer outpaces the SDK: BackpressureException- The request content is too big: RequestTooBigException- The operation had to be cancelled while on the wire or the retry strategy cancelled it instead of retrying: RequestCancelledException- The server is currently not able to process the request, retrying may help: TemporaryFailureException- The server is out of memory: CouchbaseOutOfMemoryException- Unexpected errors are caught and contained in a generic CouchbaseException.
[中]如果不存在具有默认键/值超时的文档,请插入该文档。如果给定文档(由其唯一ID标识)已存在,则可观察到的错误将包含DocumentReadyExistsException。如果操作还应覆盖现有文档,则应使用#upsert(文档)。它将始终返回文档或失败并出现错误。返回的文档包含原始属性,但已设置刷新的CAS值。如果已在主服务器节点上的托管缓存层中确认文档,则此操作将成功返回。如果担心数据持久性增加,则应使用#insert(Document、PersistTo、ReplicateTo)。此方法在以下条件下引发:-操作花费的时间超过指定的超时时间:RuntimeException中包装的TimeoutException-生产者超过SDK:BackpressureException-请求内容太大:RequestTooBigException-必须在连线时取消操作或取消重试策略它没有重试:RequestCancelledException-服务器当前无法处理请求,重试可能会有帮助:暂时性故障异常-服务器内存不足:CouchbaseOutOfMemoryException-捕获意外错误并将其包含在通用CouchbaseException中。

代码示例

代码示例来源:origin: apache/nifi

@Override
public <K, V> boolean putIfAbsent(K key, V value, Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException {
  final String docId = toDocumentId(key, keySerializer);
  final Document doc = toDocument(docId, value, valueSerializer);
  try {
    bucket.insert(doc);
    return true;
  } catch (DocumentAlreadyExistsException e) {
    return false;
  }
}

代码示例来源:origin: apache/nifi

@Override
public <K, V> boolean replace(AtomicCacheEntry<K, V, Long> entry, Serializer<K> keySerializer, Serializer<V> valueSerializer) throws IOException {
  final Long revision = entry.getRevision().orElse(0L);
  final String docId = toDocumentId(entry.getKey(), keySerializer);
  final Document doc = toDocument(docId, entry.getValue(), valueSerializer, revision);
  try {
    if (revision < 0) {
      // If the document does not exist yet, try to create one.
      try {
        bucket.insert(doc);
        return true;
      } catch (DocumentAlreadyExistsException e) {
        return false;
      }
    }
    bucket.replace(doc);
    return true;
  } catch (DocumentDoesNotExistException|CASMismatchException e) {
    return false;
  }
}

代码示例来源:origin: apache/incubator-gobblin

JsonObject content = JsonObject.empty().put("name", "Michael");
JsonDocument doc = JsonDocument.create("docId", content);
JsonDocument inserted = bucket.insert(doc);

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

/**
 * Inserts a {@link JsonDocument} if it does not exist already.
 *
 * @param documentId the unique ID of the document
 * @param json       the JSON String representing the document to store
 * @return the newly created {@link JsonDocument}
 * @see Bucket#insert(Document)
 */
public JsonDocument insert(String documentId, String json) {
  return this.bucket.insert(JsonDocument.create(documentId, JsonObject.fromJson(json)));
}

代码示例来源:origin: Impetus/Kundera

@Override
protected void onPersist(EntityMetadata entityMetadata, Object entity, Object id, List<RelationHolder> rlHolders)
{
  JsonDocument doc = handler.getDocumentFromEntity(entityMetadata, entity, kunderaMetadata);
  if (!isUpdate)
  {
    bucket.insert(doc);
    LOGGER.debug("Inserted document with ID : " + doc.id() + " in the " + bucket.name() + " Bucket");
  }
  else
  {
    bucket.upsert(doc);
    LOGGER.debug("Updated document with ID : " + doc.id() + " in the " + bucket.name() + " Bucket");
  }
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testRemove() {
  try {
    //@eclipse-formatter:off
    couchbaseBucket.insert(JsonDocument.create("testRemove", JsonObject.empty()));
    new Couchbase().remove(HOST, BUCKET_NAME, "testRemove");
    assertFalse(couchbaseBucket.exists("testRemove"));
    //@eclipse-formatter:on
  } catch (Exception e) {
    e.printStackTrace();
    fail();
  }
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

public static boolean fillDB(CouchbaseCluster cluster) {
  Bucket couchbaseBucket = cluster.openBucket(BUCKET_NAME);
  couchbaseBucket.insert(JsonDocument.create("artist:vincent_van_gogh", VINCENT_VAN_GOGH));
  N1qlQueryResult queryResult = couchbaseBucket.query(N1qlQuery.simple(String.format(QUERY, BUCKET_NAME), N1qlParams.build().consistency(ScanConsistency.REQUEST_PLUS)));
  couchbaseBucket.close();
  return queryResult.info().resultCount() == 1;
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testRemoveViaCall() {
  couchbaseBucket.insert(JsonDocument.create("testRemove", JsonObject.empty()));
  testCall(graphDB, "CALL apoc.couchbase.remove({host}, {bucket}, 'testRemove')",
      map("host", HOST, "bucket", BUCKET_NAME),
      r -> assertFalse(couchbaseBucket.exists("testRemove")));
}

代码示例来源:origin: neo4j-contrib/neo4j-apoc-procedures

@Test
public void testRemoveViaCall() {
  couchbaseBucket.insert(JsonDocument.create("testRemove", JsonObject.empty()));
  testCall(graphDB, "CALL apoc.couchbase.remove({host}, {bucket}, 'testRemove')",
      map("host", HOST, "bucket", BUCKET_NAME + ":" + PASSWORD),
      r -> assertFalse(couchbaseBucket.exists("testRemove")));
}

代码示例来源:origin: com.couchbase.client/java-client

/**
 * Create a new {@link CouchbaseArraySet}, backed by the document identified by <code>id</code>
 * in the given Couchbase <code>bucket</code>. Note that if the document already exists,
 * its content will be used as initial content for this collection. Otherwise it is created empty.
 *
 * @param id the id of the Couchbase document to back the set.
 * @param bucket the {@link Bucket} through which to interact with the document.
 */
public CouchbaseArraySet(String id, Bucket bucket) {
  this.id = id;
  this.bucket = bucket;
  try {
    this.bucket.insert(JsonArrayDocument.create(id, JsonArray.empty()));
  } catch (DocumentAlreadyExistsException e) {
    //use a pre-existing document
  }
}

代码示例来源:origin: com.couchbase.client/java-client

/**
 * Create a new {@link Bucket Couchbase-backed} List, backed by the document identified by <code>id</code>
 * in <code>bucket</code>. Note that if the document already exists, its content will be used as initial
 * content for this collection. Otherwise it is created empty.
 *
 * @param id the id of the Couchbase document to back the list.
 * @param bucket the {@link Bucket} through which to interact with the document.
 */
public CouchbaseArrayList(String id, Bucket bucket) {
  this.bucket = bucket;
  this.id = id;
  try {
    bucket.insert(JsonArrayDocument.create(id, JsonArray.empty()));
  } catch (DocumentAlreadyExistsException ex) {
    // Ignore concurrent creations, keep on moving.
  }
}

代码示例来源:origin: com.couchbase.client/java-client

/**
 * Create a new {@link Bucket Couchbase-backed} Queue, backed by the document identified by <code>id</code>
 * in <code>bucket</code>. Note that if the document already exists, its content will be used as initial
 * content for this collection. Otherwise it is created empty.
 *
 * @param id the id of the Couchbase document to back the queue.
 * @param bucket the {@link Bucket} through which to interact with the document.
 */
public CouchbaseQueue(String id, Bucket bucket) {
  this.bucket = bucket;
  this.id = id;
  try {
    bucket.insert(JsonArrayDocument.create(id, JsonArray.empty()));
  } catch (DocumentAlreadyExistsException ex) {
    // Ignore concurrent creations, keep on moving.
  }
}

代码示例来源:origin: com.couchbase.client/java-client

/**
 * Create a new {@link CouchbaseMap}, backed by the document identified by <code>id</code>
 * in the given Couchbase <code>bucket</code>. Note that if the document already exists,
 * its content will be used as initial content for this collection. Otherwise it is created empty.
 *
 * @param id the id of the Couchbase document to back the map.
 * @param bucket the {@link Bucket} through which to interact with the document.
 */
public CouchbaseMap(String id, Bucket bucket) {
  this.id = id;
  this.bucket = bucket;
  try {
    bucket.insert(JsonDocument.create(id, JsonObject.empty()));
  } catch (DocumentAlreadyExistsException ex) {
    // Ignore concurrent creations, keep on moving.
  }
}

代码示例来源:origin: lukas-krecan/ShedLock

@Override
public boolean insertRecord(LockConfiguration lockConfiguration) {
  try {
    JsonObject content = JsonObject.empty();
    content.put(LOCK_NAME, lockConfiguration.getName());
    content.put(LOCK_UNTIL, toIsoString(lockConfiguration.getLockAtMostUntil()));
    content.put(LOCKED_AT, toIsoString(Instant.now()));
    content.put(LOCKED_BY, getHostname());
    JsonDocument document = JsonDocument.create(lockConfiguration.getName(), content);
    bucket.insert(document);
    return true;
  } catch (DocumentAlreadyExistsException e) {
    return false;
  }
}

代码示例来源:origin: org.springframework.data/spring-data-couchbase

} else {
  storedDoc = client.insert(doc, persistTo, replicateTo);
case INSERT:
default:
 storedDoc = client.insert(doc, persistTo, replicateTo);
 break;

代码示例来源:origin: spring-projects/spring-data-couchbase

} else {
  storedDoc = client.insert(doc, persistTo, replicateTo);
case INSERT:
default:
 storedDoc = client.insert(doc, persistTo, replicateTo);
 break;

相关文章