在 DynamoDB 中批次讀取和寫入項目 - AWS SDK for JavaScript

AWS SDK for JavaScript V3 API 參考指南詳細介紹了 AWS SDK for JavaScript 版本 3(V3)的所有 API 操作。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在 DynamoDB 中批次讀取和寫入項目

JavaScript code example that applies to Node.js execution

這個 Node.js 程式碼範例會說明:

  • 如何讀取和寫入 DynamoDB 表中的批次項目。

該方案

在此範例中,您會使用一系列 Node.js 模組,將一批項目放入 DynamoDB 表格中,並讀取一批項目。程式碼使用 SDK 執行批次讀取和寫入作業,使用 DynamoDB 用戶端類別的下列方法: JavaScript

必要工作

若要設定和執行此範例,請先完成這些任務:

重要

這些示例使用電子信息密碼 6(ES6)。這需要 Node.js 版本 13.x 或更高版本。要下載並安裝最新版本的 Node.js,請參閱 Node.js 下載。

但是,如果您更喜歡使用 CommonJS 語法,請參閱JavaScript ES6/共同語法

注意

如需這些範例中使用之資料類型的詳細資訊,請參閱 Amazon DynamoDB 中支援的資料類型和命名規則

讀取批次中的項目

以檔名 batch-get-item.js 建立一個 Node.js 模組。請務必如先前所示設定 SDK,包括下載所需的用戶端和套件。若要存取 DynamoDB,請建立DynamoDB用戶端服務物件。建立 JSON 物件,其包含取得項目批次所需的參數,在此範例中包括要讀取的一或多個資料表、要在每個資料表中讀取的索引鍵值以及會指定要傳回屬性的投射表達式。呼叫 DynamoDB 服務物件的BatchGetItemCommand方法。

import { BatchGetItemCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new BatchGetItemCommand({ RequestItems: { // Each key in this object is the name of a table. This example refers // to a PageAnalytics table. PageAnalytics: { // Each entry in Keys is an object that specifies a primary key. Keys: [ { // "PageName" is the partition key (simple primary key). // "S" specifies a string as the data type for the value "Home". // For more information about data types, // see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes and // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.DataTypeDescriptors PageName: { S: "Home" }, }, { PageName: { S: "About" }, }, ], // Only return the "PageName" and "PageViews" attributes. ProjectionExpression: "PageName, PageViews", }, }, }); const response = await client.send(command); console.log(response.Responses["PageAnalytics"]); return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node batch-get-item.js

您可以在這裡找到這個範例程式碼 GitHub。

在批次中寫入項目

以檔名 batch-write-item.js 建立一個 Node.js 模組。請務必如先前所示設定 SDK,包括下載所需的用戶端和套件。若要存取 DynamoDB,請建立DynamoDB用戶端服務物件。建立一個 JSON 物件,其中包含取得批次項目所需的參數,在此範例中包含您要寫入項目的資料表、您要為每個項目寫入的索引鍵,以及屬性及其值。呼叫 DynamoDB 服務物件的BatchWriteItemCommand方法。

import { BatchWriteItemCommand, DynamoDBClient, } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new BatchWriteItemCommand({ RequestItems: { // Each key in this object is the name of a table. This example refers // to a Coffees table. Coffees: [ // Each entry in Coffees is an object that defines either a PutRequest or DeleteRequest. { // Each PutRequest object defines one item to be inserted into the table. PutRequest: { // The keys of Item are attribute names. Each attribute value is an object with a data type and value. // For more information about data types, // see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes Item: { Name: { S: "Donkey Kick" }, Process: { S: "Wet-Hulled" }, Flavors: { SS: ["Earth", "Syrup", "Spice"] }, }, }, }, { PutRequest: { Item: { Name: { S: "Flora Ethiopia" }, Process: { S: "Washed" }, Flavors: { SS: ["Stone Fruit", "Toasted Almond", "Delicate"] }, }, }, }, ], }, }); const response = await client.send(command); console.log(response); return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node batch-write-item.js

您可以在這裡找到這個範例程式碼 GitHub。