[英]Query DynamoDB using a script in AWS
我有一個 DynamoDB 表。 我需要在這個表上執行寫/讀/刪除操作。 這需要使用腳本來完成。 我可以想到兩種方法:
有沒有其他方法可以使用 AWS 提供的服務來完成這項任務?
對於使用 AWS Lambda,有一種方法可以將您的 DynamoDB 表與 AWS Lambda function 集成。 首先,您需要:
1.新建Lambda function和DynamoDB數據庫集成在一起
如果您已經創建了 DynamoDB 表,您需要做的第一件事是:
創建 function:
amplify add function
? Provide a friendly name for your resource to be used as a label for this category in the project: mylambda
? Provide the AWS Lambda function name: mylambda
? Choose the function runtime that you want to use: NodeJS
? Choose the function template that you want to use: Hello World
? Do you want to access other resources created in this project from your Lambda function? Y
? Select the category: storage
? Select the operations you want to permit for testtable: create, read, update, delete
? Do you want to invoke this function on a recurring schedule? N
? Do you want to edit the local lambda function now? N
部署 function 和數據庫:
amplify push
A.) 從 Lambda 在 DynamoDB 中創建一個項目
要在 DynamoDB 中創建項目,您可以使用 put 方法:
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName : 'your-table-name',
/* Item properties will depend on your application concerns */
Item: {
id: '12345',
price: 100.00
}
}
async function createItem(){
try {
await docClient.put(params).promise();
} catch (err) {
return err;
}
}
exports.handler = async (event) => {
try {
await createItem()
return { body: 'Successfully created item!' }
} catch (err) {
return { error: err }
}
};
從 Lambda 獲取 DynamoDB 中的主鍵:您也可以使用 get 方法從 DynamoDB 中獲取主鍵。 給定項目的主鍵,獲取請求返回單個項目:
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName : 'your-table-name',
/* Item properties will depend on your application concerns */
Key: {
id: '12345'
}
}
async function getItem(){
try {
const data = await docClient.get(params).promise()
return data
} catch (err) {
return err
}
}
exports.handler = async (event, context) => {
try {
const data = await getItem()
return { body: JSON.stringify(data) }
} catch (err) {
return { error: err }
}
}
掃描表:這將通過訪問表或二級索引中的每個項目來返回一個或多個項目和項目屬性。
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
const params = {
TableName : 'your-table-name'
}
async function listItems(){
try {
const data = await docClient.scan(params).promise()
return data
} catch (err) {
return err
}
}
exports.handler = async (event, context) => {
try {
const data = await listItems()
return { body: JSON.stringify(data) }
} catch (err) {
return { error: err }
}
}
查詢表:查詢通過主鍵或二級索引從表中查詢項目,返回一個或多個項目和項目屬性。
const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
var params = {
TableName: 'your-table-name',
IndexName: 'some-index',
KeyConditionExpression: '#name = :value',
ExpressionAttributeValues: { ':value': 'shoes' },
ExpressionAttributeNames: { '#name': 'name' }
}
async function queryItems(){
try {
const data = await docClient.query(params).promise()
return data
} catch (err) {
return err
}
}
exports.handler = async (event, context) => {
try {
const data = await queryItems()
return { body: JSON.stringify(data) }
} catch (err) {
return { error: err }
}
}
我的回答是出於開發目的的快速解決方案,不利於制定徹底的策略或適當的架構。
如果您在 cloud9 環境中工作並希望在該環境中快速查詢 dynamodb,您可能會發現創建一個 python 腳本很有用,然后您可以快速輕松地運行該腳本。 這將使您不必部署/配置額外的基礎設施。
Python 代碼:
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb', region_name='ab-cont-1')
table = dynamodb.Table('MyTableName')
response = table.query(
ProjectionExpression="Col1, Col2",
KeyConditionExpression=Key('Col1').eq(123)
)
print(response['Items'])
如果在當前工作目錄中將其保存為script.py ,則來自 shell:
#pip3 install boto3
python3 script.py
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.