Skip to content

Instantly share code, notes, and snippets.

@nikhilkumarsingh
Created February 15, 2020 16:57
Show Gist options
  • Save nikhilkumarsingh/1a04cbcab1a74bdb04b4862abf5b428c to your computer and use it in GitHub Desktop.
Save nikhilkumarsingh/1a04cbcab1a74bdb04b4862abf5b428c to your computer and use it in GitHub Desktop.
Working with DynamoDB using Python (Hash Key, Range Key and Indexes Explained)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Working with DynamoDB using Python\n",
"\n",
"\n",
"- [DynamoDB Core Components](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.CoreComponents.html)\n",
"- [Indexes in DynamoDB](https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/SecondaryIndexes.html)\n",
"\n",
"- [Boto3 Configuration](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html)\n",
"\n",
"- [DynamoDB boto3](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Attributes:\n",
"- student_id\n",
"- course\n",
"- year\n",
"- grade\n",
"\n",
"### Key Schema:\n",
"- Hash key: student_id\n",
"- Range key: course\n",
"\n",
"\n",
"### Global Secondary Indexes:\n",
"- course-year-index\n",
" - Hash key: course\n",
" - Range key: year\n",
" \n",
"### Local Secondary Indexes:\n",
"- year-index\n",
" - Range key: year\n",
" \n",
"### Operations:\n",
"- Put item\n",
"- Get item\n",
"- Get all records for a student_id\n",
"- Get all records for a student_id from a given year\n",
"- Get all records for a given course and year"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import boto3"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"ddb = boto3.resource('dynamodb', \n",
" aws_access_key_id='xxxxxxxxxxxxxxx', \n",
" aws_secret_access_key='xxxxxxxxxxxxxxxxxxxxxxxxxx')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"table = ddb.Table('AcademicRecords')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"items = [\n",
" {\n",
" 'student_id': 'nikhil',\n",
" 'course': 'Science',\n",
" 'year': '2015',\n",
" 'grade': 9\n",
" },\n",
" {\n",
" 'student_id': 'nikhil',\n",
" 'course': 'English',\n",
" 'year': '2016',\n",
" 'grade': 10\n",
" },\n",
" {\n",
" 'student_id': 'ravi',\n",
" 'course': 'English',\n",
" 'year': '2016',\n",
" 'grade': 9\n",
" }\n",
"]\n",
"\n",
"for item in items:\n",
" table.put_item(Item=item)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Items': [{'year': '2016',\n",
" 'student_id': 'ravi',\n",
" 'grade': Decimal('9'),\n",
" 'course': 'English'},\n",
" {'year': '2016',\n",
" 'student_id': 'nikhil',\n",
" 'grade': Decimal('10'),\n",
" 'course': 'English'},\n",
" {'year': '2015',\n",
" 'student_id': 'nikhil',\n",
" 'grade': Decimal('9'),\n",
" 'course': 'Science'}],\n",
" 'Count': 3,\n",
" 'ScannedCount': 3,\n",
" 'ResponseMetadata': {'RequestId': 'EER3IDEK54PQ9MRNMQ1EGVUMEJVV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'HTTPStatusCode': 200,\n",
" 'HTTPHeaders': {'server': 'Server',\n",
" 'date': 'Sat, 15 Feb 2020 16:55:12 GMT',\n",
" 'content-type': 'application/x-amz-json-1.0',\n",
" 'content-length': '316',\n",
" 'connection': 'keep-alive',\n",
" 'x-amzn-requestid': 'EER3IDEK54PQ9MRNMQ1EGVUMEJVV4KQNSO5AEMVJF66Q9ASUAAJG',\n",
" 'x-amz-crc32': '683477306'},\n",
" 'RetryAttempts': 0}}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.scan()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'year': '2015',\n",
" 'student_id': 'nikhil',\n",
" 'grade': Decimal('9'),\n",
" 'course': 'Science'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.get_item(Key={\n",
" 'student_id': 'nikhil',\n",
" 'course': 'Science'\n",
"})['Item']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'year': '2016',\n",
" 'student_id': 'nikhil',\n",
" 'grade': Decimal('10'),\n",
" 'course': 'English'},\n",
" {'year': '2015',\n",
" 'student_id': 'nikhil',\n",
" 'grade': Decimal('9'),\n",
" 'course': 'Science'}]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.query(KeyConditionExpression=\"student_id = :student_id\", \n",
" ExpressionAttributeValues={\n",
" ':student_id': 'nikhil'\n",
" })['Items']"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'year': '2016',\n",
" 'student_id': 'nikhil',\n",
" 'grade': Decimal('10'),\n",
" 'course': 'English'}]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.query(IndexName='year-index', \n",
" KeyConditionExpression=\"student_id = :student_id and #year = :year\",\n",
" ExpressionAttributeNames={\n",
" '#year': 'year'\n",
" },\n",
" ExpressionAttributeValues={\n",
" ':student_id': 'nikhil',\n",
" ':year': '2016'\n",
" })['Items']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'year': '2016',\n",
" 'grade': Decimal('9'),\n",
" 'student_id': 'ravi',\n",
" 'course': 'English'},\n",
" {'year': '2016',\n",
" 'grade': Decimal('10'),\n",
" 'student_id': 'nikhil',\n",
" 'course': 'English'}]"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"table.query(IndexName='course-year-index', \n",
" KeyConditionExpression=\"course = :course and #year = :year\",\n",
" ExpressionAttributeNames={\n",
" '#year': 'year'\n",
" },\n",
" ExpressionAttributeValues={\n",
" ':course': 'English',\n",
" ':year': '2016'\n",
" })['Items']"
]
},
{
"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