简体   繁体   English

在Lambda python查询DynamoDb中不返回任何数据

[英]in Lambda python query DynamoDb does not return any data

I wrote this function to return data from table where Artist name is Joe. 我编写了此函数,以从歌手姓名为Joe的表中返回数据。 But the code below is not resulting anything. 但是下面的代码没有任何结果。 It does first print. 它会先打印。 But after that nothing. 但是之后没事了。 Not sure what I am doing wrong. 不知道我在做什么错。

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'])

Here is the result I am getting. 这是我得到的结果。

START RequestId: ...... Version: $LATEST
Joe's music
2017-07-19 03:07:54.701000+00:00
END RequestId: ...

Quoting a piece of your code sample: 引用一段代码示例:

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'])

Is that exactly the code you have, including the indentation? 这就是您所拥有的代码,包括缩进吗? Recall that indentation is significant to program structure in Python. 回想缩进对于Python中的程序结构很重要。 That would mean your handler consists of just the 2 print statements, and the table lookup code is outside of that. 这意味着您的handler仅由2条print语句组成,而表查找代码不在此范围内。 It would make sense that you would only see the results of those 2 print statements. 您只会看到这两个print语句的结果,这很有意义。

Perhaps you meant something more like this, with indentation changed to group the table lookup and response handling code with the rest of the handler code. 也许您的意思更像这样,将缩进更改为将表查找和响应处理代码与其余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'])

Additionally, perhaps you want to return a value, depending on your needs. 此外,也许您想根据需要return一个值。 The AWS documentation on Lambda Function Handler (Python) has more details, including discussion of when the return value is significant. 有关Lambda函数处理程序(Python)的AWS文档具有更多详细信息,包括有关return值何时有意义的讨论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM