[英]in Lambda python query DynamoDb does not return any data
我编写了此函数,以从歌手姓名为Joe的表中返回数据。 但是下面的代码没有任何结果。 它会先打印。 但是之后没事了。 不知道我在做什么错。
from __future__ import print_function # Python 2/3 compatibility
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('Music')
def handler(event, context):
print("Joe's music")
print(table.creation_date_time)
response = table.query(
KeyConditionExpression=Key('Artist').eq('Joe')
)
for i in response['Items']:
print(i['Artist'], ":", i['Artist'])
这是我得到的结果。
START RequestId: ...... Version: $LATEST
Joe's music
2017-07-19 03:07:54.701000+00:00
END RequestId: ...
引用一段代码示例:
def handler(event,context):
print("Joe's music")
print(table.creation_date_time)
response = table.query(
KeyConditionExpression=Key('Artist').eq('Joe')
)
for i in response['Items']:
print(i['Artist'], ":", i['Artist'])
这就是您所拥有的代码,包括缩进吗? 回想缩进对于Python中的程序结构很重要。 这意味着您的handler
仅由2条print
语句组成,而表查找代码不在此范围内。 您只会看到这两个print
语句的结果,这很有意义。
也许您的意思更像这样,将缩进更改为将表查找和响应处理代码与其余handler
代码分组在一起。
def handler(event,context):
print("Joe's music")
print(table.creation_date_time)
response = table.query(
KeyConditionExpression=Key('Artist').eq('Joe')
)
for i in response['Items']:
print(i['Artist'], ":", i['Artist'])
此外,也许您想根据需要return
一个值。 有关Lambda函数处理程序(Python)的AWS文档具有更多详细信息,包括有关return
值何时有意义的讨论。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.