|
2 | 2 | Usage |
3 | 3 | ===== |
4 | 4 |
|
5 | | -To use DynamoDB Mapping in a project:: |
| 5 | +DynamoDBMapping is an alternative API for `Amazon DynamoDB`_ that implements the Python |
| 6 | +``collections.abc.MutableMapping`` abstract base class, effectively allowing you to use a DynamoDB |
| 7 | +table as if it were a Python dictionary. |
| 8 | + |
| 9 | +.. _Amazon DynamoDB: https://aws.amazon.com/dynamodb/ |
| 10 | + |
| 11 | + |
| 12 | +Getting started |
| 13 | +--------------- |
| 14 | + |
| 15 | +To do anything useful with this module you need an Amazon Web Services account and an Amazon |
| 16 | +DynamoDB table. In every AWS account several DynamoDB tables can be created for free. Open |
| 17 | +an `AWS account`_ and `create a DynamoDB table`_. You also need to create a IAM user and configure |
| 18 | +the access keys on your workstation. The easiest way to do so is to install and configure the |
| 19 | +`AWS Command Line Interface`_. Once the AWS CLI works correctly, the AWS Python libraries (boto3) |
| 20 | +will correctly pick up the credentials. |
| 21 | + |
| 22 | +.. _AWS account: https://aws.amazon.com/free/ |
| 23 | +.. _create a DynamoDB table: https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/getting-started-step-1.html |
| 24 | +.. _AWS Command Line Interface: https://docs.aws.amazon.com/cli/index.html |
| 25 | + |
| 26 | + |
| 27 | +Usage |
| 28 | +----- |
| 29 | + |
| 30 | +Once the credentials are correctly configured, you can start reading and writing to your DynamoDB |
| 31 | +table with DynamoDBMapping as it was an ordinal Python dictionary:: |
| 32 | + |
| 33 | + from dynamodb_mapping import DynamoDBMapping |
| 34 | + |
| 35 | + mapping = DynamoDBMapping(table_name="my_table") |
| 36 | + |
| 37 | + # Create or modify an item: |
| 38 | + mapping["my_item"] = {"description": "foo", "price": 123} |
| 39 | + mapping["my_item"]["price"] = 456 |
| 40 | + |
| 41 | + # Iterate over all items: |
| 42 | + for key, value in mapping.items(): |
| 43 | + print(key, value) |
| 44 | + |
| 45 | + # Get a single item: |
| 46 | + print(mapping["my_item"]) |
| 47 | + |
| 48 | + # Number of items in table: |
| 49 | + # (read bellow on how to get the estimated vs precise number of items) |
| 50 | + print(len(mapping)) |
| 51 | + |
| 52 | + # Delete an item: |
| 53 | + del mapping["my_item"] |
| 54 | + |
| 55 | + |
| 56 | +All methods that iterate over the elements of the table do so in a lazy manner, in that the |
| 57 | +successive pages of the scan operation are queried only on demand. Examples of such operations |
| 58 | +include scan, iteration over keys, iteration over values, and iteration over items (key-value |
| 59 | +tuples). You should pay particular attention to certain patterns that fetch all items in the table, |
| 60 | +for example, calling ``list(mapping.values())``. This call will execute an exhaustive scan on your |
| 61 | +table, which can be costly, and attempt to load all items into memory, which can be |
| 62 | +resource-demanding if your table is particularly large. |
| 63 | + |
| 64 | +The ``__len__`` implementation of this class returns a best-effort estimate of the number of items |
| 65 | +in the table using the TableDescription DynamoDB API. The number of items are updated at DynamoDB |
| 66 | +service side approximately once in every 6 hours. If you need the exact number of items currently in |
| 67 | +the table, you can use ``len(list(mapping.keys()))``. Note however that this will cause to run an |
| 68 | +exhaustive scan operation on your table. |
| 69 | + |
| 70 | + |
| 71 | +Advanced configuration |
| 72 | +---------------------- |
| 73 | + |
| 74 | +You have the following options to configure the underlying boto3 session: |
| 75 | + |
| 76 | +- Automatic configuration: pass nothing to DynamoDBMapping initializer. This will prompt |
| 77 | + DynamoDBMapping to load the default ``boto3.Session`` object, which in turn will use the |
| 78 | + standard boto3 credentials chain to find AWS credentials (e.g., the ``~/.aws/credentials`` |
| 79 | + file, environment variables, etc.). |
| 80 | +- Pass a preconfigured ``boto3.Session`` object |
| 81 | +- Pass ``aws_access_key_id`` and ``aws_secret_access_key`` as keyword arguments. Additionally, |
| 82 | + the optional ``aws_region`` and ``aws_profile`` arguments are also considered. |
6 | 83 |
|
7 | | - import dynamodb_mapping |
|
0 commit comments