使用Boto3

时间:2016-07-15 01:21:50

标签: amazon-web-services pagination amazon-dynamodb boto3 nosql

我们正在为我们的DynamoDB使用boto3,我们需要对表格进行全面扫描,以便根据我们需要进行分页的其他帖子来执行此操作。但是,我们无法找到分页的工作样本。这就是我们所做的。

import boto3
client_setting = boto3.client('dynamodb', region_name='ap-southeast-2')
paginator = client_setting.get_paginator('scan')
esk = {}
data = []
unconverted_ga = ourQuery(params1, params2)
    for page in unconverted_ga:
        data.append(page)
        esk = page['LastEvaluatedKey']

我们不确切知道如何将esk作为我们下一个查询的ExclusiveStartKey。 ExclusiveStartkey参数的预期值应该是多少?我们仍然是DynamoDB的新手,我们需要学习很多东西,包括这个。谢谢!

5 个答案:

答案 0 :(得分:1)

您可以尝试使用以下代码:

Warning:   StandardWrapperValve[jsp]: Servlet.service() for servlet jsp   threw exception
java.lang.IllegalArgumentException: Cannot format given Object as a Date      
at java.text.DateFormat.format(DateFormat.java:310)       
at java.text.Format.format(Format.java:157)       
at myweb.tool.SetDate.format(SetDate.java:35)        

以下是参考文档link.

希望有所帮助

答案 1 :(得分:1)

来自Tay B在https://stackoverflow.com/a/38619425/3176550

的回答
import boto3
dynamodb = boto3.resource('dynamodb',
                          aws_session_token=aws_session_token,
                          aws_access_key_id=aws_access_key_id,
                          aws_secret_access_key=aws_secret_access_key,
                          region_name=region
)

table = dynamodb.Table('widgetsTableName')

response = table.scan()
data = response['Items']

while 'LastEvaluatedKey' in response:
    response = table.scan(ExclusiveStartKey=response['LastEvaluatedKey'])
    data.update(response['Items'])

答案 2 :(得分:0)

经过一小时的搜索,我终于找到了更好的解决方案。对于那些刚接触DynamoDB的人,我们应该错过这个 - http://docs.aws.amazon.com/amazondynamodb/latest/gettingstartedguide/GettingStarted.Python.04.html

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):
            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', endpoint_url="http://localhost:8000")

table = dynamodb.Table('Movies')

fe = Key('year').between(1950, 1959)
pe = "#yr, title, info.rating"
# Expression Attribute Names for Projection Expression only.
ean = { "#yr": "year", }
esk = None


response = table.scan(
    FilterExpression=fe,
    ProjectionExpression=pe,
    ExpressionAttributeNames=ean
    )

for i in response['Items']:
    print(json.dumps(i, cls=DecimalEncoder))

// As long as LastEvaluatedKey is in response it means there are still items from the query related to the data
while 'LastEvaluatedKey' in response:
    response = table.scan(
        ProjectionExpression=pe,
        FilterExpression=fe,
        ExpressionAttributeNames= ean,
        ExclusiveStartKey=response['LastEvaluatedKey']
        )

    for i in response['Items']:
        print(json.dumps(i, cls=DecimalEncoder))

答案 3 :(得分:0)

有点冗长,但我喜欢。

def fetch_from_table(last_key=None):
if last_key:
    response = table.query(
        IndexName='advertCatalogIdx',
        KeyConditionExpression=Key('sk').eq('CATALOG'),
        Limit=5,
        ExclusiveStartKey=last_key
    )
else:
    response = table.query(
        IndexName='advertCatalogIdx',
        KeyConditionExpression=Key('sk').eq('CATALOG'),
        Limit=5
    )
# print(response)

for item in response['Items']:
    print(item['address'])
    print('***************************')

return response.get('LastEvaluatedKey')


last_key = fetch_from_table()
while last_key != None:
    print("Running again : ")
    last_key = fetch_from_table(last_key)

答案 4 :(得分:0)

clientY