简体   繁体   中英

in Lambda python query DynamoDb does not return any data

I wrote this function to return data from table where Artist name is 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. That would mean your handler consists of just the 2 print statements, and the table lookup code is outside of that. It would make sense that you would only see the results of those 2 print statements.

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.

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. The AWS documentation on Lambda Function Handler (Python) has more details, including discussion of when the return value is significant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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