Java Code Examples for com.couchbase.client.java.document.json.JsonObject#put()

The following examples show how to use com.couchbase.client.java.document.json.JsonObject#put() . 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: CouchbaseLockProvider.java    From ShedLock with 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 File: CouchbaseWriter.java    From components with Apache License 2.0 6 votes vote down vote up
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 3
Source File: CouchbaseCacheDAO.java    From incubator-pinot with 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 4
Source File: CouchbasePipeline.java    From NetDiscovery with 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 5
Source File: CodeSnippets.java    From tutorials with 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 6
Source File: CodeSnippets.java    From tutorials with 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 7
Source File: CouchbaseWriterTest.java    From components with Apache License 2.0 4 votes vote down vote up
private JsonObject createStructuredJsonObject(){
    JsonObject jsonObjectInnerInner = JsonObject.create();
    jsonObjectInnerInner.put("val1", 40);
    jsonObjectInnerInner.put("val2", 20.2);
    jsonObjectInnerInner.put("val3", true);

    JsonObject jsonObjectInner = JsonObject.create();
    jsonObjectInner.put("mobile", "0989901515");
    jsonObjectInner.put("home", "0342556644");
    jsonObjectInner.put("inner", jsonObjectInnerInner);
    jsonObjectInner.put("some", "444444");

    JsonObject jsonObjectOuterOuter = JsonObject.create();
    jsonObjectOuterOuter.put("id", "0017");
    jsonObjectOuterOuter.put("name", "Patrik");
    jsonObjectOuterOuter.put("surname", "Human");
    jsonObjectOuterOuter.put("tel" ,jsonObjectInner);

    JsonArray jsonArray = JsonArray.create();
    jsonArray.add("one");
    jsonArray.add("two");
    jsonArray.add("three");

    jsonObjectOuterOuter.put("numbers", jsonArray);

    JsonObject innerArray1 = JsonObject.create();
    innerArray1.put("arr01", 1);
    innerArray1.put("arr02", 2);
    innerArray1.put("arr03", 3);

    JsonObject innerArray2 = JsonObject.create();
    innerArray2.put("arr01", 4);
    innerArray2.put("arr02", 5);
    innerArray2.put("arr03", 6);

    JsonArray jsonArray1 = JsonArray.create();
    jsonArray1.add(innerArray1);
    jsonArray1.add(innerArray2);

    jsonObjectOuterOuter.put("arrWithJSON", jsonArray1);

    return JsonObject.create().put("val_str_like_json", jsonObjectOuterOuter).put("val_str", "str");
}