Java Code Examples for com.couchbase.client.java.Bucket#get()

The following examples show how to use com.couchbase.client.java.Bucket#get() . 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: CouchbaseClientTest.java    From nosql4idea with 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 2
Source File: CouchbaseUtils.java    From nifi with Apache License 2.0 5 votes vote down vote up
/**
 * A convenient method to retrieve String value when Document type is unknown.
 * This method uses LegacyDocument to get, then tries to convert content based on its class.
 * @param bucket the bucket to get a document
 * @param id the id of the target document
 * @return String representation of the stored value, or null if not found
 */
public static String getStringContent(Bucket bucket, String id) {
    final LegacyDocument doc = bucket.get(LegacyDocument.create(id));
    if (doc == null) {
        return null;
    }
    final Object content = doc.content();
    return getStringContent(content);
}
 
Example 3
Source File: ClusterServiceImpl.java    From tutorials with 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 4
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 5
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 6
Source File: CodeSnippets.java    From tutorials with 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;
}
 
Example 7
Source File: TestCouchbaseUtils.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Ignore("This test method requires a live Couchbase Server instance")
@Test
public void testDocumentTypesAndStringConversion() {
    final CouchbaseCluster cluster = CouchbaseCluster.fromConnectionString("couchbase://192.168.99.100:8091");
    final Bucket bucket = cluster.openBucket("b1", "b1password");

    bucket.upsert(JsonDocument.create("JsonDocument", JsonObject.create().put("one", 1)));
    bucket.upsert(JsonArrayDocument.create("JsonArray", JsonArray.create().add(1).add(2).add(3)));
    bucket.upsert(JsonDoubleDocument.create("JsonDouble", 0.123));
    bucket.upsert(JsonStringDocument.create("JsonString", "value"));
    bucket.upsert(JsonBooleanDocument.create("JsonBoolean", true));
    bucket.upsert(JsonLongDocument.create("JsonLong", 123L));

    bucket.upsert(RawJsonDocument.create("RawJsonDocument", "value"));
    bucket.upsert(StringDocument.create("StringDocument", "value"));

    bucket.upsert(BinaryDocument.create("BinaryDocument", Unpooled.copiedBuffer("value".getBytes(StandardCharsets.UTF_8))));
    bucket.upsert(ByteArrayDocument.create("ByteArrayDocument", "value".getBytes(StandardCharsets.UTF_8)));

    final String[][] expectations = {
            {"JsonDocument", "String", "{\"one\":1}"},
            {"JsonArray", "String", "[1,2,3]"},
            {"JsonDouble", "String", "0.123"},
            {"JsonString", "String", "\"value\""},
            {"JsonBoolean", "String", "true"},
            {"JsonLong", "String", "123"},
            {"RawJsonDocument", "String", "value"},
            {"StringDocument", "String", "value"},
            {"BinaryDocument", "byte[]", "value"},
            {"ByteArrayDocument", "byte[]", "value"},
    };

    for (String[] expectation : expectations) {
        final LegacyDocument document = bucket.get(LegacyDocument.create(expectation[0]));
        assertEquals(expectation[1], document.content().getClass().getSimpleName());
        assertEquals(expectation[2], CouchbaseUtils.getStringContent(document.content()));
    }

    final BinaryDocument binaryDocument = bucket.get(BinaryDocument.create("BinaryDocument"));
    final String stringFromByteBuff = CouchbaseUtils.getStringContent(binaryDocument.content());
    assertEquals("value", stringFromByteBuff);

    try {
        bucket.get(BinaryDocument.create("JsonDocument"));
        fail("Getting a JSON document as a BinaryDocument fails");
    } catch (TranscodingException e) {
        assertTrue(e.getMessage().contains("Flags (0x2000000) indicate non-binary document for id JsonDocument"));
    }

}