dynamo_pandas.transactions
- dynamo_pandas.transactions.get_all_items(*, table, attributes=None, boto3_kwargs={})
Get all the items in a table.
This function performs a scan of the table.
- Parameters:
table (str) – Name of the DynamoDB table.
attributes (list[str]) – Names of the item attributes to return. If None (default), all attributes are returned.
boto3_kwargs (dict) – Keyword arguments to pass to the underlying
boto3.resource('dynamodb')function call (see boto3 docs for details).
- Returns:
List of dictionaties where each dictionary represents an item’s attributes.
- Return type:
list[dict]
Examples
>>> items = get_all_items(table="players") >>> print(items) [{'bonus_points': 4, 'player_id': 'player_three', 'last_play': '2021-01-21 10:22:43', 'rating': 2.5, 'play_time': '1 days 14:01:19'}, {'bonus_points': None, 'player_id': 'player_four', 'last_play': '2021-01-22 13:51:12', 'rating': 4.8, 'play_time': '0 days 03:45:49'}, {'bonus_points': 3, 'player_id': 'player_one', 'last_play': '2021-01-18 22:47:23', 'rating': 4.3, 'play_time': '2 days 17:41:55'}, {'bonus_points': 1, 'player_id': 'player_two', 'last_play': '2021-01-19 19:07:54', 'rating': 3.8, 'play_time': '0 days 22:07:34'}]
Get only specific attributes:
>>> items = get_all_items(table="players", attributes=["player_id", "play_time"]) >>> print(items) [{'player_id': 'player_three', 'play_time': '1 days 14:01:19'}, {'player_id': 'player_four', 'play_time': '0 days 03:45:49'}, {'player_id': 'player_one', 'play_time': '2 days 17:41:55'}, {'player_id': 'player_two', 'play_time': '0 days 22:07:34'}]
- dynamo_pandas.transactions.get_item(*, key, table, attributes=None, boto3_kwargs={})
Get a single item from a table.
- Parameters:
key (dict) – Key of the item to get.
table (str) – Name of the DynamoDB table.
attributes (list[str]) – Names of the item attributes to return. If None (default), all attributes are returned.
boto3_kwargs (dict) –
Keyword arguments to pass to the underlying
boto3.resource('dynamodb')function call (see boto3 docs for details).
- Returns:
A dictionary representing the item’s attributes. None if the key does not exist in the table.
- Return type:
dict, None
Examples
>>> item = get_item(key={"player_id": "player_two"}, table="players") >>> print(item) {'bonus_points': 1, 'player_id': 'player_two', 'last_play': '2021-01-19 19:07:54', 'rating': 3.8, 'play_time': '0 days 22:07:34'}
Get only specific attributes:
>>> item = get_item( ... key={"player_id": "player_two"}, ... table="players", ... attributes=["play_time", "rating"] ... ) >>> print(item) {'rating': 3.8, 'play_time': '0 days 22:07:34'}
- dynamo_pandas.transactions.get_items(*, keys, table, attributes=None, boto3_kwargs={})
Get multiple items from a table.
- Parameters:
keys (list[dict]) – List of key dictionaries of the items to get.
table (str) – Name of the DynamoDB table.
attributes (list[str]) – Names of the item attributes to return. If None (default), all attributes are returned.
boto3_kwargs (dict) –
Keyword arguments to pass to the underlying
boto3.resource('dynamodb')function call (see boto3 docs for details).
- Returns:
List of dictionaties where each dictionary represents an item’s attributes. Only items for which the key exists in the table are returned.
- Return type:
list[dict]
Examples
>>> items = get_items( ... keys=[ ... {"player_id": "player_two"}, ... {"player_id": "player_one"}, ... {"player_id": "player_five"}, # Not in the table ... ], ... table="players" ... ) >>> print(items) [{'bonus_points': 3, 'player_id': 'player_one', 'last_play': '2021-01-18 22:47:23', 'rating': 4.3, 'play_time': '2 days 17:41:55'}, {'bonus_points': 1, 'player_id': 'player_two', 'last_play': '2021-01-19 19:07:54', 'rating': 3.8, 'play_time': '0 days 22:07:34'}]
Get only specific attributes:
>>> items = get_items( ... keys=[{"player_id": "player_two"}, {"player_id": "player_one"}], ... table="players", ... attributes=["player_id", "play_time"] ... ) >>> print(items) [{'player_id': 'player_one', 'play_time': '2 days 17:41:55'}, {'player_id': 'player_two', 'play_time': '0 days 22:07:34'}]
- dynamo_pandas.transactions.put_item(*, item, table, return_response=False, boto3_kwargs={})
Add or update an item in a table. If the item does not exist in the table it is created, otherwise the existing item is replaced with the new one.
Item can use supported numpy or pandas data types.
- Parameters:
item (dict) – A dictionary representing the item’s attributes. The item must include the table’s primary key attributes.
table (str) – Name of the DynamoDB table.
return_response (bool) – If True, the response from the boto3 API call will be returned.
boto3_kwargs (dict) –
Keyword arguments to pass to the underlying
boto3.client('dynamodb')function call (see boto3 docs for details).
- Returns:
None if
return_responseis False, the boto3 API call response if True.- Return type:
None, dict
Examples
>>> print(item) {'player_id': 'player_three', 'bonus_points': 4, 'last_play': Timestamp('2021-01-21 10:22:43+0000', tz='UTC'), 'rating': 2.5, 'play_time': Timedelta('1 days 14:01:19')} >>> response = put_item(item=item, table="players", return_response=True) >>> print(response["ResponseMetadata"]["HTTPStatusCode"]) 200
- dynamo_pandas.transactions.put_items(*, items, table, boto3_kwargs={})
Add or update multiple items in a table. If the item(s) do not exist in the table they are created, otherwise the existing items are replaced with the new ones.
Items can use supported numpy or pandas data types.
- Parameters:
items (list[dict]) – List of dictionaries where each dictionary represents an item’s attributes.
table (str) – Name of the DynamoDB table.
boto3_kwargs (dict) –
Keyword arguments to pass to the underlying
boto3.client('dynamodb')function call (see boto3 docs for details).
Examples
>>> pprint(items) [{'bonus_points': 4, 'last_play': Timestamp('2021-01-21 10:22:43+0000', tz='UTC'), 'play_time': Timedelta('1 days 14:01:19'), 'player_id': 'player_three', 'rating': 2.5}, {'bonus_points': <NA>, 'last_play': Timestamp('2021-01-22 13:51:12+0000', tz='UTC'), 'play_time': Timedelta('0 days 03:45:49'), 'player_id': 'player_four', 'rating': 4.8}] >>> put_items(items=items, table="players)