Search in sources :

Example 1 with JsonDocument

use of com.couchbase.client.java.document.JsonDocument in project sling by apache.

the class CouchbaseNoSqlAdapter method store.

@Override
public boolean store(NoSqlData data) {
    Bucket bucket = couchbaseClient.getBucket();
    String cacheKey = CouchbaseKey.build(data.getPath(), cacheKeyPrefix);
    JsonObject envelope = JsonObject.create();
    envelope.put(PN_PATH, data.getPath());
    envelope.put(PN_DATA, JsonObject.from(data.getProperties(MultiValueMode.LISTS)));
    // for list-children query efficiency store parent path as well
    String parentPath = ResourceUtil.getParent(data.getPath());
    if (parentPath != null) {
        envelope.put(PN_PARENT_PATH, parentPath);
    }
    JsonDocument doc = JsonDocument.create(cacheKey, envelope);
    try {
        bucket.insert(doc);
        // created
        return true;
    } catch (DocumentAlreadyExistsException ex) {
        bucket.upsert(doc);
        // updated
        return false;
    }
}
Also used : Bucket(com.couchbase.client.java.Bucket) DocumentAlreadyExistsException(com.couchbase.client.java.error.DocumentAlreadyExistsException) JsonObject(com.couchbase.client.java.document.json.JsonObject) JsonDocument(com.couchbase.client.java.document.JsonDocument)

Example 2 with JsonDocument

use of com.couchbase.client.java.document.JsonDocument in project incubator-gobblin by apache.

the class CouchbaseTestServer method testServer.

@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();
    }
}
Also used : Bucket(com.couchbase.client.java.Bucket) CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster) JsonObject(com.couchbase.client.java.document.json.JsonObject) CouchbaseEnvironment(com.couchbase.client.java.env.CouchbaseEnvironment) DefaultCouchbaseEnvironment(com.couchbase.client.java.env.DefaultCouchbaseEnvironment) JsonDocument(com.couchbase.client.java.document.JsonDocument) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 3 with JsonDocument

use of com.couchbase.client.java.document.JsonDocument in project components by Talend.

the class CouchbaseInputTestIT method populateBucket.

private void populateBucket() {
    CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder().socketConnectTimeout(60000).connectTimeout(60000).keepAliveInterval(60000).build();
    CouchbaseCluster cluster = CouchbaseCluster.create(env, bootstrapNodes);
    Bucket bucket = cluster.openBucket(bucketName, password);
    assertTrue(bucket.bucketManager().flush());
    JsonDocument document = JsonDocument.create("foo", JsonObject.create().put("bar", 42));
    bucket.upsert(document, PersistTo.MASTER);
    bucket.close();
    cluster.disconnect();
}
Also used : Bucket(com.couchbase.client.java.Bucket) CouchbaseCluster(com.couchbase.client.java.CouchbaseCluster) CouchbaseEnvironment(com.couchbase.client.java.env.CouchbaseEnvironment) DefaultCouchbaseEnvironment(com.couchbase.client.java.env.DefaultCouchbaseEnvironment) JsonDocument(com.couchbase.client.java.document.JsonDocument)

Example 4 with JsonDocument

use of com.couchbase.client.java.document.JsonDocument in project tutorials by eugenp.

the class PersonCrudServiceIntegrationTest method givenNewHometown_whenUpdate_thenNewHometownPersisted.

@Test
public final void givenNewHometown_whenUpdate_thenNewHometownPersisted() {
    // create and insert person document
    JsonDocument doc = insertRandomPersonDocument();
    // update person
    Person expected = converter.fromDocument(doc);
    String updatedHomeTown = RandomStringUtils.randomAlphabetic(12);
    expected.setHomeTown(updatedHomeTown);
    personService.update(expected);
    // check results
    JsonDocument actual = bucket.get(expected.getId());
    assertNotNull(actual);
    assertNotNull(actual.content());
    assertEquals(expected.getHomeTown(), actual.content().getString("homeTown"));
    // cleanup
    bucket.remove(expected.getId());
}
Also used : Person(com.baeldung.couchbase.async.person.Person) JsonDocument(com.couchbase.client.java.document.JsonDocument) Test(org.junit.Test) AsyncIntegrationTest(com.baeldung.couchbase.async.AsyncIntegrationTest)

Example 5 with JsonDocument

use of com.couchbase.client.java.document.JsonDocument in project tutorials by eugenp.

the class PersonCrudServiceIntegrationTest method givenPersons_whenUpdateBulk_thenPersonsAreUpdated.

@Test
public final void givenPersons_whenUpdateBulk_thenPersonsAreUpdated() {
    List<String> ids = new ArrayList<>();
    // add some person documents
    for (int i = 0; i < 5; i++) {
        ids.add(insertRandomPersonDocument().id());
    }
    // load persons from Couchbase
    List<Person> persons = new ArrayList<>();
    for (String id : ids) {
        persons.add(converter.fromDocument(bucket.get(id)));
    }
    // modify persons
    for (Person person : persons) {
        person.setHomeTown(RandomStringUtils.randomAlphabetic(10));
    }
    // perform bulk update
    personService.updateBulk(persons);
    // check results
    for (Person person : persons) {
        JsonDocument doc = bucket.get(person.getId());
        assertEquals(person.getName(), doc.content().getString("name"));
        assertEquals(person.getHomeTown(), doc.content().getString("homeTown"));
    }
    // cleanup
    for (String id : ids) {
        bucket.remove(id);
    }
}
Also used : ArrayList(java.util.ArrayList) Person(com.baeldung.couchbase.async.person.Person) JsonDocument(com.couchbase.client.java.document.JsonDocument) Test(org.junit.Test) AsyncIntegrationTest(com.baeldung.couchbase.async.AsyncIntegrationTest)

Aggregations

JsonDocument (com.couchbase.client.java.document.JsonDocument)26 JsonObject (com.couchbase.client.java.document.json.JsonObject)10 Bucket (com.couchbase.client.java.Bucket)6 AsyncBucket (com.couchbase.client.java.AsyncBucket)4 Test (org.junit.Test)4 Person (com.baeldung.couchbase.async.person.Person)3 ArrayList (java.util.ArrayList)3 Func1 (rx.functions.Func1)3 AsyncIntegrationTest (com.baeldung.couchbase.async.AsyncIntegrationTest)2 CouchbaseCluster (com.couchbase.client.java.CouchbaseCluster)2 CouchbaseEnvironment (com.couchbase.client.java.env.CouchbaseEnvironment)2 DefaultCouchbaseEnvironment (com.couchbase.client.java.env.DefaultCouchbaseEnvironment)2 ViewRow (com.couchbase.client.java.view.ViewRow)2 Observable (rx.Observable)2 CodeSnippets.extractJsonResult (com.baeldung.couchbase.n1ql.CodeSnippets.extractJsonResult)1 BackpressureException (com.couchbase.client.core.BackpressureException)1 Cluster (com.couchbase.client.java.Cluster)1 RawJsonDocument (com.couchbase.client.java.document.RawJsonDocument)1 JsonArray (com.couchbase.client.java.document.json.JsonArray)1 DocumentAlreadyExistsException (com.couchbase.client.java.error.DocumentAlreadyExistsException)1