-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-record-as-document.py
77 lines (73 loc) · 2.46 KB
/
1-record-as-document.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
from args import options
import aerospike
from aerospike import exception as ex
from aerospike_helpers.operations import list_operations as lh
from aerospike_helpers.operations import map_operations as mh
from aerospike_helpers.operations import operations as oh
import pprint
import sys
if options.set == "None":
options.set = None
config = {"hosts": [(options.host, options.port)]}
if options.alternate:
config["use_services_alternate"] = True
try:
client = aerospike.client(config).connect(options.username, options.password)
except ex.ClientError as e:
print("failed to connect to the cluster with", config["hosts"])
sys.exit(1)
pp = pprint.PrettyPrinter(indent=2)
key = ("test", "workshop", "record-as-document")
try:
client.remove(key)
except:
pass
try:
input("Begin?")
print("\nCreate the record with a single bin, integer value")
client.put(key, {"i": 1}, policy={"key": aerospike.POLICY_KEY_SEND})
try:
# Get the key by its digest
digest = client.get_key_digest("test", "workshop", "record-as-document")
print("Done.\nThis record's digest is {}".format(digest))
print("Get the current record by its digest")
by_digest = ("test", "workshop", None, digest)
k, m, b = client.get(by_digest, {"key": aerospike.POLICY_KEY_SEND})
pp.pprint(b)
except ex.RecordNotFound:
print("Record not found:", key)
input("Continue?")
print("\nAdd a list bin and a map bin to the record")
example_map = {"b": 2, "d": 4, "💀": ["🦄", {"🌈": "💰"}]}
ops = [
oh.write("l", ["🐟", "🌮"]),
oh.write("m", example_map),
mh.map_set_policy("m", {"map_order": aerospike.MAP_KEY_ORDERED}),
]
k, m, b = client.operate_ordered(
key, ops, policy={"key": aerospike.POLICY_KEY_SEND}
)
print("Document structure:")
k, m, b = client.get(key)
pp.pprint(b)
input("Continue?")
print(
"\nAdd an 🥑 to the list at 'l'; increment the 'i' counter by 3; add"
"{a: 1, z:26} to the map at 'm'"
)
k, m, b = client.operate_ordered(
key,
[
oh.increment("i", 3),
lh.list_append("l", "🥑"),
mh.map_put_items("m", {"a": 1, "z": 26}),
],
policy={"key": aerospike.POLICY_KEY_SEND},
)
print("Document structure:")
k, m, b = client.get(key)
pp.pprint(b)
except Exception as e:
print("Error: {}".format(e))
client.close()