DAX not working with lastEvaluatedKey

0

Hi all, I have a piece of code which is getting data for array of params after all the Promise settled i am returning the response.I am using DAX service. On first read i am getting the response but when i am reading for 2nd Time.I am not getting any response.does the implemented code works with DAX?

let queryArray: any = [];
    let response: any[] = [];
    const promiseRes: any = await Promise.allSettled(
      params.map(async (param) => {
        let data: any = await this.client.query(param).promise();

        queryArray = [...queryArray, ...data.Items];
        while (typeof data.LastEvaluatedKey != 'undefined') {
          param['ExclusiveStartKey'] = data.LastEvaluatedKey;

          data = await this.client.query(param).promise();
          queryArray = [...queryArray, ...data.Items];
        }
        return queryArray;
      })
    );
1개 답변
0

Hi there,

DAX APIs for reading and writing table items are fully compatible with standard DynamoDB APIs. Results should be no different than querying DynamoDB directly.

What happens if you bypass DAX and use the same code directly on the table? My guess is that you will have the same problem. I'm not sure, but my instinct is that there's a logic error with the way you are managing the promises and/or returning map results.

I don't have a DAX cluster running, but I wrote the following Javascript example that you should be able to quickly adapt and test on both DynamoDB and DAX. I also opted for a do...while loop which I think makes things a bit easier.

var AWS = require('aws-sdk');

// I'm using AWS SSO, so extra steps needed to make this work with SDK:
const { fromSSO } = require("@aws-sdk/credential-provider-sso");


async function main(client, params) {
    let queryResults = [];

    for (const param of params) {
        let done = false;

        do {
            let data = await client.query(param).promise();

            // Per docs below, sometimes query returns empty [] results.
            // https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Query.html
            if (data.Items.length > 0) queryResults.push(data.Items);
            param.ExclusiveStartKey = data.LastEvaluatedKey;
            done = !('LastEvaluatedKey' in data);
        } while (!done);   
    }

    return queryResults;
}

(async () => {
    try {
        // I'm using AWS SSO, so extra steps needed to make this work with SDK:
        const credentials = await fromSSO({ profile: "personal" })();
        var client = new AWS.DynamoDB.DocumentClient({
            region: 'us-west-2',
            credentials: credentials
        });  

        let params = [];
        let param1 = {
            TableName: 'test-with-sk',
            Limit: 1,
            KeyConditionExpression: "pk = :pk",
            ExpressionAttributeValues: {
                ":pk": "1"
            }
        }

        params.push(param1);
        let results = await main(client, params);
        console.log('Combined query results:\n', results.map(r => JSON.stringify(r)));

    } catch (e) {
        console.log(e);
    }
})();
mwrb
답변함 2년 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠