Quick Start¶
Install¶
pip install cerami
I have boto3 and aws credentials set up¶
# create the db singleton
import boto3
from cerami import Cerami
dynamodb = boto3.client('dynamodb')
db = Cerami(dynamodb)
# create classes that inherit from the singleton
from cerami.datatype import String, Set, Datetime
from cerami.decorators import primary_key
@primary_key('name', 'artist')
class Album(db.Model):
__tablename__ = "Albums"
name = String()
artist = String()
songs = Set(String())
released_date = Datetime()
# Some Query Examples
Album.scan \
.filter(Album.released_date.begins_with("1996")) \
.execute()
Album.query \
.key(Album.name == "The Black Album") \
.execute()
Album.get \
.key(Album.name == "Reasonable Doubt") \
.key(Album.artist == "Jay-Z") \
.execute()
I have never used boto3 or dynamodb before¶
You can run DynamoDB locally!
You need to install the aws2 cli and have dynamo db running locally. Dynamodb requires java to run locally as well so good luck if you dont have it. Try these steps first and see how it goes.
Download DynamoDB Locally¶
Unzip/Untar the content
Move to somewhere you wont lose it.
Download the AWS2 CLI¶
Follow the install instructions
Configure the AWS2 CLI¶
In order to run DynamoDB locally, you need to configure your aws as such:
aws2 configure
# These are the actual values to use with the local dynamodb instance
AWS Access Key ID: "fakeMyKeyId"
AWS Secret Access Key: "fakeSecretAccessKey"
us-west-1
Starting DynamoDB Locally¶
java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
Creating a DynamoDB Table¶
import boto3
dynamodb = boto3.client('dynamodb', endpoint_url="http://localhost:8000")
dynamodb.create_table(
TableName='Albums',
KeySchema=[
{
'AttributeName': 'name',
'KeyType': 'HASH' #Partition key
},
{
'AttributeName': 'artist',
'KeyType': 'Range', # Sort key
},
],
AttributeDefinitions=[
{
'AttributeName': 'name',
'AttributeType': 'S'
},
{
'AttributeName': 'artist',
'AttributeType': 'S'
},
],
ProvisionedThroughput={
'ReadCapacityUnits': 10,
'WriteCapacityUnits': 10
}
)
Using Cerami¶
# Create the db singleton
import boto3
from cerami import Cerami
dynamodb = boto3.client('dynamodb', endpoint_url="http://localhost:8000")
db = Cerami(dynamodb)
# create classes that inherit from the singleton
from cerami.datatype import String, Set, Datetime
from cerami.decorators import primary_key
@primary_key('name', 'artist')
class Album(db.Model):
__tablename__ = "Albums"
name = String()
artist = String()
songs = Set(String())
released_date = Datetime()