简体   繁体   English

如何使用 AWS DynamoDB 的并行扫描对特定 GSI 运行扫描?

[英]How can i use Parallel Scan of AWS DynamoDB to run scan on a specific GSI?

I have a DynamoDB table where each Item has a key of the name 'DataType'.我有一个 DynamoDB 表,其中每个项目都有一个名为“DataType”的键。 Also there is a GSI on this table with this 'DataType' as the HashKey and 'timestamp' as rangeKey.此表上还有一个 GSI,此“DataType”作为 HashKey,“timestamp”作为 rangeKey。

Around 10 per cent of the table items have the 'DataType' value as 'A'.大约 10% 的表项的“DataType”值为“A”。

I want to scan all the items of this GSI with HashKey fixed as 'A'.我想扫描此 GSI 的所有项目,并将 HashKey 固定为“A”。 Is there any way to perform this using scan/parallel scan?有没有办法使用扫描/并行扫描来执行此操作? Or do i need to use query on GSI itself to perform this operation?或者我是否需要在 GSI 本身上使用查询来执行此操作?

As per the documentation, https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/document/spec/ScanSpec.html根据文档, https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/document/spec/ScanSpec.html

i could not find any way to specify a GSI on which i can scan with HashKey fixed.我找不到任何方法来指定我可以在其上使用固定 HashKey 进行扫描的 GSI。

I have a DynamoDB table where each Item has a key of the name 'DataType'.我有一个 DynamoDB 表,其中每个项目都有一个名为“DataType”的键。 Also there is a GSI on this table with this 'DataType' as the HashKey and 'timestamp' as rangeKey.此表上还有一个 GSI,此“DataType”作为 HashKey,“timestamp”作为 rangeKey。

Around 10 per cent of the table items have the 'DataType' value as 'A'.大约 10% 的表项的“DataType”值为“A”。

I want to scan all the items of this GSI with HashKey fixed as 'A'.我想使用固定为“A”的 HashKey 扫描此 GSI 的所有项目。 Is there any way to perform this using scan/parallel scan?有没有办法使用扫描/并行扫描来执行此操作? Or do i need to use query on GSI itself to perform this operation?还是我需要对 GSI 本身使用查询来执行此操作?

As per the documentation, https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/document/spec/ScanSpec.html根据文档, https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/dynamodbv2/document/spec/ScanSpec.html

i could not find any way to specify a GSI on which i can scan with HashKey fixed.我找不到任何方法来指定可以使用固定 HashKey 扫描的 GSI。

Given that you want to only look at items with the Hash key "A", you'll need to use the Query API rather than the Scan API, provide the index name, and query for items in the index that have that partition key.鉴于您只想查看具有 Hash 键“A”的项目,您需要使用查询 API 而不是扫描 API,提供索引名称,并查询索引中具有该分区键的项目。

Here's some sample code using the Node AWS SDK V3 that creates three items in a table with a Global Secondary Index (called GSI1 ).下面是一些使用节点 AWS SDK V3的示例代码,它在具有全局二级索引(称为GSI1 )的表中创建三个项目。 Two of the items have a GSI1PK value of "orange" , while the other has a GSI1PK value of "gold" .其中两项的GSI1PK值为"orange" ,而另一项的GSI1PK值为"gold" The query returns the two matches:查询返回两个匹配项:

    const tableName = getFromEnv(`TABLE_NAME`);
    const client = new DynamoDBClient({});

    async function createItem(
        name: string,
        PK: string,
        SK: string,
        GSI1PK: string,
        GSI1SK: string,
    ): Promise<void> {
        const item = { PK, SK, GSI1PK, GSI1SK, name };
        const putCommand = new PutItemCommand({
            TableName: tableName,
            Item: marshall(item)
        });
        await client.send(putCommand);
        log(`Created item: ${name} with GSI1PK ${GSI1PK}`);
    }

    await createItem(`foo`, `fooPK`, `fooSK`, `orange`, `blue`);
    await createItem(`bar`, `barPK`, `barSK`, `orange`, `white`);
    await createItem(`baz`, `bazPK`, `bazSK`, `gold`, `garnet`);

    log(`Waiting 5 seconds, as GSIs don't support consistent reads`)
    await wait(5);

    const query: QueryCommandInput = {
        TableName: tableName,
        IndexName: `GSI1`,
        KeyConditionExpression: `#pk = :pk`,
        ExpressionAttributeNames: {
            '#pk': `GSI1PK`,
        },
        ExpressionAttributeValues: {
            ':pk': { S: `orange` },
        },
    }

    const result = await client.send(new QueryCommand(query));
    log(`Querying GSI1 for "orange"`);
    result.Items.forEach((entry) => {
        log(`Received: `, unmarshall(entry).name);
    });

This produces the output of:这会产生 output:

Created item: foo with GSI1PK orange
Created item: bar with GSI1PK orange
Created item: baz with GSI1PK gold
Waiting 5 seconds, as GSIs don't support consistent reads
Querying GSI1 for "orange"
Received:  foo
Received:  bar

One thing worth noting from this example is that GSIs don't allow consistent reads.此示例中值得注意的一件事是 GSI 不允许一致读取。 So if your use case requires immediate consistency, you'll need to find another solution.因此,如果您的用例需要即时一致性,您将需要寻找其他解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 DynamoDB 分页扫描中使用 StartingToken - How to use StartingToken with DynamoDB pagination scan 如何获取扫描 dynamodb 表返回的项目的大小? - How can I get the size of items returned by scan dynamodb table? 无法使用 DynamoDB GSI - Can't use DynamoDB GSI 对于 aws dynamodb,hash_key 的扫描是否使用索引? - For aws dynamodb, does a scan of the hash_key use an index? Aws DynamoDB DAX 扫描表 - Aws DynamoDB DAX Scan table 如何避免dynamodb中的扫描操作 - How to avoid scan operation in dynamodb 如何从 AWS 扫描 dynamodb 表 Lambda function - How to scan the dynamodb table form the AWS Lambda function 如何从没有 object 类型的 aws-sdk-js-v3 获得完整的 DynamoDB 扫描响应? (编组响应) - How do I get a full DynamoDB scan response from the aws-sdk-js-v3 without object types? (Marshaled response) AWS DynamoDB 扫描操作适用于 Class 级别扫描,但不适用于文档编写器扫描 - AWS DynamoDB Scan Operation Works w/ Class Level Scan, but not Document Writer Scan 调用Scan操作时如何解决(AccessDeniedException):User: arn:aws:sts... is not authorized to perform: dynamodb:Scan on resource.."? - How to solve (AccessDeniedException) when calling the Scan operation: User: arn:aws:sts... is not authorized to perform: dynamodb:Scan on resource.."?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM