com.couchbase.client.java.document.JsonDocument Java Examples
The following examples show how to use
com.couchbase.client.java.document.JsonDocument.
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 File: CouchbaseProcessor.java From datacollector with Apache License 2.0 | 6 votes |
/** * 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 #2
Source File: N1QLLiveTest.java From tutorials with MIT License | 6 votes |
@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 #3
Source File: CouchbaseLockProvider.java From ShedLock with Apache License 2.0 | 6 votes |
@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 #4
Source File: ClusterServiceImpl.java From tutorials with MIT License | 6 votes |
@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 #5
Source File: CouchbaseLockProvider.java From ShedLock with Apache License 2.0 | 6 votes |
@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 #6
Source File: CouchbaseInputTestIT.java From components with Apache License 2.0 | 6 votes |
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 #7
Source File: CouchbaseCacheDAO.java From incubator-pinot with Apache License 2.0 | 6 votes |
/** * 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 #8
Source File: N1QLLiveTest.java From tutorials with MIT License | 5 votes |
@Test public void givenDocument_whenInsert_thenResults() { Bucket bucket = bucketFactory.getTravelSampleBucket(); JsonObject ob = JsonObject.create() .put("id", "1293") .put("name", "Sample Airline") .put("type", "airline"); bucket.insert(JsonDocument.create("cust1295", ob)); }
Example #9
Source File: AbstractCrudService.java From tutorials with MIT License | 5 votes |
@Override public void deleteBulk(Iterable<String> ids) { final AsyncBucket asyncBucket = bucket.async(); Observable.from(ids).flatMap(new Func1<String, Observable<JsonDocument>>() { @SuppressWarnings("unchecked") @Override public Observable<JsonDocument> call(String key) { return asyncBucket.remove(key).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build()); } }).last().toBlocking().single(); }
Example #10
Source File: StudentServiceImplLiveTest.java From tutorials with MIT License | 5 votes |
@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 #11
Source File: PersonDocumentConverter.java From tutorials with MIT License | 5 votes |
@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 #12
Source File: PersonCrudService.java From tutorials with MIT License | 5 votes |
@Override public void create(Person person) { if (person.getId() == null) { person.setId(UUID.randomUUID().toString()); } JsonDocument document = converter.toDocument(person); bucket.insert(document); }
Example #13
Source File: N1QLLiveTest.java From tutorials with MIT License | 5 votes |
@Test public void givenDocument_whenInsert_thenResult() { Bucket bucket = bucketFactory.getTestBucket(); JsonObject personObj = JsonObject.create() .put("name", "John") .put("email", "[email protected]") .put("interests", JsonArray.from("Java", "Nigerian Jollof")); String id = UUID.randomUUID().toString(); JsonDocument doc = JsonDocument.create(id, personObj); bucket.insert(doc); assertNotNull(bucket.get(id)); }
Example #14
Source File: PersonDocumentConverter.java From tutorials with MIT License | 5 votes |
@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 #15
Source File: ClusterServiceImpl.java From tutorials with MIT License | 5 votes |
@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 #16
Source File: CouchbaseClientTest.java From java-specialagent with Apache License 2.0 | 5 votes |
@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 #17
Source File: CouchbaseLockProviderIntegrationTest.java From ShedLock with Apache License 2.0 | 5 votes |
@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 #18
Source File: N1QLLiveTest.java From tutorials with MIT License | 5 votes |
@Test public void givenDocument_whenUpsert_thenUpdate() { Bucket bucket = bucketFactory.getTravelSampleBucket(); JsonObject o2 = JsonObject.create() .put("name", "Sample Airline Updated"); bucket.upsert(JsonDocument.create("cust_1", o2)); }
Example #19
Source File: StudentGradeService.java From tutorials with MIT License | 5 votes |
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 #20
Source File: AbstractCrudService.java From tutorials with MIT License | 5 votes |
@Override public void updateBulk(Iterable<T> items) { final AsyncBucket asyncBucket = bucket.async(); Observable.from(items).flatMap(new Func1<T, Observable<JsonDocument>>() { @SuppressWarnings("unchecked") @Override public Observable<JsonDocument> call(final T t) { JsonDocument doc = converter.toDocument(t); return asyncBucket.upsert(doc).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build()); } }).last().toBlocking().single(); }
Example #21
Source File: StudentGradeService.java From tutorials with MIT License | 5 votes |
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 #22
Source File: AbstractCrudService.java From tutorials with MIT License | 5 votes |
@Override public void createBulk(Iterable<T> items) { final AsyncBucket asyncBucket = bucket.async(); Observable.from(items).flatMap(new Func1<T, Observable<JsonDocument>>() { @SuppressWarnings("unchecked") @Override public Observable<JsonDocument> call(final T t) { if (t.getId() == null) { t.setId(UUID.randomUUID().toString()); } JsonDocument doc = converter.toDocument(t); return asyncBucket.insert(doc).retryWhen(RetryBuilder.anyOf(BackpressureException.class).delay(Delay.exponential(TimeUnit.MILLISECONDS, 100)).max(10).build()); } }).last().toBlocking().single(); }
Example #23
Source File: PersonServiceLiveTest.java From tutorials with MIT License | 5 votes |
@BeforeClass public static void setupBeforeClass() { final Cluster cluster = CouchbaseCluster.create(MyCouchbaseConfig.NODE_LIST); final Bucket bucket = cluster.openBucket(MyCouchbaseConfig.BUCKET_NAME, MyCouchbaseConfig.BUCKET_PASSWORD); bucket.upsert(JsonDocument.create(johnSmithId, jsonJohnSmith)); bucket.upsert(JsonDocument.create(foobarId, jsonFooBar)); bucket.close(); cluster.disconnect(); }
Example #24
Source File: AbstractCrudService.java From tutorials with MIT License | 5 votes |
@Override public void create(T t) { if (t.getId() == null) { t.setId(UUID.randomUUID().toString()); } JsonDocument doc = converter.toDocument(t); bucket.insert(doc); }
Example #25
Source File: PersonServiceImplLiveTest.java From tutorials with MIT License | 5 votes |
@BeforeClass public static void setupBeforeClass() { final Cluster cluster = CouchbaseCluster.create(MultiBucketCouchbaseConfig.NODE_LIST); final Bucket bucket = cluster.openBucket(MultiBucketCouchbaseConfig.DEFAULT_BUCKET_NAME, MultiBucketCouchbaseConfig.DEFAULT_BUCKET_PASSWORD); bucket.upsert(JsonDocument.create(johnSmithId, jsonJohnSmith)); bucket.upsert(JsonDocument.create(foobarId, jsonFooBar)); bucket.close(); cluster.disconnect(); }
Example #26
Source File: CodeSnippets.java From tutorials with MIT License | 5 votes |
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 #27
Source File: CodeSnippets.java From tutorials with MIT License | 5 votes |
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 #28
Source File: CodeSnippets.java From tutorials with MIT License | 5 votes |
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 #29
Source File: CodeSnippets.java From tutorials with MIT License | 5 votes |
static JsonDocument getLatestReplicaVersion(Bucket bucket, String id) { long maxCasValue = -1; JsonDocument latest = null; for (JsonDocument replica : bucket.getFromReplica(id, ReplicaMode.ALL)) { if (replica.cas() > maxCasValue) { latest = replica; maxCasValue = replica.cas(); } } return latest; }
Example #30
Source File: CodeSnippets.java From tutorials with MIT License | 5 votes |
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; }