使用AWS中的脚本查询DynamoDB



我有一个DynamoDB表。我需要对此表执行写/读/删除操作。这需要使用脚本来完成。我能想到两种方法:

  1. 使用AWS Lambda
  2. 使用AWS Cloud9

使用AWS提供的服务,还有其他方法可以完成这项任务吗?

对于使用AWS Lambda,有一种方法可以将DynamoDB表与AWS Lambda函数集成。首先,您需要:

1.创建一个新的Lambda函数和DynamoDB数据库,将集成在一起

如果您已经创建了DynamoDB表,则需要做的第一件事是:

创建函数:

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

部署功能和数据库:

amplify push
  1. 接下来,您可以通过以下方式从Node.js中的Lambda调用DynamoDB表:

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中获取主键。get请求返回一个项目,给定该项目的主键:

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 

最新更新