AWS SDK for JavaScript V3 參API考指南詳細描述了 AWS SDK for JavaScript 版本 3(V3)的所有API操作。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在 DynamoDB 中批次讀取和寫入項目
這個 Node.js 程式碼範例會說明:
如何讀取和寫入 DynamoDB 表中的批次項目。
該方案
在此範例中,您會使用一系列 Node.js 模組,將一批項目放入 DynamoDB 表格中,並讀取一批項目。程式碼會使用 SDK for JavaScript 來執行使用 DynamoDB 用戶端類別的下列方法來執行批次讀取和寫入作業:
必要工作
若要設定和執行此範例,請先完成這些任務:
-
設置項目環境以運行這些節點 TypeScript 示例,並安裝所需 AWS SDK for JavaScript 的第三方模塊。按照上的說明進行操作 GitHub
。 透過使用者登入資料建立共用組態檔。如需有關提供共用認證檔案的詳細資訊,請參閱《工具參考指南》中的共用設定AWS SDKs和認證檔案。
建立一個您可以存取其項目的 DynamoDB 表格。如需有關建立 DynamoDB 資料表的詳細資訊,請參閱。在 DynamoDB 中建立和使用資料表
重要
這些範例使用 ECMAScript6 (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
您可以在這裡
在批次中寫入項目
以檔名 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
您可以在這裡