简体   繁体   中英

How to get the item in the dynamo db which match particular key

  • I have dynamo db, user as partition_key and time as sort_key
  • I need to extract from dynamodb where user=abc@gmail.com and message=192.168.1.1

Using filter Attr , I can extract with key and value

table.scan(FilterExpression=Attr("message").eq("192.168.1.1"))

Now how to extract for particular user?

There are two primary ways to fetch data in DynamoDB; scan and query

The query operation can be used to fetch an item(s) when the partition key is known. The scan operation returns one or more items by searching the entire database. As you might guess, the query operation is preferred over the scan operation because it's a more performant operation.

In your example, the partition key is known. Therefore, you should be using the query operation to fetch the data you are after. The operation should look something like this

table.query( {
        "TableName": "YOUR TABLE NAME HERE",
        "KeyConditionExpression": "#user = :user",
        "FilterExpression": "#message = :message",
        "ExpressionAttributeNames": {"#user":"user","#message":"message"},
        "ExpressionAttributeValues": {":user": {"S":"abc@gmail.com"},":message": {"S":"192.168.1.1"}}
    })

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