![](/img/trans.png)
[英]How to use 'db.query' for the NodeJS AWS-SDK for DynamoDB
[英]How to use 'BatchGetItem' for the NodeJS AWS-SDK for DynamoDB
我正在嘗試使用 Node JS AWS-SDK 從 DynamoDB 表中獲取項目。 函數getItem
工作正常,但BatchGetItem
更難使用。
我使用官方文檔: http : //docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/Client.html#batchGetItem-property
我正在尋找有關如何正確使用此功能的示例,但找不到任何示例。 我寫的代碼是:
var params = {
"RequestItems" : {
"Keys" : [
{"HashKeyElement" : { "N" : "1000" } },
{"HashKeyElement" : { "N" : "1001" } }
]
}
}
db.client.batchGetItem(params, function(err, data) {
console.log('error: '+ err);
console.log(jsDump.parse(data));
});
我收到一個SerializationException: Start of list found where not expected
error 但就我的 NodeJS 和 JSON 專業知識而言,我的語法是正確的。 但它令人困惑: http : //docs.aws.amazon.com/amazondynamodb/latest/developerguide/API_BatchGetItems.html
在該語法示例中,您必須提供表名。
我使用了 dynamo db 客戶端版本......經過一個小時的研究,我設法讓它工作......
var params = {
RequestItems: { // map of TableName to list of Key to get from each table
Music: {
Keys: [ // a list of primary key value maps
{
Artist: 'No One You Know',
SongTitle:'Call Me Today'
// ... more key attributes, if the primary key is hash/range
},
// ... more keys to get from this table ...
],
AttributesToGet: [ // option (attributes to retrieve from this table)
'AlbumTitle',
// ... more attribute names ...
],
ConsistentRead: false, // optional (true | false)
},
// ... more tables and keys ...
},
ReturnConsumedCapacity: 'NONE', // optional (NONE | TOTAL | INDEXES)
};
docClient.batchGet(params, function(err, data) {
if (err) ppJson(err); // an error occurred
else ppJson(data); // successful response
});
我感覺到你的痛苦...... AWS 文檔充其量是令人困惑的。 我認為這是由老化的基礎設施和糟糕的技術寫作造成的。 SDK 使用的 nodejs 和 JSON 語法讓我想起了 XML 結構。
無論如何,我設法在一個小時后讓 BatchGetItem 工作。 參數應如下所示:
{
"RequestItems": {
"<TableName>": {
"Keys": [
{"<HashKeyName>": {"<type>":"<hash key value>"}},
{"<HashKeyName>": {"<type>":"<hash key value>"}},
{"<HashKeyName>": {"<type>":"<hash key value>"}}
]
}
}
}
我相信你缺少表名。 你要這個:
var params = {
"RequestItems" : {
"TableName": {
"Keys" : [
{"HashKeyElement" : { "N" : "1000" } },
{"HashKeyElement" : { "N" : "1001" } }
]
}
}
}
我在這里嘗試了所有解決方案,但沒有一個對我有用,這可能意味着 NodeJS 庫已經更新。 參考他們寫得更好的文檔,你應該能夠提出這樣的請求:
var params = {
RequestItems: {
'Table-1': {
Keys: [
{
HashKey: 'haskey',
NumberRangeKey: 1
}
]
},
'Table-2': {
Keys: [
{ foo: 'bar' },
]
}
}
};
var docClient = new AWS.DynamoDB.DocumentClient();
docClient.batchGet(params, function(err, data) {
if (err) console.log(err);
else console.log(data);
});
具體來說,不再需要提供類型。
試試這個:
db.client.batchGetItem(
{"RequestItems":{
"TableName":{
"Keys":[
{"HashKeyElement" : {"N":"1000"}},
{"HashKeyElement" : {"N":"1001"}}
]
}
}
}, function(err, result){ //handle error and result here });
在您的情況下,正確答案應該是:
var params = {
"RequestItems": {
"<table_name>": {
"Keys": [
{"HashKeyElement" : { "N" : "1000" } },
{"HashKeyElement" : { "N" : "1001" } }
]
}
}
}
試試這個,雖然它未經測試:
var params = {
TableName: "tableName",
RequestItems : {
Keys : [
{
HashKeyElement : { N : "1000" }
},
{
HashKeyElement : { N : "1001" }
}
]
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.