Created
March 21, 2014 01:24
-
-
Save jlafon/9677683 to your computer and use it in GitHub Desktop.
A PynamoDB example for items larger than 64k.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pynamodb.models import Model | |
from pynamodb.attributes import ( | |
UnicodeAttribute, NumberAttribute | |
) | |
class Thread(Model): | |
table_name = "Thread" | |
forum_name = UnicodeAttribute(hash_key=True) | |
subject = UnicodeAttribute(range_key=True) | |
content = UnicodeAttribute() | |
parts = NumberAttribute(null=True) | |
# 64k of content, making the whole object larger than 64k | |
content = "word" * (1 << 14) | |
# The writes can be batched | |
with Thread.batch_write() as batch: | |
thread = Thread("ForumName", "subject") | |
# Half the content | |
thread.content = content[:32768] | |
# You could use a simple loop and counter here | |
thread.parts = 2 | |
thread_part_2 = Thread("ForumName", "subject-1") | |
thread_part_2.content = content[32768:] | |
batch.write(thread) | |
batch.write(thread_part_2) | |
# Items can be queried in batch as well | |
item = Thread.get("ForumName", "subject") | |
# Build up a list of keys (the first item is excluded because we already have it) | |
item_keys = [("ForumName", "subject-{0}".format(part) for part in range(item.parts)[1:])] | |
for item in Thread.batch_get(item_keys): | |
print(item) | |
# Alternative using query for a prefixed range key | |
for item in Thread.query("ForumName", subject__begins_with='subject-'): | |
print(item) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment