Lab: Dynamo DB

一.Using python to access DynamoDB.

Inside the command prompt, enter: 

aws configure --profile dbaccess

Install the boto3 library using pip (if you have not done so). 

pip install boto3

Create a python script dynamodb1.py.

import boto3
import pprint
session = boto3.Session(
 aws_access_key_id = '你的access_key',
 aws_secret_access_key='你的secret_key'
)
dynamodb = session.resource('dynamodb', region_name='us-east-2' )
table = dynamodb.Table('users')
def table_scan():
 result = table.scan()
 for i in result['Items']:
  print(i)

table_scan()

open your cmd and input

python dynamodb1.py

Modify dynamodb1.py to comment out the call to table_scan().

Add the following code to add an item to users table.

import boto3
import pprint
session = boto3.Session(
 aws_access_key_id = '你的access_key',
 aws_secret_access_key='你的secret_key'
)
dynamodb = session.resource('dynamodb', region_name='us-east-2' )
table = dynamodb.Table('users')
# def table_scan():
#  result = table.scan()
#  for i in result['Items']:
#   print(i)
#
# table_scan()
def insert_item_db():
  response = table.put_item(
   Item={
   'username': 'janedoe',
   'first_name': 'Jane',
   'last_name': 'Doe',
   'age': 25,
   'hobbies':['badminton', 'foodball','singing'],
   'account_type': 'standard_user'
   }
 )
  pprint.pprint (response)//注意,这里不能用pprint(response),否则会报错

insert_item_db()

在这里遇到一个问题,如果最后写pprint(response)则会报错,显示模块未找到,应该改成pprint.pprinit

原因分析

Python导入模块的方法有两种:

import module 和 from module import

区别是前者所有导入的东西使用时需加上模块名的限定,而后者则不需要

import xxx

import pprint
pprint.pprint(people)

from xxx import

from pprint import *
pprint(people)

The put_item() will return the follow JSON object

Verify in DynamoDB website that the item is created.

Comment out the call to insert_item_db().

Add the following code to retrieve an item using the primary key

def get_db_item(): #retrieve an item using primary key
 response = table.get_item(
 Key={
 'username': 'janedoe'
 }
 )
 item = response['Item']

 print(item)
get_db_item()

run again

python dynamodb1.py

二.Example: Movie Table
1.Create Table

Create a table named Movies. The primary key for the table is composed of the following attributes:

• year – The partition key. The AttributeType is N for number.

• title – The sort key. The AttributeType is S for string. 

Copy the following program and paste it into a file named MoviesCreateTable.py and provide your AWS Access Key ID and Secret Access key. 

import boto3
session = boto3.Session(
 aws_access_key_id = '你的access_key',
 aws_secret_access_key='你的secret_key'
)
dynamodb = session.resource('dynamodb', region_name='us-east-2' )
def create_movie_table():
     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': 5,
             'WriteCapacityUnits': 5
         }
     )
     return table
movie_table = create_movie_table()
print("Table status:", movie_table.table_status)

then, running the programming

Check that the Movies table is created in DynamoDB

 2.Load Sample Data 

Download and unzip the movie data is in JSON format in the current folder

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/samples/moviedata.zip

Create python script MoviesLoadData.py

import boto3
import json
from decimal import Decimal
session = boto3.Session(
    aws_access_key_id = '你的access_key',
    aws_secret_access_key='你的secret_key'
)
dynamodb = session.resource('dynamodb', region_name='us-east-2' )
def load_movies(movies):
    table = dynamodb.Table('Movies')
    for movie in movies:
        year = int(movie['year'])
        title = movie['title']
        print("Adding movie:", year, title)
        table.put_item(Item=movie)
with open("moviedata.json") as json_file:
    movie_list = json.load(json_file, parse_float=Decimal)
load_movies(movie_list)

Execute the script: 

python MoviesLoadData.py

 3.Reading data from Dynamo DB tables

Options for reading data from DynamoDB tables

- GetItem

- Query

- Scan 

Create a python script MoviesQuery.py. 

import boto3
from pprint import pprint
from boto3.dynamodb.conditions import Key, Attr
session = boto3.Session(
    aws_access_key_id='AKIAUBZTK6DYWI3IVIIA',
    aws_secret_access_key='USHAJ/Q/Yl9ovlU5T/Mo5+E9P5ezsocogHs5svMV'
)
dynamodb = session.resource('dynamodb', region_name='us-east-2' )

Example 1: Read an item Use the get_item method to read a single item. You must specify the whole primary key (i.e. year and title).

def read_item(year, title): 
  table = dynamodb.Table('Movies') 
  response = table.get_item(Key={'year': year, 'title': title}) 
  movie = response['Item'] 
  pprint(movie) 
read_item(2012, 'End of Watch')

Example 2: Query All Movies Released in a Year (e.g. 2012) 

def query_movies(year):
   table = dynamodb.Table('Movies')
   response = table.query(
     KeyConditionExpression=Key('year').eq(year)
   )
   movies = response['Items']
   print(f"Movies from {year}")
   for movie in movies:
      print(movie['year'], ":", movie['title'])
query_movies(2012)

Example 3: Query All Movies Released in a Year (e.g. 2012) with Certain Titles (e.g. beginning with the letter "A" through the letter "L").

• Amazon DynamoDB returns all the item attributes by default. To get only some, rather than all of the attributes, use a projection expression

• If you need to write an expression containing an attribute name that conflicts with a DynamoDB reserved word (e.g. year), we can define an expression attribute name to use in the place of the reserved word.

• Use the KeyConditionExpression parameter to provide a specific value for the partition key.

o The Query operation will return all of the items from the table or index with that partition key value.

o We scan optionally narrow the scope of the Query operation by specifying a sort key value and a comparison operator in KeyConditionExpression.  

def query_and_project_movies(year):
    table = dynamodb.Table('Movies')
    # Expression attribute names can only reference items in the projection expression.
    response = table.query(ProjectionExpression="#yr, title, info.genres,info.actors[0]",ExpressionAttributeNames={"#yr": "year"},KeyConditionExpression= Key('year').eq(year) & Key('title').between('D', 'H'))
    print(f"Get year, title, genres, and lead actor")
    movies = response['Items']
    for movie in movies:
        print(f"
{movie['year']} : {movie['title']}")
        pprint(movie['info'])
    print(f"
Count:{response['Count']}")
    print(f"
ScanCount:{response['ScannedCount']}")
query_and_project_movies(2012)

Example 4: Use table scan to find all movies with title that begins with 'K'. 

def table_scan1():
    table = dynamodb.Table('Movies')
    response = table.scan(
    ProjectionExpression="#yr, title, info.genres, info.actors[0]",
    ExpressionAttributeNames={"#yr": "year"},
    FilterExpression=Key('title').begins_with('K')
    )
    pprint(response['Items'])
    print(f"
Count:{response['Count']}")
    print(f"
ScanCount:{response['ScannedCount']}")

table_scan1()

Example 5: Use table scan to find all movies with info.rating>=9

from decimal import *
def table_scan2():
    table = dynamodb.Table('Movies')
    response = table.scan(
     ProjectionExpression="#yr, title, info.genres, info.actors[0], info.rating",
     ExpressionAttributeNames={"#yr": "year"},
     FilterExpression=Attr('info.rating').gte(Decimal(9))

    )
    pprint(response['Items'])
    print(f"
Count:{response['Count']}")
    print(f"
ScanCount:{response['ScannedCount']}")
table_scan2()

4.CRUD Operations 

Refer to the following tutorial:

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.03.html

原文地址:https://www.cnblogs.com/ak918xp/p/13862290.html