File: dynamodb.rst

package info (click to toggle)
python-boto3 1.26.27%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 7,880 kB
  • sloc: python: 12,629; makefile: 128
file content (471 lines) | stat: -rw-r--r-- 13,402 bytes parent folder | download
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
.. _dynamodb_guide:

Amazon DynamoDB
================
By following this guide, you will learn how to use the
:py:class:`DynamoDB.ServiceResource` and :py:class:`DynamoDB.Table`
resources in order to create tables, write items to tables, modify existing
items, retrieve items, and query/filter the items in the table.


Creating a new table
--------------------

In order to create a new table, use the
:py:meth:`DynamoDB.ServiceResource.create_table` method::

    import boto3
    
    # Get the service resource.
    dynamodb = boto3.resource('dynamodb')

    # Create the DynamoDB table.
    table = dynamodb.create_table(
        TableName='users',
        KeySchema=[
            {
                'AttributeName': 'username',
                'KeyType': 'HASH'
            },
            {
                'AttributeName': 'last_name',
                'KeyType': 'RANGE'
            }
        ],
        AttributeDefinitions=[
            {
                'AttributeName': 'username',
                'AttributeType': 'S'
            },
            {
                'AttributeName': 'last_name',
                'AttributeType': 'S'
            },
        ],
        ProvisionedThroughput={
            'ReadCapacityUnits': 5,
            'WriteCapacityUnits': 5
        }
    )

    # Wait until the table exists.
    table.wait_until_exists()

    # Print out some data about the table.
    print(table.item_count)

Expected output::

    0

This creates a table named ``users`` that respectively has the hash and
range primary keys ``username`` and ``last_name``.
This method will return a :py:class:`DynamoDB.Table` resource to call
additional methods on the created table.


Using an existing table
-----------------------
It is also possible to create a :py:class:`DynamoDB.Table` resource from
an existing table::

    import boto3

    # Get the service resource.
    dynamodb = boto3.resource('dynamodb')

    # Instantiate a table resource object without actually
    # creating a DynamoDB table. Note that the attributes of this table
    # are lazy-loaded: a request is not made nor are the attribute
    # values populated until the attributes
    # on the table resource are accessed or its load() method is called.
    table = dynamodb.Table('users')

    # Print out some data about the table.
    # This will cause a request to be made to DynamoDB and its attribute
    # values will be set based on the response.
    print(table.creation_date_time)

Expected output (Please note that the actual times will probably not match up)::

    2015-06-26 12:42:45.149000-07:00


Creating a new item
-------------------

Once you have a :py:class:`DynamoDB.Table` resource you can add new items
to the table using :py:meth:`DynamoDB.Table.put_item`::

    table.put_item(
       Item={
            'username': 'janedoe',
            'first_name': 'Jane',
            'last_name': 'Doe',
            'age': 25,
            'account_type': 'standard_user',
        }
    )

For all of the valid types that can be used for an item, refer to
:ref:`ref_valid_dynamodb_types`.


Getting an item
---------------
You can then retrieve the object using :py:meth:`DynamoDB.Table.get_item`::

    response = table.get_item(
        Key={
            'username': 'janedoe',
            'last_name': 'Doe'
        }
    )
    item = response['Item']
    print(item)


Expected output::

    {u'username': u'janedoe',
     u'first_name': u'Jane',
     u'last_name': u'Doe',
     u'account_type': u'standard_user',
     u'age': Decimal('25')}


Updating an item
----------------

You can then update attributes of the item in the table::

    table.update_item(
        Key={
            'username': 'janedoe',
            'last_name': 'Doe'
        },
        UpdateExpression='SET age = :val1',
        ExpressionAttributeValues={
            ':val1': 26
        }
    )

Then if you retrieve the item again, it will be updated appropriately::

    response = table.get_item(
        Key={
            'username': 'janedoe',
            'last_name': 'Doe'
        }
    )
    item = response['Item']
    print(item)


Expected output::

    {u'username': u'janedoe',
     u'first_name': u'Jane',
     u'last_name': u'Doe',
     u'account_type': u'standard_user',
     u'age': Decimal('26')}


Deleting an item
----------------
You can also delete the item using :py:meth:`DynamoDB.Table.delete_item`::
    
    table.delete_item(
        Key={
            'username': 'janedoe',
            'last_name': 'Doe'
        }
    )


Batch writing
-------------
If you are loading a lot of data at a time, you can make use of
:py:meth:`DynamoDB.Table.batch_writer` so you can both speed up the process and
reduce the number of write requests made to the service.

This method returns a handle to a batch writer object that will automatically
handle buffering and sending items in batches.  In addition, the
batch writer will also automatically handle any unprocessed items and
resend them as needed.  All you need to do is call ``put_item`` for any
items you want to add, and ``delete_item`` for any items you want to delete::

    with table.batch_writer() as batch:
        batch.put_item(
            Item={
                'account_type': 'standard_user',
                'username': 'johndoe',
                'first_name': 'John',
                'last_name': 'Doe',
                'age': 25,
                'address': {
                    'road': '1 Jefferson Street',
                    'city': 'Los Angeles',
                    'state': 'CA',
                    'zipcode': 90001
                }
            }
        )
        batch.put_item(
            Item={
                'account_type': 'super_user',
                'username': 'janedoering',
                'first_name': 'Jane',
                'last_name': 'Doering',
                'age': 40,
                'address': {
                    'road': '2 Washington Avenue',
                    'city': 'Seattle',
                    'state': 'WA',
                    'zipcode': 98109
                }
            }
        )
        batch.put_item(
            Item={
                'account_type': 'standard_user',
                'username': 'bobsmith',
                'first_name': 'Bob',
                'last_name':  'Smith',
                'age': 18,
                'address': {
                    'road': '3 Madison Lane',
                    'city': 'Louisville',
                    'state': 'KY',
                    'zipcode': 40213
                }
            }
        )
        batch.put_item(
            Item={
                'account_type': 'super_user',
                'username': 'alicedoe',
                'first_name': 'Alice',
                'last_name': 'Doe',
                'age': 27,
                'address': {
                    'road': '1 Jefferson Street',
                    'city': 'Los Angeles',
                    'state': 'CA',
                    'zipcode': 90001
                }
            }
        )

The batch writer is even able to handle a very large amount of writes to the
table.

::

    with table.batch_writer() as batch:
        for i in range(50):
            batch.put_item(
                Item={
                    'account_type': 'anonymous',
                    'username': 'user' + str(i),
                    'first_name': 'unknown',
                    'last_name': 'unknown'
                }
            )

The batch writer can help to de-duplicate request by specifying ``overwrite_by_pkeys=['partition_key', 'sort_key']``
if you want to bypass no duplication limitation of single batch write request as
``botocore.exceptions.ClientError: An error occurred (ValidationException) when calling the BatchWriteItem operation: Provided list of item keys contains duplicates``.

It will drop request items in the buffer if their primary keys(composite) values are
the same as newly added one, as eventually consistent with streams of individual
put/delete operations on the same item.

::

    with table.batch_writer(overwrite_by_pkeys=['partition_key', 'sort_key']) as batch:
        batch.put_item(
            Item={
                'partition_key': 'p1',
                'sort_key': 's1',
                'other': '111',
            }
        )
        batch.put_item(
            Item={
                'partition_key': 'p1',
                'sort_key': 's1',
                'other': '222',
            }
        )
        batch.delete_item(
            Key={
                'partition_key': 'p1',
                'sort_key': 's2'
            }
        )
        batch.put_item(
            Item={
                'partition_key': 'p1',
                'sort_key': 's2',
                'other': '444',
            }
        )

after de-duplicate:

::

    batch.put_item(
        Item={
            'partition_key': 'p1',
            'sort_key': 's1',
            'other': '222',
        }
    )
    batch.put_item(
        Item={
            'partition_key': 'p1',
            'sort_key': 's2',
            'other': '444',
        }
    )


Querying and scanning
---------------------

With the table full of items, you can then query or scan the items in the table
using the :py:meth:`DynamoDB.Table.query` or :py:meth:`DynamoDB.Table.scan`
methods respectively. To add conditions to scanning and querying the table,
you will need to import the :py:class:`boto3.dynamodb.conditions.Key` and
:py:class:`boto3.dynamodb.conditions.Attr` classes. The
:py:class:`boto3.dynamodb.conditions.Key` should be used when the
condition is related to the key of the item.
The :py:class:`boto3.dynamodb.conditions.Attr` should be used when the
condition is related to an attribute of the item::

    from boto3.dynamodb.conditions import Key, Attr
    

This queries for all of the users whose ``username`` key equals ``johndoe``::

    response = table.query(
        KeyConditionExpression=Key('username').eq('johndoe')
    )
    items = response['Items']
    print(items)


Expected output::

    [{u'username': u'johndoe',
      u'first_name': u'John',
      u'last_name': u'Doe',
      u'account_type': u'standard_user',
      u'age': Decimal('25'),
      u'address': {u'city': u'Los Angeles',
                   u'state': u'CA',
                   u'zipcode': Decimal('90001'),
                   u'road': u'1 Jefferson Street'}}]


Similarly you can scan the table based on attributes of the items. For
example, this scans for all the users whose ``age`` is less than ``27``::

    response = table.scan(
        FilterExpression=Attr('age').lt(27)
    )
    items = response['Items']
    print(items)


Expected output::

    [{u'username': u'johndoe',
      u'first_name': u'John',
      u'last_name': u'Doe',
      u'account_type': u'standard_user',
      u'age': Decimal('25'),
      u'address': {u'city': u'Los Angeles',
                   u'state': u'CA',
                   u'zipcode': Decimal('90001'),
                   u'road': u'1 Jefferson Street'}},
     {u'username': u'bobsmith',
      u'first_name': u'Bob',
      u'last_name': u'Smith',
      u'account_type': u'standard_user',
      u'age': Decimal('18'),
      u'address': {u'city': u'Louisville',
                   u'state': u'KY',
                   u'zipcode': Decimal('40213'),
                   u'road': u'3 Madison Lane'}}]


You are also able to chain conditions together using the logical operators:
``&`` (and), ``|`` (or), and ``~`` (not). For example, this scans for all
users whose ``first_name`` starts with ``J`` and whose ``account_type`` is
``super_user``::
    
    response = table.scan(
        FilterExpression=Attr('first_name').begins_with('J') & Attr('account_type').eq('super_user')
    )
    items = response['Items']
    print(items)


Expected output::

    [{u'username': u'janedoering',
      u'first_name': u'Jane',
      u'last_name': u'Doering',
      u'account_type': u'super_user',
      u'age': Decimal('40'),
      u'address': {u'city': u'Seattle',
                   u'state': u'WA',
                   u'zipcode': Decimal('98109'),
                   u'road': u'2 Washington Avenue'}}]


You can even scan based on conditions of a nested attribute. For example this
scans for all users whose ``state`` in their ``address`` is ``CA``::

    response = table.scan(
        FilterExpression=Attr('address.state').eq('CA')
    )
    items = response['Items']
    print(items)


Expected output::

    [{u'username': u'johndoe',
      u'first_name': u'John',
      u'last_name': u'Doe',
      u'account_type': u'standard_user',
      u'age': Decimal('25'),
      u'address': {u'city': u'Los Angeles',
                   u'state': u'CA',
                   u'zipcode': Decimal('90001'),
                   u'road': u'1 Jefferson Street'}},
     {u'username': u'alicedoe',
      u'first_name': u'Alice',
      u'last_name': u'Doe',
      u'account_type': u'super_user',
      u'age': Decimal('27'),
      u'address': {u'city': u'Los Angeles',
                   u'state': u'CA',
                   u'zipcode': Decimal('90001'),
                   u'road': u'1 Jefferson Street'}}]


For more information on the various conditions you can use for queries and
scans, refer to :ref:`ref_dynamodb_conditions`.


Deleting a table
----------------
Finally, if you want to delete your table call
:py:meth:`DynamoDB.Table.delete`::

    table.delete()