File: dynamodb_v1_to_v2.rst

package info (click to toggle)
python-boto 2.34.0-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 8,584 kB
  • ctags: 10,521
  • sloc: python: 78,553; makefile: 123
file content (366 lines) | stat: -rw-r--r-- 9,532 bytes parent folder | download | duplicates (11)
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
.. dynamodb_v1_to_v2:

=========================================
Migrating from DynamoDB v1 to DynamoDB v2
=========================================

For the v2 release of AWS' DynamoDB_, the high-level API for interacting via
``boto`` was rewritten. Since there were several new features added in v2,
people using the v1 API may wish to transition their code to the new API.
This guide covers the high-level APIs.

.. _DynamoDB: http://aws.amazon.com/dynamodb/


Creating New Tables
===================

DynamoDB v1::

    >>> import boto.dynamodb
    >>> conn = boto.dynamodb.connect_to_region()
    >>> message_table_schema = conn.create_schema(
    ...     hash_key_name='forum_name',
    ...     hash_key_proto_value=str,
    ...     range_key_name='subject',
    ...     range_key_proto_value=str
    ... )
    >>> table = conn.create_table(
    ...     name='messages',
    ...     schema=message_table_schema,
    ...     read_units=10,
    ...     write_units=10
    ... )

DynamoDB v2::

    >>> from boto.dynamodb2.fields import HashKey
    >>> from boto.dynamodb2.fields import RangeKey
    >>> from boto.dynamodb2.table import Table

    >>> table = Table.create('messages', schema=[
    ...     HashKey('forum_name'),
    ...     RangeKey('subject'),
    ... ], throughput={
    ...     'read': 10,
    ...     'write': 10,
    ... })


Using an Existing Table
=======================

DynamoDB v1::

    >>> import boto.dynamodb
    >>> conn = boto.dynamodb.connect_to_region()
    # With API calls.
    >>> table = conn.get_table('messages')

    # Without API calls.
    >>> message_table_schema = conn.create_schema(
    ...     hash_key_name='forum_name',
    ...     hash_key_proto_value=str,
    ...     range_key_name='subject',
    ...     range_key_proto_value=str
    ... )
    >>> table = conn.table_from_schema(
    ...     name='messages',
    ...     schema=message_table_schema)


DynamoDB v2::

    >>> from boto.dynamodb2.table import Table
    # With API calls.
    >>> table = Table('messages')

    # Without API calls.
    >>> from boto.dynamodb2.fields import HashKey
    >>> from boto.dynamodb2.table import Table
    >>> table = Table('messages', schema=[
    ...     HashKey('forum_name'),
    ...     HashKey('subject'),
    ... ])


Updating Throughput
===================

DynamoDB v1::

    >>> import boto.dynamodb
    >>> conn = boto.dynamodb.connect_to_region()
    >>> table = conn.get_table('messages')
    >>> conn.update_throughput(table, read_units=5, write_units=15)

DynamoDB v2::

    >>> from boto.dynamodb2.table import Table
    >>> table = Table('messages')
    >>> table.update(throughput={
    ...     'read': 5,
    ...     'write': 15,
    ... })


Deleting a Table
================

DynamoDB v1::

    >>> import boto.dynamodb
    >>> conn = boto.dynamodb.connect_to_region()
    >>> table = conn.get_table('messages')
    >>> conn.delete_table(table)

DynamoDB v2::

    >>> from boto.dynamodb2.table import Table
    >>> table = Table('messages')
    >>> table.delete()


Creating an Item
================

DynamoDB v1::

    >>> import boto.dynamodb
    >>> conn = boto.dynamodb.connect_to_region()
    >>> 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
    ... )

DynamoDB v2::

    >>> from boto.dynamodb2.table import Table
    >>> table = Table('messages')
    >>> item = table.put_item(data={
    ...     'forum_name': 'LOLCat Forum',
    ...     'subject': 'Check this out!',
    ...     'Body': 'http://url_to_lolcat.gif',
    ...     'SentBy': 'User A',
    ...     'ReceivedTime': '12/9/2011 11:36:03 PM',
    ... })


Getting an Existing Item
========================

DynamoDB v1::

    >>> table = conn.get_table('messages')
    >>> item = table.get_item(
    ...     hash_key='LOLCat Forum',
    ...     range_key='Check this out!'
    ... )

DynamoDB v2::

    >>> table = Table('messages')
    >>> item = table.get_item(
    ...     forum_name='LOLCat Forum',
    ...     subject='Check this out!'
    ... )


Updating an Item
================

DynamoDB v1::

    >>> item['a_new_key'] = 'testing'
    >>> del item['a_new_key']
    >>> item.put()

DynamoDB v2::

    >>> item['a_new_key'] = 'testing'
    >>> del item['a_new_key']

    # Conditional save, only if data hasn't changed.
    >>> item.save()

    # Forced full overwrite.
    >>> item.save(overwrite=True)

    # Partial update (only changed fields).
    >>> item.partial_save()


Deleting an Item
================

DynamoDB v1::

    >>> item.delete()

DynamoDB v2::

    >>> item.delete()


Querying
========

DynamoDB v1::

    >>> import boto.dynamodb
    >>> conn = boto.dynamodb.connect_to_region()
    >>> table = conn.get_table('messages')
    >>> from boto.dynamodb.condition import BEGINS_WITH
    >>> items = table.query('Amazon DynamoDB',
    ...                     range_key_condition=BEGINS_WITH('DynamoDB'),
    ...                     request_limit=1, max_results=1)
    >>> for item in items:
    >>>     print item['Body']

DynamoDB v2::

    >>> from boto.dynamodb2.table import Table
    >>> table = Table('messages')
    >>> items = table.query_2(
    ...     forum_name__eq='Amazon DynamoDB',
    ...     subject__beginswith='DynamoDB',
    ...     limit=1
    ... )
    >>> for item in items:
    >>>     print item['Body']


Scans
=====

DynamoDB v1::

    >>> import boto.dynamodb
    >>> conn = boto.dynamodb.connect_to_region()
    >>> table = conn.get_table('messages')

    # All items.
    >>> items = table.scan()

    # With a filter.
    >>> items = table.scan(scan_filter={'Replies': GT(0)})

DynamoDB v2::

    >>> from boto.dynamodb2.table import Table
    >>> table = Table('messages')

    # All items.
    >>> items = table.scan()

    # With a filter.
    >>> items = table.scan(replies__gt=0)


Batch Gets
==========

DynamoDB v1::

    >>> import boto.dynamodb
    >>> conn = boto.dynamodb.connect_to_region()
    >>> table = conn.get_table('messages')
    >>> from boto.dynamodb.batch import BatchList
    >>> the_batch = BatchList(conn)
    >>> the_batch.add_batch(table, keys=[
    ...     ('LOLCat Forum', 'Check this out!'),
    ...     ('LOLCat Forum', 'I can haz docs?'),
    ...     ('LOLCat Forum', 'Maru'),
    ... ])
    >>> results = conn.batch_get_item(the_batch)

    # (Largely) Raw dictionaries back from DynamoDB.
    >>> for item_dict in response['Responses'][table.name]['Items']:
    ...     print item_dict['Body']

DynamoDB v2::

    >>> from boto.dynamodb2.table import Table
    >>> table = Table('messages')
    >>> results = table.batch_get(keys=[
    ...     {'forum_name': 'LOLCat Forum', 'subject': 'Check this out!'},
    ...     {'forum_name': 'LOLCat Forum', 'subject': 'I can haz docs?'},
    ...     {'forum_name': 'LOLCat Forum', 'subject': 'Maru'},
    ... ])

    # Lazy requests across pages, if paginated.
    >>> for res in results:
    ...     # You get back actual ``Item`` instances.
    ...     print item['Body']


Batch Writes
============

DynamoDB v1::

    >>> import boto.dynamodb
    >>> conn = boto.dynamodb.connect_to_region()
    >>> table = conn.get_table('messages')
    >>> from boto.dynamodb.batch import BatchWriteList
    >>> from boto.dynamodb.item import Item

    # You must manually manage this so that your total ``puts/deletes`` don't
    # exceed 25.
    >>> the_batch = BatchList(conn)
    >>> the_batch.add_batch(table, puts=[
    ...     Item(table, 'Corgi Fanciers', 'Sploots!', {
    ...         'Body': 'Post your favorite corgi-on-the-floor shots!',
    ...         'SentBy': 'User B',
    ...         'ReceivedTime': '2013/05/02 10:56:45 AM',
    ...     }),
    ...     Item(table, 'Corgi Fanciers', 'Maximum FRAPS', {
    ...         'Body': 'http://internetvideosite/watch?v=1247869',
    ...         'SentBy': 'User C',
    ...         'ReceivedTime': '2013/05/01 09:15:25 PM',
    ...     }),
    ... ], deletes=[
    ...     ('LOLCat Forum', 'Off-topic post'),
    ...     ('LOLCat Forum', 'They be stealin mah bukket!'),
    ... ])
    >>> conn.batch_write_item(the_writes)

DynamoDB v2::

    >>> from boto.dynamodb2.table import Table
    >>> table = Table('messages')

    # Uses a context manager, which also automatically handles batch sizes.
    >>> with table.batch_write() as batch:
    ...     batch.delete_item(
    ...         forum_name='LOLCat Forum',
    ...         subject='Off-topic post'
    ...     )
    ...     batch.put_item(data={
    ...         'forum_name': 'Corgi Fanciers',
    ...         'subject': 'Sploots!',
    ...         'Body': 'Post your favorite corgi-on-the-floor shots!',
    ...         'SentBy': 'User B',
    ...         'ReceivedTime': '2013/05/02 10:56:45 AM',
    ...     })
    ...     batch.put_item(data={
    ...         'forum_name': 'Corgi Fanciers',
    ...         'subject': 'Sploots!',
    ...         'Body': 'Post your favorite corgi-on-the-floor shots!',
    ...         'SentBy': 'User B',
    ...         'ReceivedTime': '2013/05/02 10:56:45 AM',
    ...     })
    ...     batch.delete_item(
    ...         forum_name='LOLCat Forum',
    ...         subject='They be stealin mah bukket!'
    ...     )