Skip to content

Instantly share code, notes, and snippets.

@nikhilkumarsingh
Last active January 26, 2020 18:50
Show Gist options
  • Select an option

  • Save nikhilkumarsingh/ff0261d6a3f6894c7ec0bb61deb990e0 to your computer and use it in GitHub Desktop.

Select an option

Save nikhilkumarsingh/ff0261d6a3f6894c7ec0bb61deb990e0 to your computer and use it in GitHub Desktop.
DynamoDB Python Tutorial
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Introduction to DynamoDB\n",
"\n",
"![](https://l3snh1odsii3hndbd2rovotm-wpengine.netdna-ssl.com/wp-content/uploads/2018/02/amazon_dynamodb_logo.png)\n",
"\n",
"- [aws dynamodb](https://aws.amazon.com/dynamodb/)\n",
"\n",
"- [boto3 doc](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html)\n",
"\n",
"## Installation\n",
"\n",
"```\n",
"$ pip install boto3\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"import boto3"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"ddb = boto3.resource('dynamodb')"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"table = ddb.Table('students')"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dynamodb.Table(name='students')"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"table.put_item(Item={\n",
" 'id': '1',\n",
" 'name': 'nikhil',\n",
" 'branch': 'COE'\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ResponseMetadata': {'RequestId': 'P8BA30UQGOLO5G8Q44EJB8V9E3VV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'HTTPStatusCode': 200,\n",
" 'HTTPHeaders': {'server': 'Server',\n",
" 'date': 'Sun, 26 Jan 2020 18:00:21 GMT',\n",
" 'content-type': 'application/x-amz-json-1.0',\n",
" 'content-length': '2',\n",
" 'connection': 'keep-alive',\n",
" 'x-amzn-requestid': 'P8BA30UQGOLO5G8Q44EJB8V9E3VV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'x-amz-crc32': '2745614147'},\n",
" 'RetryAttempts': 0}}"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.get_item(Key={\n",
" 'id': '1'\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ResponseMetadata': {'RequestId': 'LUSCPBSMOJEO6BUV2255E3D5EBVV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'HTTPStatusCode': 200,\n",
" 'HTTPHeaders': {'server': 'Server',\n",
" 'date': 'Sun, 26 Jan 2020 18:01:25 GMT',\n",
" 'content-type': 'application/x-amz-json-1.0',\n",
" 'content-length': '2',\n",
" 'connection': 'keep-alive',\n",
" 'x-amzn-requestid': 'LUSCPBSMOJEO6BUV2255E3D5EBVV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'x-amz-crc32': '2745614147'},\n",
" 'RetryAttempts': 0}}"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.delete_item(Key={\n",
" 'id': '3'\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'ResponseMetadata': {'RequestId': 'T7L6AU257BVP77G0HH144TGSK7VV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'HTTPStatusCode': 200,\n",
" 'HTTPHeaders': {'server': 'Server',\n",
" 'date': 'Sun, 26 Jan 2020 13:37:34 GMT',\n",
" 'content-type': 'application/x-amz-json-1.0',\n",
" 'content-length': '2',\n",
" 'connection': 'keep-alive',\n",
" 'x-amzn-requestid': 'T7L6AU257BVP77G0HH144TGSK7VV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'x-amz-crc32': '2745614147'},\n",
" 'RetryAttempts': 0}}"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.update_item(Key={\n",
" 'id': '1'\n",
"}, AttributeUpdates={\n",
" 'name': {\n",
" 'Value': 'nik',\n",
" 'Action': 'PUT'\n",
" }\n",
"})"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Items': [{'id': '2', 'name': 'ravi', 'branch': 'IT'},\n",
" {'id': '1', 'name': 'nik', 'branch': 'COE'}],\n",
" 'Count': 2,\n",
" 'ScannedCount': 2,\n",
" 'ResponseMetadata': {'RequestId': 'ADU507BCHSAEAHEE2VGK3FOVSVVV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'HTTPStatusCode': 200,\n",
" 'HTTPHeaders': {'server': 'Server',\n",
" 'date': 'Sun, 26 Jan 2020 13:38:42 GMT',\n",
" 'content-type': 'application/x-amz-json-1.0',\n",
" 'content-length': '152',\n",
" 'connection': 'keep-alive',\n",
" 'x-amzn-requestid': 'ADU507BCHSAEAHEE2VGK3FOVSVVV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'x-amz-crc32': '2646087154'},\n",
" 'RetryAttempts': 0}}"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.scan()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment