com.couchbase.client.java.document.json.JsonObject Java Examples
The following examples show how to use
com.couchbase.client.java.document.json.JsonObject.
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: 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 #2
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 #3
Source File: CouchbaseWriter.java From components with Apache License 2.0 | 6 votes |
public JsonObject createHierarchicalJson(Schema schema, IndexedRecord record, int idPos){ JsonObject jsonObject = JsonObject.create(); for (int i = 0; i < schema.getFields().size(); i++){ if (i == idPos) continue; Object value = record.get(i); String fieldName = schema.getFields().get(i).name(); try { JsonObject innerJson = JsonObject.fromJson(value.toString()); jsonObject.put(fieldName, innerJson); } catch (Exception e) { try { JsonArray jsonArray = JsonArray.fromJson(value.toString()); jsonObject.put(fieldName, jsonArray); } catch (Exception e2) { // This mean it's not JSON object jsonObject.put(fieldName, value); } } } return jsonObject; }
Example #4
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 #5
Source File: CouchbaseKeyValueDescriptor.java From nosql4idea with Apache License 2.0 | 6 votes |
public static CouchbaseKeyValueDescriptor createDescriptor(String key, Object value) { if (value == null) { return new CouchbaseKeyNullValueDescriptor(key); } if (value instanceof String) { return new CouchbaseKeyStringValueDescriptor(key, (String) value); } SimpleTextAttributes textAttributes = StyleAttributesProvider.getStringAttribute(); if (value instanceof Boolean) { textAttributes = StyleAttributesProvider.getBooleanAttribute(); } else if (value instanceof Number) { textAttributes = StyleAttributesProvider.getNumberAttribute(); } else if (value instanceof JsonObject || value instanceof JsonArray) { textAttributes = StyleAttributesProvider.getObjectAttribute(); } return new CouchbaseKeyValueDescriptor(key, value, textAttributes); }
Example #6
Source File: CouchbasePanelTest.java From nosql4idea with Apache License 2.0 | 6 votes |
private CouchbaseResult createResults() { CouchbaseResult result = new CouchbaseResult("test"); result.add(JsonObject.create().put("firstname", "Jojo") .put("age", 25) .put("mad", true) .put("interests", Arrays.asList("programming", "XP", "TDD")) .put("amount", 123456764L) .put("score", 12345.12121d) .put("address", JsonObject.create() .put("Street", "Av. Champs Elysées") .put("City", "Paris") .put("ZIP Code", 75016)) .put("movies", Arrays.asList( JsonObject.create() .put("title", "Fight Club") .put("critic", 8.2), JsonObject.create() .put("title", "Blade Runner") .put("critic", 9.3), JsonObject.create() .put("title", "Toys Story") .put("critic", 8.7) ))); return result; }
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: CacheUtilTest.java From incubator-pinot with Apache License 2.0 | 5 votes |
@Test public void testBuildDocumentStructureShouldMapNullStringDataValueToZero() { dataPoint.setDataValue("null"); JsonObject mappedDataPoint = CacheUtils.buildDocumentStructure(dataPoint); Assert.assertEquals(mappedDataPoint.getDouble(dataPoint.getMetricUrnHash()), (double)0); }
Example #9
Source File: TestCouchbaseTableWriteFunction.java From samza with Apache License 2.0 | 5 votes |
@Test public void testDeleteAsyncException() { String key = "throwExceptionKey"; Bucket bucket = mock(Bucket.class); AsyncBucket asyncBucket = mock(AsyncBucket.class); CouchbaseTableWriteFunction<JsonObject> writeFunction = createAndInit(bucket, asyncBucket); when(asyncBucket.remove(eq(key), anyLong(), any(TimeUnit.class))).thenReturn( Observable.error(new CouchbaseException())); assertTrue(writeFunction.deleteAsync(key).isCompletedExceptionally()); }
Example #10
Source File: CouchbaseTestServer.java From incubator-gobblin with Apache License 2.0 | 5 votes |
@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 #11
Source File: CouchbaseTreeModel.java From nosql4idea with Apache License 2.0 | 5 votes |
public static NoSqlTreeNode buildTree(CouchbaseResult couchbaseResult) { NoSqlTreeNode resultTreeNode = new NoSqlTreeNode(new CouchbaseResultDescriptor(couchbaseResult.getName())); for (JsonObject record : couchbaseResult.getRecords()) { processRecord(resultTreeNode, record); } return resultTreeNode; }
Example #12
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 #13
Source File: CouchbaseTreeModel.java From nosql4idea with Apache License 2.0 | 5 votes |
private static void processRecordListValues(NoSqlTreeNode parentNode, JsonArray values) { int index = 0; for (Object value : values) { NoSqlTreeNode currentValueNode = new NoSqlTreeNode(CouchbaseValueDescriptor.createDescriptor(index++, value)); if (value instanceof JsonArray) { processRecordListValues(currentValueNode, (JsonArray) value); } else if (value instanceof JsonObject) { processRecord(currentValueNode, (JsonObject) value); } parentNode.add(currentValueNode); } }
Example #14
Source File: CouchbaseClient.java From nosql4idea with Apache License 2.0 | 5 votes |
public CouchbaseResult loadRecords(ServerConfiguration configuration, CouchbaseDatabase database, CouchbaseQuery couchbaseQuery) { Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment .builder() .queryEnabled(true) .build(), configuration.getServerUrl()); // AuthenticationSettings authenticationSettings = configuration.getAuthenticationSettings(); // ClusterManager clusterManager = cluster.clusterManager(authenticationSettings.getUsername(), authenticationSettings.getPassword()); Bucket beerBucket = cluster.openBucket(database.getName(), 10, TimeUnit.SECONDS); N1qlQueryResult queryResult = beerBucket.query(N1qlQuery.simple(select("*").from(i(database.getName())).limit(couchbaseQuery.getLimit()))); //TODO dirty zone :( CouchbaseResult result = new CouchbaseResult(database.getName()); List<JsonObject> errors = queryResult.errors(); if (!errors.isEmpty()) { cluster.disconnect(); result.addErrors(errors); return result; } for (N1qlQueryRow row : queryResult.allRows()) { result.add(row.value()); } cluster.disconnect(); return result; }
Example #15
Source File: CouchbaseClientTest.java From nosql4idea with Apache License 2.0 | 5 votes |
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 #16
Source File: CacheUtilTest.java From incubator-pinot with Apache License 2.0 | 5 votes |
@Test public void testBuildDocumentStructureShouldMapToJsonObject() { JsonObject mappedDataPoint = CacheUtils.buildDocumentStructure(dataPoint); Assert.assertEquals(mappedDataPoint.getLong("timestamp").longValue(), 1234567); Assert.assertEquals(mappedDataPoint.getLong("metricId").longValue(), 1); Assert.assertEquals(mappedDataPoint.getDouble(dataPoint.getMetricUrnHash()), (double)100); }
Example #17
Source File: CacheUtilTest.java From incubator-pinot with Apache License 2.0 | 5 votes |
@Test public void testBuildDocumentStructureShouldMapNullDataValueToZero() { dataPoint.setDataValue(null); JsonObject mappedDataPoint = CacheUtils.buildDocumentStructure(dataPoint); Assert.assertEquals(mappedDataPoint.getDouble(dataPoint.getMetricUrnHash()), (double)0); }
Example #18
Source File: CouchbaseTreeModel.java From nosql4idea with Apache License 2.0 | 5 votes |
private static void processRecord(NoSqlTreeNode parentNode, JsonObject record) { for (String key : record.getNames()) { Object value = record.get(key); NoSqlTreeNode currentNode = new NoSqlTreeNode(CouchbaseKeyValueDescriptor.createDescriptor(key, value)); if (value instanceof JsonArray) { processRecordListValues(currentNode, (JsonArray) value); } else if (value instanceof JsonObject) { processRecord(currentNode, (JsonObject) value); } parentNode.add(currentNode); } }
Example #19
Source File: BucketReadWriteTest.java From hazelcast-simulator with Apache License 2.0 | 5 votes |
@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 #20
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 #21
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 #22
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 #23
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 #24
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 #25
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 #26
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 #27
Source File: N1QLLiveTest.java From tutorials with MIT License | 5 votes |
@Test public void givenSelectStatement_whenQuery_thenResult() { Bucket bucket = bucketFactory.getTravelSampleBucket(); String query = "SELECT name FROM `travel-sample` " + "WHERE type = 'airport' LIMIT 100"; N1qlQueryResult result1 = bucket.query(N1qlQuery.simple(query)); System.out.println("Result Count " + result1.info().resultCount()); N1qlQueryRow row = result1.allRows().get(0); JsonObject rowJson = row.value(); System.out.println("Name in First Row " + rowJson.get("name")); }
Example #28
Source File: N1QLLiveTest.java From tutorials with MIT License | 5 votes |
@Test public void givenSelectStatement2_whenQuery_thenResult() { Bucket bucket = bucketFactory.getTravelSampleBucket(); JsonObject pVal = JsonObject.create().put("type", "airport"); String query = "SELECT * FROM `travel-sample` " + "WHERE type = $type LIMIT 100"; N1qlQueryResult r2 = bucket.query(N1qlQuery.parameterized(query, pVal)); System.out.println(r2.allRows()); List<JsonNode> list = extractJsonResult(r2); System.out.println( list.get(0).get("travel-sample").get("airportname").asText()); }
Example #29
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 #30
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)); }