简体   繁体   中英

How to search for a list of maps in dynamodb using boto3

With the json below in dynamodb how would I search any item where session.id == "12ghbcyg" using boto3 in python?

{
    "name": "John",
    "sessions": [
        {
            "id": "12ghbcyg",
            "foo": "bar"
        }
    ]
}

Here is the sample for scan using contains. Please note that scan is a costly operation if the table has millions of items. Normally, scan is used if you don't know the partition key value. Even if you don't know the partition key value, the most recommended solution is to use Global Secondary Index (GSI) and query the index directly rather than table.

You may need to explore the above options and decide the approach suitable for your use case.

The below code is to give an idea of using SCAN API using contains . You can use CONTAINS similarly on QUERY API if you know the partition key value.

Scan using contains:-

from __future__ import print_function # Python 2/3 compatibility
import boto3
from botocore.exceptions import ClientError
import json
import decimal
from boto3.dynamodb.conditions import Key, Attr

# Helper class to convert a DynamoDB item to JSON.
class DecimalEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            if o % 1 > 0:
                return float(o)
            else:
                return int(o)
        return super(DecimalEncoder, self).default(o)

dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")

table = dynamodb.Table('usertable')

print("Scanning...")

sessionData = {
        "id": "12ghbcyg",
        "foo": "bar"
       }

fe = Attr('sessions').contains(sessionData)

response = table.scan(
        FilterExpression=fe        
    )

for i in response['Items']:

    print(json.dumps(i, cls=DecimalEncoder))

while 'LastEvaluatedKey' in response:
    response = table.scan(        
        FilterExpression=fe,        
        ExclusiveStartKey=response['LastEvaluatedKey']
        )

    for i in response['Items']:
        print(json.dumps(i, cls=DecimalEncoder))

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