简体   繁体   中英

php Get items from DynamoDB table using a list

I have an array of usernames and I want to retrieve from a DynamoDB table all the items that contains usernames that exist in the array.

For Example: if my array is:

["Foo","Bar"]

and my table is:

username | attribute1 | attribute2 Foo | Value 1 | Value 2 Min | value1 | value2 Bar | value 1 | value 2

Then i want to get the first item and the last item.

I read the documentation for the scan operation but it doesn't seem to support this type of request.

Is what I'm asking for possible or am i going to have to iterate over my usernames array and retrieve items for each of them separately?

You may want to use the Batch Get Item API if user name is the Hash Key of the table.

The Batch Get Item API allows you to retrieve multiple items from the DynamoDB table based, provided the keys you provide in the request are part of the HashKey(+RangeKey) or a Global Secondary Index (HashKey (+RangeKey)).

Here is the batch get example. Please note that you may need to execute the batch API until UnprocessedKeys is null.

Batch Get API

var params = {
    "RequestItems" : {
        "yourtablename" : {
            "Keys" : [ {
                "username" : "Foo"              
            },{
                "username" : "Bar"              
            } ],
        }
    },
    "ReturnConsumedCapacity" : "TOTAL"
};

docClient.batchGet(params, function(err, data) {
    if (err) {
        console.error("Unable to read item. Error JSON:", JSON.stringify(err,
                null, 2));
    } else {
        console.log("GetItem succeeded:", JSON.stringify(data, null, 2));
    }
});

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