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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
|
.. dynamodb_tut:
============================================
An Introduction to boto's DynamoDB interface
============================================
This tutorial focuses on the boto interface to AWS' DynamoDB_. This tutorial
assumes that you have boto already downloaded and installed.
.. _DynamoDB: http://aws.amazon.com/dynamodb/
Creating a Connection
---------------------
The first step in accessing DynamoDB is to create a connection to the service.
To do so, the most straight forward way is the following::
>>> import boto
>>> conn = boto.connect_dynamodb(
aws_access_key_id='<YOUR_AWS_KEY_ID>',
aws_secret_access_key='<YOUR_AWS_SECRET_KEY>')
>>> conn
<boto.dynamodb.layer2.Layer2 object at 0x3fb3090>
Bear in mind that if you have your credentials in boto config in your home
directory, the two keyword arguments in the call above are not needed. More
details on configuration can be found in :doc:`boto_config_tut`.
.. note:: At this
time, Amazon DynamoDB is available only in the US-EAST-1 region. The
``connect_dynamodb`` method automatically connect to that region.
The :py:func:`boto.connect_dynamodb` functions returns a
:py:class:`boto.dynamodb.layer2.Layer2` instance, which is a high-level API
for working with DynamoDB. Layer2 is a set of abstractions that sit atop
the lower level :py:class:`boto.dynamodb.layer1.Layer1` API, which closely
mirrors the Amazon DynamoDB API. For the purpose of this tutorial, we'll
just be covering Layer2.
Listing Tables
--------------
Now that we have a DynamoDB connection object, we can then query for a list of
existing tables in that region::
>>> conn.list_tables()
['test-table', 'another-table']
Creating Tables
---------------
DynamoDB tables are created with the
:py:meth:`Layer2.create_table <boto.dynamodb.layer2.Layer2.create_table>`
method. While DynamoDB's items (a rough equivalent to a relational DB's row)
don't have a fixed schema, you do need to create a schema for the table's
hash key element, and the optional range key element. This is explained in
greater detail in DynamoDB's `Data Model`_ documentation.
We'll start by defining a schema that has a hash key and a range key that
are both keys::
>>> message_table_schema = conn.create_schema(
hash_key_name='forum_name',
hash_key_proto_value='S',
range_key_name='subject',
range_key_proto_value='S'
)
The next few things to determine are table name and read/write throughput. We'll
defer explaining throughput to the DynamoDB's `Provisioned Throughput`_ docs.
We're now ready to create the table::
>>> table = conn.create_table(
name='messages',
schema=message_table_schema,
read_units=10,
write_units=10
)
>>> table
Table(messages)
This returns a :py:class:`boto.dynamodb.table.Table` instance, which provides
simple ways to create (put), update, and delete items.
.. _Data Model: http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/DataModel.html
.. _Provisioned Throughput: http://docs.amazonwebservices.com/amazondynamodb/latest/developerguide/ProvisionedThroughputIntro.html
Getting a Table
---------------
To retrieve an existing table, use
:py:meth:`Layer2.get_table <boto.dynamodb.layer2.Layer2.get_table>`::
>>> conn.list_tables()
['test-table', 'another-table', 'messages']
>>> table = conn.get_table('messages')
>>> table
Table(messages)
:py:meth:`Layer2.get_table <boto.dynamodb.layer2.Layer2.get_table>`, like
:py:meth:`Layer2.create_table <boto.dynamodb.layer2.Layer2.create_table>`,
returns a :py:class:`boto.dynamodb.table.Table` instance.
Describing Tables
-----------------
To get a complete description of a table, use
:py:meth:`Layer2.describe_table <boto.dynamodb.layer2.Layer2.describe_table>`::
>>> conn.list_tables()
['test-table', 'another-table', 'messages']
>>> conn.describe_table('messages')
{
'Table': {
'CreationDateTime': 1327117581.624,
'ItemCount': 0,
'KeySchema': {
'HashKeyElement': {
'AttributeName': 'forum_name',
'AttributeType': 'S'
},
'RangeKeyElement': {
'AttributeName': 'subject',
'AttributeType': 'S'
}
},
'ProvisionedThroughput': {
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
},
'TableName': 'messages',
'TableSizeBytes': 0,
'TableStatus': 'ACTIVE'
}
}
Adding Items
------------
Continuing on with our previously created ``messages`` table, adding an::
>>> table = conn.get_table('messages')
>>> item_data = {
'Body': 'http://url_to_lolcat.gif',
'SentBy': 'User A',
'ReceivedTime': '12/9/2011 11:36:03 PM',
}
>>> item = table.new_item(
# Our hash key is 'forum'
hash_key='LOLCat Forum',
# Our range key is 'subject'
range_key='Check this out!',
# This has the
attrs=item_data
)
The
:py:meth:`Table.new_item <boto.dynamodb.table.Table.new_item>` method creates
a new :py:class:`boto.dynamodb.item.Item` instance with your specified
hash key, range key, and attributes already set.
:py:class:`Item <boto.dynamodb.item.Item>` is a :py:class:`dict` sub-class,
meaning you can edit your data as such::
item['a_new_key'] = 'testing'
del item['a_new_key']
After you are happy with the contents of the item, use
:py:meth:`Item.put <boto.dynamodb.item.Item.put>` to commit it to DynamoDB::
>>> item.put()
Retrieving Items
----------------
Now, let's check if it got added correctly. Since DynamoDB works under an
'eventual consistency' mode, we need to specify that we wish a consistent read,
as follows::
>>> table = conn.get_table('messages')
>>> item = table.get_item(
# Your hash key was 'forum_name'
hash_key='LOLCat Forum',
# Your range key was 'subject'
range_key='Check this out!'
)
>>> item
{
# Note that this was your hash key attribute (forum_name)
'forum_name': 'LOLCat Forum',
# This is your range key attribute (subject)
'subject': 'Check this out!'
'Body': 'http://url_to_lolcat.gif',
'ReceivedTime': '12/9/2011 11:36:03 PM',
'SentBy': 'User A',
}
Updating Items
--------------
To update an item's attributes, simply retrieve it, modify the value, then
:py:meth:`Item.put <boto.dynamodb.item.Item.put>` it again::
>>> table = conn.get_table('messages')
>>> item = table.get_item(
hash_key='LOLCat Forum',
range_key='Check this out!'
)
>>> item['SentBy'] = 'User B'
>>> item.put()
Deleting Items
--------------
To delete items, use the
:py:meth:`Item.delete <boto.dynamodb.item.Item.delete>` method::
>>> table = conn.get_table('messages')
>>> item = table.get_item(
hash_key='LOLCat Forum',
range_key='Check this out!'
)
>>> item.delete()
Deleting Tables
---------------
.. WARNING::
Deleting a table will also **permanently** delete all of its contents without prompt. Use carefully.
There are two easy ways to delete a table. Through your top-level
:py:class:`Layer2 <boto.dynamodb.layer2.Layer2>` object::
>>> conn.delete_table(table)
Or by getting the table, then using
:py:meth:`Table.delete <boto.dynamodb.table.Table.delete>`::
>>> table = conn.get_table('messages')
>>> table.delete()
|