Giter Site home page Giter Site logo

mrtj / dynamodb-mapping Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 76 KB

A Python dictionary-like interface for an Amazon DynamoDB table.

Home Page: https://dynamodb-mapping.readthedocs.io/

License: MIT License

Makefile 7.11% Python 92.89%
amazon-dynamodb api dynamodb-table python

dynamodb-mapping's Introduction

DynamoDB Mapping

https://codecov.io/gh/mrtj/dynamodb-mapping/branch/main/graph/badge.svg?token=Y44R08UKEG Documentation Status

A Python dictionary-like interface for an Amazon DynamoDB table.

DynamoDBMapping is an alternative API for Amazon DynamoDB that implements the Python collections.abc.MutableMapping abstract base class, effectively allowing you to use a DynamoDB table as if it were a Python dictionary.

Getting started

To do anything useful with this module you need an Amazon Web Services account and an Amazon DynamoDB table. In every AWS account several DynamoDB tables can be created for free. Open an AWS account and create a DynamoDB table. You also need to create a IAM user and configure the access keys on your workstation. The easiest way to do so is to install and configure the AWS Command Line Interface. Once the AWS CLI works correctly, the AWS Python libraries (boto3) will correctly pick up the credentials.

Installation

Stable release

To install DynamoDB Mapping, run this command in your terminal:

$ pip install dynamodb_mapping

This is the preferred method to install DynamoDB Mapping, as it will always install the most recent stable release.

If you don't have pip installed, this Python installation guide can guide you through the process.

From sources

The sources for DynamoDB Mapping can be downloaded from the Github repo.

You can either clone the public repository:

$ git clone git://github.com/mrtj/dynamodb_mapping

Or download the tarball:

$ curl -OJL https://github.com/mrtj/dynamodb_mapping/tarball/master

Once you have a copy of the source, you can install it with:

$ python setup.py install

Usage

Once the credentials are correctly configured, you can start reading and writing to your DynamoDB table with DynamoDBMapping as it was an ordinal Python dictionary:

from dynamodb_mapping import DynamoDBMapping

mapping = DynamoDBMapping(table_name="my_table")

# Create or modify an item:
mapping["my_item"] = {"description": "foo", "price": 123}
mapping["my_item"]["price"] = 456

# Iterate over all items:
for key, value in mapping.items():
    print(key, value)

# Get a single item:
print(mapping["my_item"])

# Number of items in table:
# (read bellow on how to get the estimated vs precise number of items)
print(len(mapping))

# Delete an item:
del mapping["my_item"]

All methods that iterate over the elements of the table do so in a lazy manner, in that the successive pages of the scan operation are queried only on demand. Examples of such operations include scan, iteration over keys, iteration over values, and iteration over items (key-value tuples). You should pay particular attention to certain patterns that fetch all items in the table, for example, calling list(mapping.values()). This call will execute an exhaustive scan on your table, which can be costly, and attempt to load all items into memory, which can be resource-demanding if your table is particularly large.

The __len__ implementation of this class returns a best-effort estimate of the number of items in the table using the TableDescription DynamoDB API. The number of items are updated at DynamoDB service side approximately once in every 6 hours. If you need the exact number of items currently in the table, you can use len(list(mapping.keys())). Note however that this will cause to run an exhaustive scan operation on your table.

Advanced configuration

You have the following options to configure the underlying boto3 session:

  • Automatic configuration: pass nothing to DynamoDBMapping initializer. This will prompt DynamoDBMapping to load the default boto3.Session object, which in turn will use the standard boto3 credentials chain to find AWS credentials (e.g., the ~/.aws/credentials file, environment variables, etc.).
  • Pass a preconfigured boto3.Session object
  • Pass aws_access_key_id and aws_secret_access_key as keyword arguments. Additionally, the optional aws_region and aws_profile arguments are also considered.

dynamodb-mapping's People

Contributors

mrtj avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

dynamodb-mapping's Issues

Support DynamoDB indices

Description

This feature request is about supporting DynamoDB global and local secondary indices, maybe with an accessor property that in turn can be used much like the Mapping instance, using a dictionary-like API.

Test composite primary keys

Description

Currently composite primary key support (hash key + sort key) is not tested and the implementation likely contains bugs. Test and fix this feature.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.