Created
June 21, 2014 22:34
-
-
Save pbojinov/e3533c4646bfbfed1bd3 to your computer and use it in GitHub Desktop.
Here is how you can create a table, with indexes, using PynamoDB - http://jlafon.io/pynamodb.html
This file contains 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.indexes import GlobalSecondaryIndex, AllProjection | |
from pynamodb.attributes import UnicodeAttribute, NumberAttribute | |
class DaysIndex(GlobalSecondaryIndex): | |
""" | |
This class represents a global secondary index | |
""" | |
read_capacity_units = 2 | |
write_capacity_units = 1 | |
projection = AllProjection() | |
days_old = NumberAttribute(hash_key=True) | |
class HackerNewsLinks(Model): | |
""" | |
A test model that uses a global secondary index | |
""" | |
table_name = 'HackerNews' | |
link = UnicodeAttribute(hash_key=True) | |
title = UnicodeAttribute() | |
days_index = DaysIndex() | |
days_old = NumberAttribute(default=0) | |
if not HackerNewsLinks.exists(): | |
HackerNewsLinks.create_table(read_capacity_units=1, write_capacity_units=1) | |
# Indexes can be queried easily using the index's hash key | |
for item in HackerNewsLinks.day_index.query(1): | |
print("Item queried from index: {0}".format(item)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice!