@aws/dynamodb-query-iterator

中文文档
0.7.1发布于 2018/07/21Apache-2.0TypeScriptCommonJS近 30 天下载 10.2万

Abstraction for DynamoDB queries and scans that handles pagination and parallel worker coordination

原始使用文档
文档目录8 节
  1. Paginators
  2. QueryPaginator
  3. ScanPaginator
  4. ParallelScanPaginator
  5. Iterators
  6. QueryIterator
  7. ScanIterator
  8. ParallelScanIterator

Amazon DynamoDB Query and Scan Iteration

Apache 2 License

This library provides utilities for automatically iterating over all DynamoDB records returned by a query or scan operation using async iterables. Each iterator and paginator included in this package automatically tracks DynamoDB metadata and supports resuming iteration from any point within a full query or scan.

Paginators

Paginators are asynchronous iterables that yield each page of results returned by a DynamoDB query or scan operation. For sequential paginators, each invocation of the next method corresponds to an invocation of the underlying API operation until all no more pages are available.

QueryPaginator

Retrieves all pages of a DynamoDB query in order.

Example usage

import { QueryPaginator } from '@aws/dynamodb-query-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const paginator = new QueryPaginator(
    new DynamoDB({region: 'us-west-2'}),
    {
        TableName: 'my_table',
        KeyConditionExpression: 'partitionKey = :value',
        ExpressionAttributeValues: {
            ':value': {S: 'foo'}
        },
        ReturnConsumedCapacity: 'INDEXES'
    }
);

for await (const page of paginator) {
    // do something with `page`
}

// Inspect the total number of items yielded
console.log(paginator.count);

// Inspect the total number of items scanned by this operation
console.log(paginator.scannedCount);

// Inspect the capacity consumed by this operation 
// This will only be available if `ReturnConsumedCapacity` was set on the input
console.log(paginator.consumedCapacity);

Suspending and resuming queries

You can suspend any running query from within the for loop by using the break keyword. If there are still pages that have not been fetched, the lastEvaluatedKey property of paginator will be defined. This can be provided as the ExclusiveStartKey for another QueryPaginator instance:

import { QueryPaginator } from '@aws/dynamodb-query-iterator';
import { QueryInput } from 'aws-sdk/clients/dynamodb';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const dynamoDb = new DynamoDB({region: 'us-west-2'});
const input: QueryInput = {
    TableName: 'my_table',
    KeyConditionExpression: 'partitionKey = :value',
    ExpressionAttributeValues: {
        ':value': {S: 'foo'}
    },
    ReturnConsumedCapacity: 'INDEXES'
};

const paginator = new QueryPaginator(dynamoDb, input);

for await (const page of paginator) {
    // do something with the first page of results
    break
}

for await (const page of new QueryPaginator(dynamoDb, {
    ...input,
    ExclusiveStartKey: paginator.lastEvaluatedKey
})) {
    // do something with the remaining pages
}

Suspending and resuming the same paginator instance is not supported.

ScanPaginator

Retrieves all pages of a DynamoDB scan in order.

Example usage

import { ScanPaginator } from '@aws/dynamodb-query-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const paginator = new ScanPaginator(
    new DynamoDB({region: 'us-west-2'}),
    {
        TableName: 'my_table',
        ReturnConsumedCapacity: 'INDEXES'
    }
);

for await (const page of paginator) {
    // do something with `page`
}

// Inspect the total number of items yielded
console.log(paginator.count);

// Inspect the total number of items scanned by this operation
console.log(paginator.scannedCount);

// Inspect the capacity consumed by this operation 
// This will only be available if `ReturnConsumedCapacity` was set on the input
console.log(paginator.consumedCapacity);

Suspending and resuming scans

You can suspend any running scan from within the for loop by using the break keyword. If there are still pages that have not been fetched, the lastEvaluatedKey property of paginator will be defined. This can be provided as the ExclusiveStartKey for another ScanPaginator instance:

import { ScanPaginator } from '@aws/dynamodb-query-iterator';
import { ScanInput } from 'aws-sdk/clients/dynamodb';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const dynamoDb = new DynamoDB({region: 'us-west-2'});
const input: ScanInput = {
    TableName: 'my_table',
    ReturnConsumedCapacity: 'INDEXES'
};

const paginator = new ScanPaginator(dynamoDb, input);

for await (const page of paginator) {
    // do something with the first page of results
    break
}

for await (const page of new ScanPaginator(dynamoDb, {
    ...input,
    ExclusiveStartKey: paginator.lastEvaluatedKey
})) {
    // do something with the remaining pages
}

Suspending and resuming the same paginator instance is not supported.

ParallelScanPaginator

Retrieves all pages of a DynamoDB scan utilizing a configurable number of scan segments that operate in parallel. When performing a parallel scan, you must specify the total number of segments you wish to use, and neither an ExclusiveStartKey nor a Segment identifier may be included with the input provided.

Example usage

import { ParallelScanPaginator } from '@aws/dynamodb-query-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const paginator = new ParallelScanPaginator(
    new DynamoDB({region: 'us-west-2'}),
    {
        TableName: 'my_table',
        TotalSegments: 4,
        ReturnConsumedCapacity: 'INDEXES'
    }
);

for await (const page of paginator) {
    // do something with `page`
}

// Inspect the total number of items yielded
console.log(paginator.count);

// Inspect the total number of items scanned by this operation
console.log(paginator.scannedCount);

// Inspect the capacity consumed by this operation 
// This will only be available if `ReturnConsumedCapacity` was set on the input
console.log(paginator.consumedCapacity);

Suspending and resuming parallel scans

You can suspend any running scan from within the for loop by using the break keyword. If there are still pages that have not been fetched, the scanState property of interrupted paginator can be provided to the constructor of another ParallelScanPaginator instance:

import { 
    ParallelScanInput,
    ParallelScanPaginator,
} from '@aws/dynamodb-query-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const client = new DynamoDB({region: 'us-west-2'});
const input: ParallelScanInput = {
    TableName: 'my_table',
    TotalSegments: 4,
    ReturnConsumedCapacity: 'INDEXES'
};

const paginator = new ParallelScanPaginator(client, input);

for await (const page of paginator) {
    // do something with the first page of results
    break
}

for await (const page of new ParallelScanPaginator(
    client,
    input,
    paginator.scanState
)) {
    // do something with the remaining pages
}

Suspending and resuming the same paginator instance is not supported.

Iterators

Iterators are asynchronous iterables that yield each of record returned by a DynamoDB query or scan operation. Each invocation of the next method may invoke the underlying API operation until all no more pages are available.

QueryIterator

Retrieves all records of a DynamoDB query in order.

Example usage

import { QueryIterator } from '@aws/dynamodb-query-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const iterator = new QueryIterator(
    new DynamoDB({region: 'us-west-2'}),
    {
        TableName: 'my_table',
        KeyConditionExpression: 'partitionKey = :value',
        ExpressionAttributeValues: {
            ':value': {S: 'foo'}
        },
        ReturnConsumedCapacity: 'INDEXES'
    },
    ['partitionKey']
);

for await (const record of iterator) {
    // do something with `record`
}

// Inspect the total number of items yielded
console.log(iterator.count);

// Inspect the total number of items scanned by this operation
console.log(iterator.scannedCount);

// Inspect the capacity consumed by this operation 
// This will only be available if `ReturnConsumedCapacity` was set on the input
console.log(iterator.consumedCapacity);

ScanIterator

Retrieves all records of a DynamoDB scan in order.

Example usage

import { ScanIterator } from '@aws/dynamodb-query-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const iterator = new ScanIterator(
    new DynamoDB({region: 'us-west-2'}),
    {
        TableName: 'my_table',
        ReturnConsumedCapacity: 'INDEXES'
    },
    ['partitionKey', 'sortKey']
);

for await (const record of iterator) {
    // do something with `record`
}

// Inspect the total number of items yielded
console.log(iterator.count);

// Inspect the total number of items scanned by this operation
console.log(iterator.scannedCount);

// Inspect the capacity consumed by this operation 
// This will only be available if `ReturnConsumedCapacity` was set on the input
console.log(iterator.consumedCapacity);

ParallelScanIterator

Retrieves all pages of a DynamoDB scan utilizing a configurable number of scan segments that operate in parallel. When performing a parallel scan, you must specify the total number of segments you wish to use, and neither an ExclusiveStartKey nor a Segment identifier may be included with the input provided.

Example usage

import { ParallelScanIterator} from '@aws/dynamodb-query-iterator';
import DynamoDB = require('aws-sdk/clients/dynamodb');

const iterator = new ParallelScanIterator(
    new DynamoDB({region: 'us-west-2'}),
    {
        TableName: 'my_table',
        TotalSegments: 4,
        ReturnConsumedCapacity: 'INDEXES'
    },
    ['partitionKey']
);

for await (const record of iterator) {
    // do something with `record`
}

// Inspect the total number of items yielded
console.log(iterator.count);

// Inspect the total number of items scanned by this operation
console.log(iterator.scannedCount);

// Inspect the capacity consumed by this operation 
// This will only be available if `ReturnConsumedCapacity` was set on the input
console.log(iterator.consumedCapacity);

版本与发布记录

@aws/dynamodb-query-iterator 共发布 4 个版本,首次发布于 2018/06/17;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.7.1latest2018/07/218 年前
0.7.02018/07/118 年前
0.6.12018/06/298 年前
0.6.02018/06/178 年前

关于 @aws/dynamodb-query-iterator 的常见问题

@aws/dynamodb-query-iterator 是什么?

Abstraction for DynamoDB queries and scans that handles pagination and parallel worker coordination

如何安装 @aws/dynamodb-query-iterator?

在项目目录运行 npm install @aws/dynamodb-query-iterator,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:aws-sdk(^2.7.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@aws/dynamodb-query-iterator 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws/dynamodb-query-iterator 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@aws/dynamodb-query-iterator 可以商用吗?

@aws/dynamodb-query-iterator 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aws/dynamodb-query-iterator 的下载量有多少?

根据 npm 官方下载统计,@aws/dynamodb-query-iterator 在 2026/10/08 – 2026/11/06 这 30 天内被下载 101,741 次,日均约 3,391 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aws/dynamodb-query-iterator 还在维护吗?

最新版本 0.7.1 发布于 2018/07/21(8 年前);至今共发布 4 个版本,其中过去 12 个月发布了 0 个;首次发布于 2018/06/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。