예제 #1
0
cluster = Cluster.connect("couchbases://127.0.0.1", ClusterOptions(PasswordAuthenticator("user", "pass")))
bucket = cluster.bucket("travel-sample")
collection = bucket.default_collection()

# tag::upsertandget[]
upsert_result = collection.upsert("mydoc-id", {})
get_result = collection.get("mydoc-id")
# end::upsertandget[]

from couchbase_v2.bucket import Bucket

# tag::upsertandget_sdk2[]
# SDK 2 upsert and get
bucket = Bucket("couchbases://127.0.0.1/default")
upsert_result = bucket.upsert("mydoc-id", {})
get_result = bucket.get("mydoc-id")
# end::upsertandget_sdk2[]

import couchbase_tests.base

#natag::rawjson[]
# TODO: update when implemented
from couchbase.collection import UpsertOptions
from couchbase_core.transcoder import Transcoder

class RawJSONTranscoder(Transcoder):
    pass

content = "{}".encode("UTF_8")
upsert_result = collection.upsert(
    "mydoc-id",
예제 #2
0
from couchbase_v2.bucket import Bucket
from couchbase_core.exceptions import KeyExistsError

# Connect to the default bucket on local host
cb = Bucket('couchbase://127.0.0.1/default')

# If you want to store the Python objects pickled and not as JSON
#cb.default_format = FMT_PICKLE

# Store a document
rv = cb.upsert('first', {'hello': 'world'})
cas = rv.cas
print(rv)

# Get the document
item = cb.get('first')
print(item)

# Overwrite the existing document only if the CAS value matched
try:
    # An exception will be raised if the CAS doesn't match
    wrong_cas = cas + 123
    cb.upsert('first', {'hello': 'world', 'additional': True}, cas=wrong_cas)
except KeyExistsError:
    # Get the correct current CAS value
    rv = cb.get('first')
    item, flags, correct_cas = rv.value, rv.flags, rv.cas
    # Set it again, this time with the correct CAS value
    rv = cb.upsert('first', {
        'hello': 'world',
        'additional': True
from couchbase_v2.bucket import Bucket


class ReverseTranscoder(Transcoder):
    def encode_key(self, key):
        return super(ReverseTranscoder, self).encode_key(key[::-1])

    def decode_key(self, key):
        key = super(ReverseTranscoder, self).decode_key(key)
        return key[::-1]


c_reversed = Bucket('couchbase://localhost/default',
                    transcoder=ReverseTranscoder())
c_plain = Bucket('couchbase://localhost/default')

c_plain.remove_multi(('ABC', 'CBA', 'XYZ', 'ZYX'), quiet=True)

c_reversed.upsert("ABC", "This is a reversed key")

rv = c_plain.get("CBA")
print("Got value for reversed key '{0}'".format(rv.value))

rv = c_reversed.get("ABC")
print("Got value for reversed key '{0}' again".format(rv.value))

c_plain.upsert("ZYX", "This is really ZYX")

rv = c_reversed.get("XYZ")
print("Got value for '{0}': '{1}'".format(rv.key, rv.value))