lambdaからDynamoDB内にある条件に一致したデータを取り出したい。

0

lambdaで指定したDynamoDBのテーブルから条件に一致した行だけを抜き出すため、以下のようなスクリプトを利用しています。

DynamoDBには指定した条件に一致した情報が登録があるにもかかわらず、lambdaでは"No matching item found."がプリント出力されてしまいます。

    query_kwargs = {
        "KeyConditionExpression": Key("userId").eq(userId) ,
        "FilterExpression": Key("recordId").eq(recordId)& Key("event_type").eq(event_type),
    }
    response_event = dynamodb_my_Table.query(**query_kwargs)
    print(response_event)
    if response_event['Items']:
        print(response_event)
    else:
        print("No matching item found.")

なにか解決策があればご教示いただけますと幸いです。よろしくお願いいたします。

yuuka_u
asked a month ago587 views
1 Answer
1

まずは以下の条件だけでレコードが取得できるか確認した方がよいと思います。

"KeyConditionExpression": Key("userId").eq(userId)

ここから徐々に条件を増やしていくのはいかがでしょうか?

"FilterExpression"はパーティションキー、ソートキーは使えないみたいなので注意してください。
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb/client/query.html

A FilterExpression does not allow key attributes. You cannot define a filter expression based on a partition key or a sort key.

profile picture
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
  • @Riku Kobayashiさん、いつもご回答ありがとうございます。 細かく確認することは必要そうですね。 色々試してみます。 アドバイスありがとうございました。参考にさせていただきます。

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions