问题
I am new with coucbase,i have some doubt regarding the key value storage in couchbase. Normally we store data as document. I need clarification for bellow queries ,
- What is the difference between document type and key-value type?
- How can i achieve key - value storage ? Can you explain with a small example.
- what is the benefit of storing as key-value?
回答1:
- What is the difference between document type and key-value type?
In Couchbase you can store any key/value pairs. At this level keys and values are just byte arrays. However, if the value you store happens to be valid JSON, then there is additional functionality (such as views) that become available. You can mix and match within the same bucket. Sometimes its useful to use integer counters, or comma-delimited string lists along side regular JSON documents in the same bucket. Note however that the Couchbase Elasticsearch adapter ONLY works with JSON documents. If you store plain key/value items in a bucket, they will be ignored by the Elasticsearch adapter.
How can i achieve key - value storage ? Can you explain with a small example.
// Connect to localhost or to the appropriate URIuris.add(URI.create("http://localhost:8091/pools")); CouchbaseClient client = null; client = new CouchbaseClient(uris, "streams", ""); client.add("1234", "xxx"); client.replace("1234", "1234"); Object data = client.get("1234"); System.out.println(data.toString()); client.delete("1234");
what is the benefit of storing as key-value?
Typically the benefit is maximum performance for a couple of reasons.
- you don't need to JSON encode/decode the value
- operations like Incr() only work on values which are integers
- operations like Append() only work on values which are strings
- using these operations are special case operations that can allow you to avoid Get/Set/Cas retry operations
来源:https://stackoverflow.com/questions/26527466/what-is-the-key-value-storage-in-couch-base