我以不同的方式研究了DynamoDB。我想要一个AWS CLI版本,但是要搜索的关键字和术语非常困难。
所以我决定做一个javascript版本。
任何人都可以将此节点的dynamodb查询转换为AWS CLI吗?
创建表格
const AWS = require("aws-sdk");
const region = "us-west-2";
AWS.config.update({
region
});
AWS.config.dynamodb = {
endpoint: new AWS.Endpoint('http://localhost:8005'),
region
}
const dynamodb = new AWS.DynamoDB() //low-level client
const tableName = "Services";
const params = {
TableName : tableName,
KeySchema: [
{ AttributeName: "pk", KeyType: "HASH"}, //Partition key
{ AttributeName: "sk", KeyType: "RANGE" } //Sort key
],
AttributeDefinitions: [
{ AttributeName: "pk", AttributeType: "S" },
{ AttributeName: "sk", AttributeType: "S" }
],
ProvisionedThroughput: {
ReadCapacityUnits: 10,
WriteCapacityUnits: 10
}
};
dynamodb.createTable(params, function(err, data) {
if (err) {
console.error("Unable to create table. Error JSON:", JSON.stringify(err, null, 2));
} else {
console.log("Created table. Table description JSON:", JSON.stringify(data, null, 2));
}
});
插入2行:
const AWS = require("aws-sdk");
const region = "us-west-2";
AWS.config.update({
region
});
AWS.config.dynamodb = {
endpoint: new AWS.Endpoint('http://localhost:8005'),
region
}
const ddbClient = new AWS.DynamoDB()
const params = {
RequestItems: {
"Services": [
{
PutRequest: {
Item: {
"pk": { S:"1"},
"sk": { S:"1"},
"Service": {S:"Express"},
"Range1": {N:"0000000"},
"Range2": {N:"2000000"},
"Counters": {N:"0"},
"TriggerUpdate": {N:"1"}
}
}
},
{
PutRequest: {
Item: {
"pk": { S:"2"},
"sk": { S:"2"},
"Service": {S:"Saver"},
"Range1": {N:"2000001"},
"Range2": {N:"3999999"},
"Counters": {N:"0"},
"TriggerUpdate": {N:"1"}
}
}
}
]
}
};
ddbClient.batchWriteItem(params, function(err, data) {
if (err) {
console.error("Unable to write data: ", JSON.stringify(err, null, 2));
} else {
console.log("PutItem succeeded");
}
});
更新一个特定的项目。我不能搜索我要找的东西,它应该增加计数器列。谁也可以教我怎么做吗?
const AWS = require("aws-sdk");
const region = "us-west-2";
AWS.config.update({
region
});
AWS.config.dynamodb = {
endpoint: new AWS.Endpoint('http://localhost:8005'),
region
}
// const ddbClient = new AWS.DynamoDB()
const ddbClient = new AWS.DynamoDB.DocumentClient()
const params = {
TableName: "Services",
Key: {
pk: "1",
sk: "1"
},
UpdateExpression: "add Counters :value",
ExpressionAttributeValues: {
":val": "1"
},
ReturnConsumedCapacity: 'TOTAL',
ReturnValues: 'ALL_NEW',
};
return ddbClient.update(params).promise();
示例结果为:aws dynamodb create-table --cli-input-json file://create-table.json --endpoint=http://localhost:8003
我向你保证这些代码是有效的。我可以在我的NoSQL Workbench DynamoDB Offline中看到它。
1条答案
按热度按时间yqkkidmi1#
若要透过更新项目递增计数器,您可以使用下列更新运算式(根据您的要求透过CLI):
对于您的其他JS示例,您是否可以分享您尝试通过CLI执行的操作以及您遇到的错误?