Sunday, October 9, 2016

My experiments with DynamoDB:



Been playing with this amazing machine called the Amazon Echo and I too am blown away at the way the future has talked back to me.

During the course of my travels, I discovered that while the documentation is extensive, I've come face to face with a paucity of good examples on how to do CRUD operations in DynamoDB.

My experiments involved maps, lists and sets and here are some snippets from what I could get to work by going thru the documentation and the limited content available out there. All examples were tried via Lambda functions executing in AWS, writing and reading from a DynamoDB instance in AWS.

Here is the table structure against which these were performed:



With lists:


Create:


from __future__ import print_function
import logging,time
import boto3
import botocore
import json
import decimal
import sys
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError


dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Test2')

def insert_data():
    actors=["A","B","C"]

    response = table.update_item(
   Key={
        "name":"SOME_RANDOM_KEY"
   },
   UpdateExpression="set partners = :v",
   ExpressionAttributeValues={
       ':v': actors
   },
   ReturnValues="UPDATED_NEW"
    )

    print ("Insert successful!",response)



def lambda_handler(event, context):
    # TODO implement
    insert_data()
    return 'Hello from Lambda'


..gives us...


Modify:

from __future__ import print_function
import logging,time
import boto3
import botocore
import json
import decimal
import sys
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError


dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Test2')


def modify_data():
    response = table.update_item(
        Key={
             "name":"SOME_RANDOM_KEY"
        },
        UpdateExpression="SET #p = list_append(#p, :v)",  
        ExpressionAttributeNames={
            '#p': 'partners'
        },
        ExpressionAttributeValues={
            ':v': ["99"]
        },
        ReturnValues="UPDATED_NEW"
    )
    print ("Update successful!",response)


def lambda_handler(event, context):
    # TODO implement
    modify_data()
    return 'Hello from Lambda'

..gives us...



Delete:


from __future__ import print_function
import logging,time
import boto3
import botocore
import json
import decimal
import sys
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Test2')
def remove_data():
    response = table.update_item(
        Key={
             "name":"SOME_RANDOM_KEY"
        },
        UpdateExpression="REMOVE partners[1]",
        ReturnValues="UPDATED_NEW"
    )
    print ("Del successful!",response)
 
def lambda_handler(event, context):
    # TODO implement
    remove_data()
    return 'Hello from Lambda'


..gives us...




With Sets:


Create:


from __future__ import print_function
import logging,time
import boto3
import botocore
import json
import decimal
import sys
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError


dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Test2')

def insert_data():
    actors={"A","B","C"}

    response = table.update_item(
   Key={
        "name":"SOME_RANDOM_KEY"
   },
   UpdateExpression="set partners = :v",
   ExpressionAttributeValues={
       ':v': actors
   },
   ReturnValues="UPDATED_NEW"
    )

    print ("Insert successful!",response)

    
def lambda_handler(event, context):
    # TODO implement
    insert_data()
    return 'Hello from Lambda'

..gives us..


Modify:

from __future__ import print_function
import logging,time
import boto3
import botocore
import json
import decimal
import sys
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError


dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Test2')


def modify_data():
    response = table.update_item(
        Key={
             "name":"SOME_RANDOM_KEY"
        },
        UpdateExpression="ADD partners :v",  
        ExpressionAttributeValues={
            ':v': {"99"}
        },
        ReturnValues="UPDATED_NEW"
    )
    print ("Update successful!",response)

def lambda_handler(event, context):
    # TODO implement
    modify_data()
    return 'Hello from Lambda'

..gives us..


Delete:

from __future__ import print_function
import logging,time
import boto3
import botocore
import json
import decimal
import sys
from boto3.dynamodb.conditions import Key, Attr
from botocore.exceptions import ClientError


dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Test2')

def remove_data():
    response = table.update_item(
    Key={
        "name":"SOME_RANDOM_KEY"
    },
    UpdateExpression="DELETE partners  :v ",  
    ExpressionAttributeValues={
       ':v': {"A"}    
    } ,
    ReturnValues="UPDATED_NEW"
)
    print ("Del successful!",response)
    
def lambda_handler(event, context):
    # TODO implement
    remove_data()
    return 'Hello from Lambda'

...gives us..


No comments:

Post a Comment