Created
July 23, 2019 14:17
-
-
Save posilva/dcce9e623940b94b5cb6de284d151b53 to your computer and use it in GitHub Desktop.
python ddb tests
This file contains hidden or 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
# | |
# Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. | |
# | |
# This file is licensed under the Apache License, Version 2.0 (the "License"). | |
# You may not use this file except in compliance with the License. A copy of | |
# the License is located at | |
# | |
# http://aws.amazon.com/apache2.0/ | |
# | |
# This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | |
# CONDITIONS OF ANY KIND, either express or implied. See the License for the | |
# specific language governing permissions and limitations under the License. | |
# | |
from __future__ import print_function # Python 2/3 compatibility | |
import boto3 | |
import os | |
import json | |
import decimal | |
import uuid | |
from boto3.dynamodb.conditions import Key | |
TABLE_NAME = "Movies" | |
client = boto3.client('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000") | |
dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000") | |
os.environ['AWS_ACCESS_KEY_ID'] = 'ddb_test1' | |
os.environ['AWS_SECRET_ACCESS_KEY'] = 'ddb_test1' | |
def create_table(): | |
table = dynamodb.create_table( | |
TableName=TABLE_NAME, | |
KeySchema=[ | |
{ | |
'AttributeName': 'year', | |
'KeyType': 'HASH' #Partition key | |
}, | |
{ | |
'AttributeName': 'title', | |
'KeyType': 'RANGE' #Sort key | |
} | |
], | |
AttributeDefinitions=[ | |
{ | |
'AttributeName': 'year', | |
'AttributeType': 'N' | |
}, | |
{ | |
'AttributeName': 'title', | |
'AttributeType': 'S' | |
}, | |
], | |
ProvisionedThroughput={ | |
'ReadCapacityUnits': 10, | |
'WriteCapacityUnits': 10 | |
} | |
) | |
print("Table status:", table.table_status) | |
def create_gsi(): | |
try: | |
resp = client.update_table( | |
TableName=TABLE_NAME, | |
# Any attributes used in your new global secondary index must be declared in AttributeDefinitions | |
AttributeDefinitions=[ | |
{ | |
"AttributeName": "Category", | |
"AttributeType": "S" | |
}, | |
], | |
# This is where you add, update, or delete any global secondary indexes on your table. | |
GlobalSecondaryIndexUpdates=[ | |
{ | |
"Create": { | |
# You need to name your index and specifically refer to it when using it for queries. | |
"IndexName": "CategoryIndex", | |
# Like the table itself, you need to specify the key schema for an index. | |
# For a global secondary index, you can use a simple or composite key schema. | |
"KeySchema": [ | |
{ | |
"AttributeName": "Category", | |
"KeyType": "HASH" | |
} | |
], | |
# You can choose to copy only specific attributes from the original item into the index. | |
# You might want to copy only a few attributes to save space. | |
"Projection": { | |
"ProjectionType": "ALL" | |
}, | |
# Global secondary indexes have read and write capacity separate from the underlying table. | |
"ProvisionedThroughput": { | |
"ReadCapacityUnits": 1, | |
"WriteCapacityUnits": 1, | |
} | |
} | |
} | |
], | |
) | |
print("Secondary index added!") | |
except Exception as e: | |
print("Error updating table:") | |
print(e) | |
def create_item(year, title, category): | |
table = dynamodb.Table(TABLE_NAME) | |
response = table.put_item( | |
Item={ | |
'year': year, | |
'title': title, | |
'Category': category, | |
'info': { | |
'plot':"Nothing happens at all.", | |
'rating': decimal.Decimal(0) | |
} | |
} | |
) | |
print("PutItem succeeded:") | |
print(json.dumps(response, indent=4)) | |
def query(category): | |
table = dynamodb.Table(TABLE_NAME) | |
resp = table.query( | |
IndexName="CategoryIndex", | |
KeyConditionExpression=Key('Category').eq(category)) | |
print("The query returned the following items:") | |
for item in resp['Items']: | |
print(item) | |
create_item(2019, "what a film", "sport") | |
create_item(2019, "what a film", "history") | |
query('sport') | |
query('history') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment