Java Code Examples for com.couchbase.client.java.document.JsonDocument

The following examples show how to use com.couchbase.client.java.document.JsonDocument. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ShedLock   Source File: CouchbaseLockProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean insertRecord(@NonNull 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(ClockProvider.now()));
        content.put(LOCKED_BY, getHostname());
        JsonDocument document = JsonDocument.create(lockConfiguration.getName(), content);

        bucket.insert(document);
        return true;

    } catch (DocumentAlreadyExistsException e) {
        return false;
    }

}
 
Example 2
Source Project: ShedLock   Source File: CouchbaseLockProvider.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean extend(@NonNull LockConfiguration lockConfiguration) {
    try {
        JsonDocument document = bucket.get(lockConfiguration.getName());
        Instant lockUntil = parse(document.content().get(LOCK_UNTIL));
        Instant now = ClockProvider.now();
        if (lockUntil.isBefore(now) || !document.content().get(LOCKED_BY).equals(getHostname())) {
            return false;
        }

        document.content().put(LOCK_UNTIL, toIsoString(lockConfiguration.getLockAtMostUntil()));

        bucket.replace(document);
        return true;

    } catch (CASMismatchException e) {
        return false;
    }
}
 
Example 3
Source Project: datacollector   Source File: CouchbaseProcessor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Writes full document lookup values to the record
 *
 * @param record the record being processed
 * @param doc the JsonDocument from Couchbase
 * @return an Observable of the updated record
 */
private Observable<Record> setDocumentInRecord(Record record, JsonDocument doc) {
  if(doc.content() == null) {
    LOG.debug("Document does not exist: {}", doc.id());
    return handleError(record, Errors.COUCHBASE_26, true);
  }

  try {
    record.set(config.outputField, jsonToField(doc.content().toMap()));
    record.getHeader().setAttribute(config.CAS_HEADER_ATTRIBUTE, String.valueOf(doc.cas()));
    return Observable.just(record);
  } catch (IOException e) {
    LOG.debug("Unable to set KV lookup in record for: {}", doc.id());
    return handleError(record, Errors.COUCHBASE_19, e, false);
  }
}
 
Example 4
Source Project: components   Source File: CouchbaseInputTestIT.java    License: Apache License 2.0 6 votes vote down vote up
private void populateBucket() {
    CouchbaseEnvironment env = DefaultCouchbaseEnvironment
            .builder()
            .socketConnectTimeout(60000)
            .connectTimeout(60000)
            .keepAliveInterval(60000)
            .keyValueServiceConfig(KeyValueServiceConfig.create(60)) // If skip this config, we may get TimeoutException https://forums.couchbase.com/t/kv-upsert-throwing-timeoutexception-couchbase-4-5/9399
            .build();
    CouchbaseCluster cluster = CouchbaseCluster.create(env, bootstrapNodes);
    Bucket bucket = cluster.openBucket(bucketName, password);
    LOGGER.info("Connected to bucket - " + bucketName);
    assertTrue(bucket.bucketManager().flush());
    JsonDocument document = JsonDocument.create("foo", JsonObject.create().put("bar", 42));
    bucket.upsert(document, PersistTo.MASTER);
    bucket.close();
    LOGGER.info("Bucket is closed after upserting data");
    if (cluster != null) {
        cluster.disconnect();
    }
}
 
Example 5
Source Project: incubator-pinot   Source File: CouchbaseCacheDAO.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Insert a TimeSeriesDataPoint into Couchbase. If a document for this data point already
 * exists within Couchbase and the data value for the metricURN already exists in the cache,
 * we don't do anything. An example document generated and inserted for this might look like:
 * {
 *   "timestamp": 123456700000
 *   "metricId": 123456,
 *   "61427020": "3.0",
 *   "83958352": "59.6",
 *   "98648743": "0.0"
 * }
 * @param point data point
 */
public void insertTimeSeriesDataPoint(TimeSeriesDataPoint point) {

  JsonDocument doc = bucket.getAndTouch(point.getDocumentKey(), CacheConfig.getInstance().getCentralizedCacheSettings().getTTL());
  ThirdeyeMetricsUtil.couchbaseCallCounter.inc();

  if (doc == null) {
    JsonObject documentBody = CacheUtils.buildDocumentStructure(point);
    doc = JsonDocument.create(point.getDocumentKey(), CacheConfig.getInstance().getCentralizedCacheSettings().getTTL(), documentBody);
  } else {
    JsonObject dimensions = doc.content();
    if (dimensions.containsKey(point.getMetricUrnHash())) {
      return;
    }

    dimensions.put(point.getMetricUrnHash(), point.getDataValueAsDouble());
  }

  bucket.upsert(doc);
  ThirdeyeMetricsUtil.couchbaseWriteCounter.inc();
}
 
Example 6
Source Project: tutorials   Source File: ClusterServiceImpl.java    License: MIT License 6 votes vote down vote up
@Override
public List<JsonDocument> getDocumentsAsync(final AsyncBucket asyncBucket, Iterable<String> keys) {
    Observable<JsonDocument> asyncBulkGet = Observable.from(keys).flatMap(new Func1<String, Observable<JsonDocument>>() {
        public Observable<JsonDocument> call(String key) {
            return asyncBucket.get(key);
        }
    });

    final List<JsonDocument> docs = new ArrayList<>();
    try {
        asyncBulkGet.toBlocking().forEach(new Action1<JsonDocument>() {
            public void call(JsonDocument doc) {
                docs.add(doc);
            }
        });
    } catch (Exception e) {
        logger.error("Error during bulk get", e);
    }

    return docs;
}
 
Example 7
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenDocuments_whenBatchInsert_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();

    List<JsonDocument> documents = IntStream.rangeClosed(0,10)
      .mapToObj( i -> {
        JsonObject content = JsonObject.create()
          .put("id", i)
          .put("type", "airline")
          .put("name", "Sample Airline "  + i);
        return JsonDocument.create("cust_" + i, content);
      })
      .collect(Collectors.toList());

    List<JsonDocument> r5 = Observable
      .from(documents)
      .flatMap(doc -> bucket.async().insert(doc))
      .toList()
      .last()
      .toBlocking()
      .single();

    r5.forEach(System.out::println);
}
 
Example 8
Source Project: java-specialagent   Source File: CouchbaseClientTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test(final MockTracer tracer) {
  final Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder().connectTimeout(TimeUnit.SECONDS.toMillis(60)).build());
  final Bucket bucket = cluster.openBucket(bucketName);

  final JsonObject arthur = JsonObject.create()
    .put("name", "Arthur")
    .put("email", "[email protected]")
    .put("interests", JsonArray.from("Holy Grail", "African Swallows"));

  bucket.upsert(JsonDocument.create("u:king_arthur", arthur));
  System.out.println(bucket.get("u:king_arthur"));

  cluster.disconnect(60, TimeUnit.SECONDS);

  final List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(6, spans.size());

  boolean foundCouchbaseSpan = false;
  for (final MockSpan span : spans) {
    final String component = (String)span.tags().get(Tags.COMPONENT.getKey());
    if (component != null && component.startsWith("couchbase-java-client")) {
      foundCouchbaseSpan = true;
      break;
    }
  }

  assertTrue("couchbase-java-client span not found", foundCouchbaseSpan);
}
 
Example 9
Source Project: java-specialagent   Source File: CouchbaseClientITest.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws BucketAlreadyExistsException, InterruptedException, IOException {
  final CouchbaseMock couchbaseMock = new CouchbaseMock("localhost", 8091, 2, 1);
  final BucketConfiguration bucketConfiguration = new BucketConfiguration();
  bucketConfiguration.name = bucketName;
  bucketConfiguration.numNodes = 1;
  bucketConfiguration.numReplicas = 1;
  bucketConfiguration.password = "";
  couchbaseMock.start();
  couchbaseMock.waitForStartup();
  couchbaseMock.createBucket(bucketConfiguration);

  final Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder().connectTimeout(TimeUnit.SECONDS.toMillis(60)).build());
  final Bucket bucket = cluster.openBucket(bucketName);

  final JsonObject arthur = JsonObject
    .create().put("name", "Arthur")
    .put("email", "[email protected]")
    .put("interests", JsonArray.from("Holy Grail", "African Swallows"));

  bucket.upsert(JsonDocument.create("u:king_arthur", arthur));

  System.out.println(bucket.get("u:king_arthur"));

  cluster.disconnect(60, TimeUnit.SECONDS);
  couchbaseMock.stop();

  TestUtil.checkSpan(new ComponentSpanCount("couchbase-java-client.*", 2));
}
 
Example 10
Source Project: NetDiscovery   Source File: CouchbasePipeline.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void process(ResultItems resultItems) {

    JsonObject content = JsonObject.create();
    for (Map.Entry<String, Object> entry : resultItems.getAll().entrySet()) {

        content.put(entry.getKey(),entry.getValue());
    }

    bucket.upsert(JsonDocument.create(documentId, content));

    // Close all buckets and disconnect
    cluster.disconnect();
}
 
Example 11
Source Project: java-dcp-client   Source File: WorkloadGenerator.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String... args) throws Exception {
  CouchbaseCluster cluster = CouchbaseCluster.create("127.0.0.1").authenticate("Administrator", "password");
  Bucket bucket = cluster.openBucket("default");

  while (true) {
    for (int i = 0; i < 1024; i++) {
      bucket.upsert(JsonDocument.create("doc:" + i, JsonObject.create().put("uuid", UUID.randomUUID().toString())));
      Thread.sleep(1000);
    }
  }
}
 
Example 12
Source Project: serverless   Source File: ButtonDocument.java    License: Apache License 2.0 5 votes vote down vote up
JsonDocument toJson() {
    ObjectMapper mapper = new ObjectMapper();
    String json;
    try {
        json = mapper.writeValueAsString(this);
    } catch (JsonProcessingException ex) {
        throw new RuntimeException(ex);
    }

    return JsonDocument.create(getId(), JsonObject.fromJson(json));
}
 
Example 13
Source Project: tutorials   Source File: StudentServiceLiveTest.java    License: MIT License 5 votes vote down vote up
@BeforeClass
public static void setupBeforeClass() {
    Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST);
    Bucket bucket = cluster.openBucket(MyCouchbaseConfig.BUCKET_NAME, MyCouchbaseConfig.BUCKET_PASSWORD);
    bucket.upsert(JsonDocument.create(joeCollegeId, jsonJoeCollege));
    bucket.upsert(JsonDocument.create(judyJetsonId, jsonJudyJetson));
    bucket.close();
    cluster.disconnect();
}
 
Example 14
@Override
public void assertUnlocked(String lockName) {
    JsonDocument lockDocument = bucket.get(lockName);
    assertThat(parse((String) lockDocument.content().get(LOCK_UNTIL))).isBeforeOrEqualTo(now());
    assertThat(parse((String) lockDocument.content().get(LOCKED_AT))).isBefore(now());
    assertThat(lockDocument.content().get(LOCKED_BY)).asString().isNotEmpty();
}
 
Example 15
@Override
public void assertLocked(String lockName) {
    JsonDocument lockDocument = bucket.get(lockName);
    assertThat(parse((String) lockDocument.content().get(LOCK_UNTIL))).isAfter(now());
    assertThat(parse((String) lockDocument.content().get(LOCKED_AT))).isBefore(now());
    assertThat(lockDocument.content().get(LOCKED_BY)).asString().isNotEmpty();
}
 
Example 16
Source Project: samza   Source File: CouchbaseTableWriteFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> putAsync(String key, V record) {
  Preconditions.checkArgument(StringUtils.isNotBlank(key), "key must not be null, empty or blank");
  Preconditions.checkArgument(!key.contains(" "), String.format("key should not contain spaces: %s", key));
  Preconditions.checkNotNull(record);
  Document<?> document = record instanceof JsonObject
      ? JsonDocument.create(key, (int) ttl.getSeconds(), (JsonObject) record)
      : BinaryDocument.create(key, (int) ttl.getSeconds(), Unpooled.copiedBuffer(valueSerde.toBytes(record)));
  return asyncWriteHelper(
      bucket.async().upsert(document, timeout.toMillis(), TimeUnit.MILLISECONDS),
      String.format("Failed to insert key %s into bucket %s", key, bucketName));
}
 
Example 17
Source Project: tutorials   Source File: StudentGradeServiceLiveTest.java    License: MIT License 5 votes vote down vote up
private void printViewResultDocuments(ViewResult result) {
    for(ViewRow row : result.allRows()) {
        JsonDocument doc = row.document();
        String key = doc.id();
        logger.info(key + "=" + doc.content().toString());
    }
}
 
Example 18
Source Project: incubator-gobblin   Source File: CouchbaseTestServer.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public static void testServer()
    throws InterruptedException, IOException {
  CouchbaseTestServer couchbaseTestServer = new CouchbaseTestServer(TestUtils.findFreePort());
  couchbaseTestServer.start();

  int port = couchbaseTestServer.getPort();
  int serverPort = couchbaseTestServer.getServerPort();



  try {
    CouchbaseEnvironment cbEnv = DefaultCouchbaseEnvironment.builder().bootstrapHttpEnabled(true)
        .bootstrapHttpDirectPort(port)
        .bootstrapCarrierDirectPort(serverPort)
        .connectTimeout(TimeUnit.SECONDS.toMillis(15))
        .bootstrapCarrierEnabled(true).build();
    CouchbaseCluster cbCluster = CouchbaseCluster.create(cbEnv, "localhost");
    Bucket bucket = cbCluster.openBucket("default","");
    try {
      JsonObject content = JsonObject.empty().put("name", "Michael");
      JsonDocument doc = JsonDocument.create("docId", content);
      JsonDocument inserted = bucket.insert(doc);
    }
    catch (Exception e)
    {
      Assert.fail("Should not throw exception on insert", e);
    }
  }
  finally
  {
    couchbaseTestServer.stop();
  }
}
 
Example 19
Source Project: nosql4idea   Source File: CouchbaseClientTest.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment
            .builder()
            .queryEnabled(true)
            .build());

    Bucket defaultBucket = cluster.openBucket("default");
    defaultBucket.remove("user:walter");

    JsonArray friends = JsonArray.empty()
            .add(JsonObject.empty().put("name", "Mike Ehrmantraut"))
            .add(JsonObject.empty().put("name", "Jesse Pinkman"));

    JsonObject content = JsonObject.empty()
            .put("firstname", "Walter")
            .put("lastname", "White")
            .put("age", 52)
            .put("aliases", JsonArray.from("Walt Jackson", "Mr. Mayhew", "David Lynn"))
            .put("friends", friends);
    JsonDocument walter = JsonDocument.create("user:walter", content);
    JsonDocument inserted = defaultBucket.insert(walter);

    JsonDocument foundGuy = defaultBucket.get("user:walter");
    System.out.println(foundGuy.content().toMap());


    Bucket beerBucket = cluster.openBucket("beer-sample");
    N1qlQueryResult result = beerBucket.query(N1qlQuery.simple(select("*").from(i("beer-sample")).limit(10)));

    System.out.println("Errors found: " + result.errors());

    for (N1qlQueryRow row : result.allRows()) {
        JsonObject jsonObject = row.value();
        System.out.println(jsonObject.toMap());
    }

    cluster.disconnect();
}
 
Example 20
Source Project: hazelcast-simulator   Source File: BucketReadWriteTest.java    License: Apache License 2.0 5 votes vote down vote up
@Prepare(global = true)
public void prepare() {
    Random random = new Random();
    for (int k = 0; k < keyDomain; k++) {
        JsonObject content = JsonObject.create().put("x", values[random.nextInt(valueCount)]);
        bucket.upsert(JsonDocument.create("" + k, content));
    }
}
 
Example 21
Source Project: tutorials   Source File: ClusterServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public List<JsonDocument> getDocuments(Bucket bucket, Iterable<String> keys) {
    List<JsonDocument> docs = new ArrayList<>();
    for (String key : keys) {
        JsonDocument doc = bucket.get(key);
        if (doc != null) {
            docs.add(doc);
        }
    }
    return docs;
}
 
Example 22
Source Project: tutorials   Source File: PersonCrudService.java    License: MIT License 5 votes vote down vote up
@Override
public void create(Person person) {
    if (person.getId() == null) {
        person.setId(UUID.randomUUID().toString());
    }
    JsonDocument document = converter.toDocument(person);
    bucket.insert(document);
}
 
Example 23
Source Project: tutorials   Source File: StudentServiceImplLiveTest.java    License: MIT License 5 votes vote down vote up
@BeforeClass
public static void setupBeforeClass() {
    Cluster cluster = CouchbaseCluster.create(MultiBucketCouchbaseConfig.NODE_LIST);
    Bucket bucket = cluster.openBucket(MultiBucketCouchbaseConfig.DEFAULT_BUCKET_NAME, MultiBucketCouchbaseConfig.DEFAULT_BUCKET_PASSWORD);
    bucket.upsert(JsonDocument.create(joeCollegeId, jsonJoeCollege));
    bucket.upsert(JsonDocument.create(judyJetsonId, jsonJudyJetson));
    bucket.close();
    cluster.disconnect();
}
 
Example 24
Source Project: tutorials   Source File: PersonDocumentConverter.java    License: MIT License 5 votes vote down vote up
@Override
public Person fromDocument(JsonDocument doc) {
    JsonObject content = doc.content();
    Person p = new Person();
    p.setId(doc.id());
    p.setType("Person");
    p.setName(content.getString("name"));
    p.setHomeTown(content.getString("homeTown"));
    return p;
}
 
Example 25
Source Project: tutorials   Source File: StudentGradeService.java    License: MIT License 5 votes vote down vote up
public String insert(StudentGrade studentGrade) throws DuplicateKeyException {
    String id = keyGenerator.generateKey(studentGrade);
    if(bucket.exists(id)) {
        throw new DuplicateKeyException("document already exists with key " + id);
    }
    JsonObject content = JsonObject.empty()
            .put("type", "StudentGrade")
            .put("name", studentGrade.getName())
            .put("course", studentGrade.getCourse())
            .put("grade", studentGrade.getGrade())
            .put("hours", studentGrade.getHours());
    JsonDocument doc = JsonDocument.create(id, content);
    bucket.insert(doc);
    return id;
}
 
Example 26
Source Project: tutorials   Source File: StudentGradeService.java    License: MIT License 5 votes vote down vote up
private List<JsonDocument> extractDocuments(ViewResult result) {
    List<JsonDocument> docs = new ArrayList<>();
    for(ViewRow row : result.allRows()) {
        JsonDocument doc = row.document();
        docs.add(doc);
    }
    return docs;
}
 
Example 27
Source Project: tutorials   Source File: CodeSnippets.java    License: MIT License 5 votes vote down vote up
static JsonDocument insertExample(Bucket bucket) {
    JsonObject content = JsonObject.empty().put("name", "John Doe").put("type", "Person").put("email", "[email protected]").put("homeTown", "Chicago");
    String id = UUID.randomUUID().toString();
    JsonDocument document = JsonDocument.create(id, content);
    JsonDocument inserted = bucket.insert(document);
    return inserted;
}
 
Example 28
Source Project: tutorials   Source File: CodeSnippets.java    License: MIT License 5 votes vote down vote up
static JsonDocument retrieveAndUpsertExample(Bucket bucket, String id) {
    JsonDocument document = bucket.get(id);
    JsonObject content = document.content();
    content.put("homeTown", "Kansas City");
    JsonDocument upserted = bucket.upsert(document);
    return upserted;
}
 
Example 29
Source Project: tutorials   Source File: CodeSnippets.java    License: MIT License 5 votes vote down vote up
static JsonDocument replaceExample(Bucket bucket, String id) {
    JsonDocument document = bucket.get(id);
    JsonObject content = document.content();
    content.put("homeTown", "Milwaukee");
    JsonDocument replaced = bucket.replace(document);
    return replaced;
}
 
Example 30
Source Project: tutorials   Source File: CodeSnippets.java    License: MIT License 5 votes vote down vote up
static JsonDocument getFirstFromReplicaExample(Bucket bucket, String id) {
    try {
        return bucket.get(id);
    } catch (CouchbaseException e) {
        List<JsonDocument> list = bucket.getFromReplica(id, ReplicaMode.FIRST);
        if (!list.isEmpty()) {
            return list.get(0);
        }
    }
    return null;
}