-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e1d6d50
commit b0eec24
Showing
17 changed files
with
50,824 additions
and
31 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
.ipynb_checkpoints/aws-dynamodb-create-describe-table-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# AWS DynamoDB Create and Describe Table\n", | ||
"\n", | ||
"This is a companion notebook for the new [Data Science Solutions](https://strtupsci.com) book. The code is explained in the book.\n", | ||
"\n", | ||
"**IMPORTANT.** The code will only run if you have a local DynamoDB server installed and running on localhost port 8000." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Checking if table exists\n", | ||
"Table does not exist\n", | ||
"Creating table\n", | ||
"Table CensusTest created\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import boto3\n", | ||
"\n", | ||
"resource = boto3.resource(\n", | ||
" 'dynamodb',\n", | ||
" endpoint_url='http://localhost:8000')\n", | ||
"\n", | ||
"client = boto3.client(\n", | ||
" 'dynamodb',\n", | ||
" endpoint_url='http://localhost:8000')\n", | ||
"\n", | ||
"table_to_create = 'Census'\n", | ||
"print('Checking if table exists')\n", | ||
"\n", | ||
"try:\n", | ||
" table_description = client.describe_table(TableName=table_to_create)\n", | ||
" print('Table {} already exists'.format(table_to_create))\n", | ||
" print('Table description:')\n", | ||
" print(table_description)\n", | ||
"\n", | ||
"# Exception raised by describe_table if table does not exist\n", | ||
"except Exception as e:\n", | ||
" if 'ResourceNotFoundException' in str(e):\n", | ||
" print('Table does not exist')\n", | ||
" print('Creating table')\n", | ||
" table = resource.create_table(\n", | ||
" TableName=table_to_create,\n", | ||
" KeySchema=[\n", | ||
" {'AttributeName': 'uid', 'KeyType': 'HASH'}\n", | ||
" ],\n", | ||
" AttributeDefinitions=[\n", | ||
" {'AttributeName': 'uid', 'AttributeType': 'N' }\n", | ||
" ],\n", | ||
" ProvisionedThroughput={\n", | ||
" 'ReadCapacityUnits': 10,\n", | ||
" 'WriteCapacityUnits': 10\n", | ||
" }\n", | ||
" )\n", | ||
" \n", | ||
" #wait for contirmation that the table exists\n", | ||
" table.meta.client.get_waiter(\n", | ||
" 'table_exists').wait(TableName=table_to_create)\n", | ||
" print('Table {} created'.format(table_to_create))\n", | ||
" else:\n", | ||
" print('Table cannot be created')\n", | ||
" raise" | ||
] | ||
} | ||
], | ||
"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.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
57 changes: 57 additions & 0 deletions
57
.ipynb_checkpoints/aws-dynamodb-delete-table-checkpoint.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"collapsed": false | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Table Census deleted\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"import boto3\n", | ||
"\n", | ||
"resource = boto3.resource('dynamodb', endpoint_url='http://localhost:8000')\n", | ||
"\n", | ||
"table_to_delete = 'Census'\n", | ||
"\n", | ||
"try:\n", | ||
" table = resource.Table(table_to_delete)\n", | ||
" table.delete()\n", | ||
" print('Table {} deleted'.format(table_to_delete))\n", | ||
"\n", | ||
"except Exception as e:\n", | ||
" if 'ResourceNotFoundException' in str(e):\n", | ||
" print('Table {} does not exist'.format(table_to_delete))" | ||
] | ||
} | ||
], | ||
"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.0" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |
Oops, something went wrong.