Louie NRT Story

[AWS] Lambda programming for DynamoDB 본문

서버시스템

[AWS] Lambda programming for DynamoDB

hyeok0724.kim@gmail.com 2019. 1. 6. 00:10
반응형
Create Table

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import boto3
 
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
table = dynamodb.create_table(
    TableName='Movies',
    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)
 
cs


Insert Data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import boto3
import json
import decimal
 
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if abs(o) % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)
 
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
 
table = dynamodb.Table('Movies')
 
title = "The Big New Movie"
year = 2015
 
response = table.put_item(
   Item={
        'year': year,
        'title': title,
        'info': {
            'plot':"Nothing happens at all.",
            'rating': decimal.Decimal(0)
        }
    }
)
 
print("PutItem succeeded:")
print(json.dumps(response, indent=4, cls=DecimalEncoder))
 

cs


Select Data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from __future__ import print_function # Python 2/3 compatibility
import boto3
import json
import decimal
from boto3.dynamodb.conditions import Key, Attr
 
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            return str(o)
        return super(DecimalEncoder, self).default(o)
 
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
 
table = dynamodb.Table('Movies')
 
print("Movies from 1992 - titles A-L, with genres and lead actor")
 
response = table.query(
    ProjectionExpression="#yr, title, info.genres, info.actors[0]",
    ExpressionAttributeNames="#yr""year" }, # Expression Attribute Names for Projection Expression only.
    KeyConditionExpression=Key('year').eq(1992& Key('title').between('A''L')
)
 
for i in response[u'Items']:
    print(json.dumps(i, cls=DecimalEncoder))
 
cs

Update Data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import boto3
import json
import decimal
 
# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)
 
dynamodb = boto3.resource('dynamodb', region_name='us-west-2')
 
table = dynamodb.Table('Movies')
 
title = "The Big New Movie"
year = 2015
 
response = table.update_item(
    Key={
        'year': year,
        'title': title
    },
    UpdateExpression="set info.rating = :r, info.plot=:p, info.actors=:a",
    ExpressionAttributeValues={
        ':r': decimal.Decimal(5.5),
        ':p'"Everything happens all at once.",
        ':a': ["Larry""Moe""Curly"]
    },
    ReturnValues="UPDATED_NEW"
)
 
print("UpdateItem succeeded:")
print(json.dumps(response, indent=4, cls=DecimalEncoder))
 
cs


[출처] https://www.qwiklabs.com

반응형

'서버시스템' 카테고리의 다른 글

[Cloud_Class] 190104 Githook  (0) 2019.01.09
[AWS] Lambda for S3  (0) 2019.01.06
[AWS] Quick Starts  (0) 2019.01.04
[Cloud_Class] 190103 Elastic Beanstalk  (0) 2019.01.04
[Cloud_Class] 190102 AWS Summary  (0) 2019.01.03
Comments